IDEA中关于JDBC的增删改查操作

连接数据库

1.加载驱动

Class.forName("com.mysql.jdbc.Driver");

2.创建连接

Connection conn= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useSSL=true&characterEncoding=utf-8&user=root&password=123456");

注意jdbc:mysql://127.0.0.1:3306/test?useSSL=true&characterEncoding=utf-8&user=root&password=123456
这段代码中的test即你所要连接的数据库名(我的数据库名为test),root为管理员名,123456为密码。

对数据库的操作

3.创建sql语句

String sql="insert into user (id,name,password) value (?,?,?)";

我这个sql语句是向user表中插入一条信息

4.得到statement对象

            statement=conn.prepareStatement(sql);//预编译
            statement.setString(1,"hhh");
            statement.setString(2,"哈哈哈");
            statement.setString(3,"555");

5.执行sql,得到结果集

 statement.executeUpdate();

6.处理结果集

            while(resultSet.next()){
                System.out.println(resultSet.getString(1));
                System.out.println(resultSet.getString(3));
                System.out.println(resultSet.getString(2));
            }

7.关闭资源

            if(resultSet!=null)//如果为空,会报错,空指针
            {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(statement!=null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

执行后可以在数据库中看到新增了一条用户信息
在这里插入图片描述

关于增删改查

上述对数据库的操作只是新增了一个用户信息,也就是增删改查中的增加操作,我们接下来具体看一看这几个操作的实现方法。

public void insertUser(User newUser) throws SQLException {
        //增加操作
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        String sql_insert="insert into users (id,name,password) value (?,?,?)";
        pstm=conn.prepareStatement(sql_insert);//预编译

        pstm.setString(1,newUser.getId());
        pstm.setString(2,newUser.getUsername());
        pstm.setString(3,newUser.getPassword());
        pstm.executeUpdate();
        System.out.println("新增用户成功!");


    }
    public void deleteUser(String id) throws SQLException {
        //删除操作
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        String sql_delete="delete from users where id=?";
        pstm=conn.prepareStatement(sql_delete);
        pstm.setString(1,id);
        pstm.executeUpdate();
        System.out.println("删除用户成功!");
    }
    public void updateUser(User modUser) throws SQLException {
        //修改操作
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        String sql_update="update users set name=?,password=? where id=?";
        pstm=conn.prepareStatement(sql_update);
        pstm.setString(1,modUser.getUsername());
        pstm.setString(2,modUser.getPassword());
        pstm.setString(3,modUser.getId());
        pstm.executeUpdate();
        System.out.println(modUser.getUsername()+modUser.getPassword()+modUser.getId());
        System.out.println("修改用户成功!");

    }
    public void selectByID(String id) throws SQLException {
        //通过ID查询
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String sql_selectById ="select * from users where id=?";
        pstm = conn.prepareStatement(sql_selectById);
        pstm.setString(1,id);
        System.out.println(id);
        rs=pstm.executeQuery();
        System.out.println("查询成功,信息如下:");
        System.out.println("用户编号\t\t用户名称\t\t用户密码");
        while(rs.next()){
            System.out.println(rs.getString("id")+"\t\t\t\t"+rs.getString("name")+"\t\t\t\t"+rs.getString("password"));
        }
    }

    public void selectAll() throws SQLException {
        //查询所有用户信息
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String sql_selectAll ="select * from users";
        pstm = conn.prepareStatement(sql_selectAll);
        rs=pstm.executeQuery();
        System.out.println("查询成功,信息如下:");
        System.out.println("用户编号\t\t用户名称\t\t用户密码");
        while(rs.next()){
            System.out.println(rs.getString("id")+"\t\t\t\t"+rs.getString("name")+"\t\t\t\t"+rs.getString("password"));
        }

    }

由于我将这些方法封装在UserDao类里,所以在主函数里new一个UserDao类的对象,调用这些方法,便可以对数据库进行增删改查。结果如下图所示。
在这里插入图片描述
在这里插入图片描述
这样IDEA中关于JDBC简单的增删改查操作便实现了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值