通过IDEA实现单表的增删改查

基于前一篇,连接上数据库后,现在开始实现增删改查

增加字段

 @Test
    public void testInsert() {
        try {
            Connection conn = MySqlDbUtil.getConnect();
            String sql = "insert into band(name,remark) values(?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, "19软工2班");
            pstmt.setString(2, "这是备注信息");

            pstmt.execute();
            pstmt.close();
            conn.close();

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

通过IDEA实现单表的增删改查_第1张图片

多创建几个字段,以便于观察

通过IDEA实现单表的增删改查_第2张图片

修改表

 @Test
    public void testUpdate() {
        try {
            Connection conn = MySqlDbUtil.getConnect();
            String sql = "update band set name=?, remark=? where id=?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, "新班级名称");
            pstmt.setString(2, "这是新的备注信息");
            pstmt.setInt(3, 2);
            pstmt.execute();
            pstmt.close();
            conn.close();

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

通过IDEA实现单表的增删改查_第3张图片

删除字段

 @Test
    public void testDelete() {
        try {
            Connection conn = MySqlDbUtil.getConnect();
            String sql = "delete from band where id=?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, 3);
            pstmt.execute();
            pstmt.close();
            conn.close();

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

通过IDEA实现单表的增删改查_第4张图片

查询全部字段

       @Test
    public void testFindAll() {
        try {
            Connection conn = MySqlDbUtil.getConnect();
            String sql = "select * from band";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();//结果集,
            while (rs.next()) {
                Integer id = rs.getInt("id");
                String name = rs.getString("name");
                String remark = rs.getString("remark");
                System.out.println("id =" + id + ", name =" + name + ", remark=" + remark);
            }
            rs.close();
            pstmt.execute();
            pstmt.close();
            conn.close();

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

通过IDEA实现单表的增删改查_第5张图片

查询单个字段

 @Test
    public void testFindById() {
        try {
            Connection conn = MySqlDbUtil.getConnect();
            String sql = "select * from band where id=?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,2);
            ResultSet rs = pstmt.executeQuery();//结果集,
            if (rs.next()) {
                Integer id = rs.getInt("id");
                String name = rs.getString("name");
                String remark = rs.getString("remark");
                System.out.println("id =" + id + ", name =" + name + ", remark=" + remark);
            }
            rs.close();
            pstmt.execute();
            pstmt.close();
            conn.close();

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

通过IDEA实现单表的增删改查_第6张图片

遇到的问题

我在添加字段的时候,有遇到报错问题,处理方法如下

通过IDEA实现单表的增删改查_第7张图片

打开workbench修改里面就可以了

通过IDEA实现单表的增删改查_第8张图片

你可能感兴趣的:(通过IDEA实现单表的增删改查)