(Java)实现数据库的增删改查

import cn.edu.mju.project1.persiste.impl.MySqlDbUtil;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class TextDbUtil {
    @Test
    public void  testConnection(){
        try{
            MySqlDbUtil.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void  testInsert(){
        try{
            Connection conn =MySqlDbUtil.getCOnnection();
            String sql ="INSERT INTO band(name,remark) VALUES(?,?)";
            PreparedStatement pstmt =conn.prepareStatement(sql);
            pstmt.setString(1,"软工");
            pstmt.setString(2,"备注信息");
            int result =pstmt.executeUpdate();
            if(result>0){
                System.out.println("添加成功");
            }else {
                System.out.println("添加失败");
            }
            pstmt.close();
            conn.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Test
    public void  testUpdate(){
        try{
            Connection conn =MySqlDbUtil.getCOnnection();
            String sql ="UPDATE band SET name=?,remark=? WHERE id=?";
            PreparedStatement pstmt =conn.prepareStatement(sql);
            pstmt.setString(1,"软工2");
            pstmt.setString(2,"备注信息2");
            pstmt.setInt(3,4);
            int result =pstmt.executeUpdate();
            if(result>0){
                System.out.println("添加成功");
            }else {
                System.out.println("添加失败");
            }
            pstmt.close();
            conn.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void  testDelete(){
        try{
            Connection conn =MySqlDbUtil.getCOnnection();
            String sql ="DELETE FROM band where id=?";
            PreparedStatement pstmt =conn.prepareStatement(sql);
            pstmt.setInt(1,2);
            int result =pstmt.executeUpdate();
            if(result>0){
                System.out.println("添加成功");
            }else {
                System.out.println("添加失败");
            }
            pstmt.close();
            conn.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void  testFindAll() {
        try {
            Connection conn = MySqlDbUtil.getCOnnection();
            String sql = "SELECT * FROM band";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String remark = rs.getString("remark");
                System.out.println(id + "," + name + "," + remark);
            }
            rs.close();
            pstmt.close();
            conn.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Test
    public void  testFindById() {
        try {
            Connection conn = MySqlDbUtil.getCOnnection();
            String sql = "SELECT * FROM band WHERE id=?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,6);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String remark = rs.getString("remark");
                System.out.println(id + "," + name + "," + remark);
            }
            rs.close();
            pstmt.close();
            conn.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void  testFindName() {
        try {
            Connection conn = MySqlDbUtil.getCOnnection();
            String sql = "SELECT * FROM band WHERE name LIKE ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,"%1%");
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String remark = rs.getString("remark");
                System.out.println(id + "," + name + "," + remark);
            }
            rs.close();
            pstmt.close();
            conn.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:((Java)实现数据库的增删改查)