zoukankan      html  css  js  c++  java
  • JDBC-CRUD(一)

    JDBC(一)

    DriverManager 驱动管理者

    Connection 连接(代表数据库)

    Statement 要发送的SQL对象(不安全)

    PreparedStatement 要发送的SQL对象(安全,预编译)

    ResultSet 结果集

    配置信息【此处为高版本MySQL】,因为在加载驱动时需要jdbc,连接数据库时需要url,username,password这3个参数,故直接定义在固定步骤前面。

    jdbc= com.mysql.cj.jdbc.Driver
    url=jdbc:mysql://localhost:3306/jdbc?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
    username=root
    password=123456
    

    JDBC固定步骤(一)

    1.加载驱动

    2.连接数据库,代表数据库

    3.向数据库发送SQL的对象Statement :CRUD

    4.编写SQL(根据业务,不同的SQL)

    5.执行SQL

    6.关闭连接

    【说明】:

    1、2、3、6都是不变的。

    4、5可根据业务变化。

    查询:执行时使用 statement.executeQuery(sql);

    增删改:执行时使用 statement.executeUpdate(sql);

    示例1 :Statement执行查询语句 ----> executeQuery(sql)

    public class TestJDBC {
        public static void main(String[] args) throws ClassNotFoundException, SQLException {
            //配置信息
            String url = "jdbc:mysql://localhost:3306/jdbc?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false";
            String username = "root";
            String password = "123456";
            String jdbc = "com.mysql.cj.jdbc.Driver";
    
            //1.加载驱动
            Class.forName(jdbc);
            //2.连接数据库,代表数据库 DrierManager驱动管理者
            Connection connection = DriverManager.getConnection(url, username, password);
            //3.向数据库发送SQL的对象--Statement(不安全)、PrepareStatement(安全,预编译):CRUD
            Statement statement = connection.createStatement();
            //4.编写SQL
            String sql = "select * from users";
            //5.执行查询SQL,返回一个ResultSet:结果集
            ResultSet rs = statement.executeQuery(sql);
            
            while(rs.next()){
                System.out.println("id="+rs.getObject("id"));
                System.out.println("name="+rs.getObject("name"));
                System.out.println("password="+rs.getObject("password"));
                System.out.println("email="+rs.getObject("email"));
                System.out.println("birthday="+rs.getObject("birthday"));
            }
            //6.关闭连接,释放资源(一定要做)先开后关
            rs.close();
            statement.close();
            connection.close();
        }
    }
    

    JDBC固定步骤(二):预编译

    1.加载驱动

    2.连接数据库,代表数据库

    3.编写SQL(根据业务,不同的SQL)

    4.向数据库发送能预编译SQL的PreparedStatement对象:CRUD

    5.执行SQL

    6.关闭连接

    【说明】:

    固定步骤二和一的区别就在于向数据库发送SQL的对象不同,一个为Statement (不安全),另一个为PreparedStatement(安全),二者均能执行CRUD。因为PreparedStatement需要预编译SQL,所以固定步骤中,调整了3、4的位置,先编写SQL,后发送。

    同理--》

    ​ 查询:执行时使用 ps.executeQuery(sql);

    ​ 增删改:执行时使用 ps.executeUpdate(sql);

    其实二者均还有 execute() 方法,只是用专门负责查询和专门负责增删改的方法,效率更高。

    示例2:PreparedStatement 执行查询语句 ----> executeQuery()

    public class TestJDBC2 {
        public static void main(String[] args) throws ClassNotFoundException, SQLException {
            //配置信息
            String url = "jdbc:mysql://localhost:3306/jdbc?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false";
            String username = "root";
            String password = "123456";
            String jdbc = "com.mysql.cj.jdbc.Driver";
    
            //1.加载驱动
            Class.forName(jdbc);
            //2.连接数据库,代表数据库 DrierManager驱动管理者
            Connection connection = DriverManager.getConnection(url, username, password);
            //3.编写SQL
            String sql = "select * from users";
            //4.向数据库发送能预编译SQL的PreparedStatement对象(安全)
            PreparedStatement ps = connection.prepareStatement(sql);
            //5.执行SQL
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                System.out.println("id="+rs.getObject("id"));
                System.out.println("name="+rs.getObject("name"));
                System.out.println("password="+rs.getObject("password"));
                System.out.println("email="+rs.getObject("email"));
                System.out.println("birthday="+rs.getObject("birthday"));
            }
            //6.关闭连接,释放资源(一定要做)先开后关
            rs.close();
            ps.close();
            connection.close();
        }
    }
    

    示例3:Statement执行修改语句 ---> executeUpdate(sql)

    public class TestJDBC3 {
        public static void main(String[] args) throws ClassNotFoundException, SQLException {
            //配置信息
            String url = "jdbc:mysql://localhost:3306/jdbc?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false";
            String username = "root";
            String password = "123456";
            String jdbc = "com.mysql.cj.jdbc.Driver";
    
            //1.加载驱动
            Class.forName(jdbc);
            //2.连接数据库,代表数据库 DrierManager驱动管理者
            Connection connection = DriverManager.getConnection(url, username, password);
            //3.向数据库发送SQL的对象--Statement(不安全)、PrepareStatement(安全,预编译):CRUD
            Statement statement = connection.createStatement();
            //4.编写SQL
            String sql = "update users set name = '小明' where id=1";
            //5.执行SQL    返回受影响的行数
            int count = statement.executeUpdate(sql);
            if(count > 0){
                System.out.println("修改成功@");
            }
            //6.关闭连接,释放资源(一定要做)先开后关
            statement.close();
            connection.close();
        }
    }
    
    
  • 相关阅读:
    用户可以设置每页显示记录数的分页程序
    将分页程序写成函数
    对查询结果进行分页
    使用mysql_query()方法操纵数据库以及综合实例
    php访问数据库
    cookie记录用户的浏览商品的路径
    php中如何输出当前服务器的(中国)当前时间
    mysql 与 mysqli的区别
    Django框架 之 ORM中介模型
    Django框架 之 查询 Extra
  • 原文地址:https://www.cnblogs.com/mytJava/p/12725732.html
Copyright © 2011-2022 走看看