叨叨两句

  1. ~

JDBC的使用步骤

1.注册驱动
2.获得连接
3.创建执行SQL语句的对象
4.执行SQL语句
5.关闭资源

    public static void main(String[] args) throws SQLException {
        //注册驱动
        DriverManager.registerDriver(new Driver());
        String url = "jdbc:mysql://localhost:3306/day10";
        String user = "root";
        String password = "123456";
        //获得连接
        Connection connection = DriverManager.getConnection(url, user, password);
        //创建执行sql语句对象
        Statement statement = connection.createStatement();
        //执行sql,处理结果
        String sql = "select *from user";
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            System.out.println(resultSet.getObject(1));
            System.out.println(resultSet.getObject(2));
            System.out.println(resultSet.getObject(3));
            System.out.println(resultSet.getObject(4));
            
        }
        //关闭资源
        if(resultSet != null){
            resultSet.close();
            
        }
        
        if(statement  != null){
            statement .close();
        }
        
        if(connection != null){
            connection.close();
        }
        

    }

Wall_Breaker
2.1k 声望1.2k 粉丝

生死之间,就是我的跃迁之路,全程记录,欢迎见证