JDBC的详解

1.加载驱动

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

2.获取连接
        Connection conn=DriverMAnager.getConnection(url,username,password);

3.获取sql执行对象
        PreapareStatment pst=conn.prepareStatement("sql");

4.执行sql
        ResultSet rs=pst.executeQuery(); 查询
        int i = pst.executeUpdate(); 增删改

5.查看执行结果
        rs
        i

6.关闭资源
        public static void closeDb(ResultSet rs,PreparedStatement pst,Connection connection){
        
        if(null != rs){
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        if(null != pst){
            try {
                pst.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        if(null != connection){
            try {
                connection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }

猜你喜欢

转载自blog.csdn.net/JIUTIANDEYING/article/details/80910358