• Java 第三阶段增强分析需求,代码实现能力【JDBC】


    代码链接:https://download.csdn.net/download/qq_52354698/86478243?spm=1001.2014.3001.5501

    1. JDBC概述

    1. 基本介绍

    1. JDBC为访问不同的数据库提供了统一的接口,为使用者屏蔽了细节问题。
    2. Java程序员使用JDBC,可以连接任何提供了JDBC驱动程序的数据库系统,从而完成对数据库的各种操作。

    在这里插入图片描述

    2. JDBC带来的好处

    Java 直接访问数据库
    在这里插入图片描述

    通过 JDBC 访问数据库
    在这里插入图片描述

    说明:JDBC是Java提供一套用于数据库操作的接口API, Java程序员只需要面向这套接口编程即可。不同的数据库厂商,需要针对这套接口,提供不同实现。

    3. JDBC API

    JDBC API是一系列的接口, 它统一和规范 了应用程序与数据库的连接、 执行SQL语句,并到得到返回结果等各类操作,相关类和接口在java.sql与javax.sql包中。

    在这里插入图片描述

    2. JDBC快速入门

    1. JDBC 程序编写步骤

    1. 注册驱动 - 加载 Driver 类
    2. 获取连接 - 得到 Connection
    3. 执行增删改查 - 发送 SQL 给 mysql 执行
    4. 释放资源 - 关闭相关连接

    2. JDBC第一个程序

    通过JDBC对表actor进行添加、删除、修改

    create table actor (
    id int primary key auto_increment,
    name varchar(32) not null default ' ',
    sex char(1) not null default '女',
    borndate datetime ,
    phone varchar(12));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    前置工作:在项目下创建一个文件夹libs,将mysql.jar拷贝到该目录下,点击add to project… 加入到项目中

    jdbc:mysql:规定好表示协议,通过jdbc的方式连接mysql
    localhost:主机,可以是ip地址
    3306:表示mysql监听的端口号
    qdu_01:连接到mysql的那个数据库

    String url = "jdbc:mysql://localhost:3306/qdu_01";
    
    • 1

    mysql的连接本质上就是前面学过的socket连接

    package com.qdu.jdbc;
    
    import com.mysql.jdbc.Driver;
    
    import java.sql.Connection;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    
    /**
     * @author dell
     * @version 1.0
     */
    public class Jdbc01 {
    
        public static void main(String[] args) throws SQLException {
    
            //前置工作:在项目下创建一个文件夹libs,将mysql.jar拷贝到该目录下,点击add to project.. 加入到项目中
            //注册驱动
            Driver driver = new Driver();
            //得到连接
            String url = "jdbc:mysql://localhost:3306/qdu_01?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=false";
            //将用户名和密码放入到Properties对象
            Properties properties = new Properties();
            //user 和 password 是规定好的,后面对应的就是数据库的用户名和密码
            properties.setProperty("user", "root");
            properties.setProperty("password", "root");
            Connection connection = driver.connect(url, properties);
            //执行sql
            String sql = "insert into actor values(null, '刘德华', '男', '1970-01-02', '110')";
            //statement 用于执行静态SQL语句并返回其生成的结果对象
            Statement statement = connection.createStatement();
            int rows = statement.executeUpdate(sql);//如果是dml语句,返回的就是影响的行数
            System.out.println(rows > 0 ? "成功" : "失败");
            //关闭连接资源
            statement.close();
            connection.close();
    
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    在这里插入图片描述

    可能存在的问题:对于mysql.jar包的导入要首先确定自己的mysql的版本,最后是统一版本,同时对于url的设置,6.0之前和6.0之后会有所不同,如果数据库连接失败的话,最好查看一下是否符合版本要求

    3. 获取数据库连接5种方式

    1. 方式一

    静态加载,灵活性差,依赖性强

            Driver driver = new Driver();
            String url = "jdbc:mysql://localhost:3306/qdu_01?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=false";
            Properties properties = new Properties();
            properties.setProperty("user", "root");
            properties.setProperty("password", "root");
            Connection connection = driver.connect(url, properties);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2. 方式二

    动态加载,更加的灵活,减少依赖性

            Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
            Driver driver = (Driver) aClass.newInstance();
            String url = "jdbc:mysql://localhost:3306/qdu_01?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=false";
            Properties properties = new Properties();
            properties.setProperty("user", "root");
            properties.setProperty("password", "root");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3. 方式三

    使用DriverManger替换Driver

        public void connect03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
    
            Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
            Driver driver = (Driver) aClass.newInstance();
            String url = "jdbc:mysql://localhost:3306/qdu_01?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=false";
            String user = "root";
            String password = "root";
            DriverManager.registerDriver(driver);//注册Driver驱动
            Connection connection = DriverManager.getConnection(url, user, password);
            System.out.println(connection);
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    4. 方式四

    使用 Class.forName 自动完成注册驱动
    在加载 Driver 类时,会自动完成注册
    使用最多

        public void connect04() throws ClassNotFoundException, SQLException {
    
            //使用反射加载Driver类
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/qdu_01?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=false";
            String user = "root";
            String password = "root";
            Connection connection = DriverManager.getConnection(url, user, password);
            System.out.println(connection);
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    mysql驱动5.1.6之后就不需要再写 Class.forName(“com.mysql.jdbc.Driver”);

    5. 方式五

    使用配置文件,连接数据库更灵活
    最建议使用

    user=root
    password=root
    url=jdbc:mysql://localhost:3306/qdu_01?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=false
    driver=com.mysql.jdbc.Driver
    
    • 1
    • 2
    • 3
    • 4
        public void connect() throws IOException, SQLException {
    
            //通过Properties对象获取配置文件的信息
            Properties properties = new Properties();
            properties.load(new FileInputStream("src\\mysql.properties"));
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String driver = properties.getProperty("driver");
            String url = properties.getProperty("url");
            Connection connection = DriverManager.getConnection(url, user, password);
            System.out.println(connection);
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    4. ResultSet[结果集]

    1. 基本介绍

    1. 表示数据库结果集的数据表,通常通过执行查询数据库的语句生成。
    2. ResultSet 对象保持一个光标指向其当前的数据行。最初光标位于第一行之前
    3. next 方法将光标移动到下一行,并且由于在 ResultSet 对象中没有更多行时返回 false,因此可以在 while 循环中使用循环来遍历结果集

    在这里插入图片描述

    2. 应用实例

    package com.qdu.jdbc;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.sql.*;
    import java.util.Properties;
    
    /**
     * @author dell
     * @version 1.0
     */
    public class ResultSet_ {
    
        public static void main(String[] args) throws IOException, SQLException {
    
            Properties properties = new Properties();
            properties.load(new FileInputStream("src\\mysql.properties"));
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String driver = properties.getProperty("driver");
            String url = properties.getProperty("url");
            Connection connection = DriverManager.getConnection(url, user, password);
            Statement statement = connection.createStatement();
            String sql = "select id, name, sex, borndate from actor";
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()){//让光标向后移动,如果没有更多记录,返回false
                int id = resultSet.getInt(1);//获取该行的第一列数据
                String name = resultSet.getString(2);
                String sex = resultSet.getString(3);
                Date date = resultSet.getDate(4);
                System.out.println(id + " " + name + " " + sex + " " + date);
            }
            resultSet.close();
            statement.close();
            connection.close();
    
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    在这里插入图片描述

    5. Statement

    1. 基本介绍

    1. Statement 对象用于执行静态SQL语句并返回其生成的结果的对象
    2. 在连接建立后,需要对数据库进行访问,执行命名或是SQL语句,可以通过Statement【存在SQL注入】、PreparedStatement【预处理】、CallableStatement【存储过程】
    3. Statement对象执行SQL语句,存在SQL注入风险
    4. SQL注入是利用某些系统没有对用户输入的数据进行充分的检查,而在用户输入数据中注入非法的SQL语句段或命名,恶意攻击数据库。
    5. 要防范SQL注入,只要用PreparedStatement(从Statement扩展而来)取代Statement就可以了。

    2. 应用案例【注入】

    package com.qdu.jdbc.statement_;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.sql.*;
    import java.util.Properties;
    import java.util.Scanner;
    
    /**
     * @author dell
     * @version 1.0
     */
    public class Statement_ {
    
        public static void main(String[] args) throws IOException, SQLException {
    
            Scanner scanner = new Scanner(System.in);
            System.out.print("请输入管理员的名字:");
            //用户输入用户名和密码
            String admin_name = scanner.nextLine();//如果希望看到SQL注入,这里需要使用nextLine(),next():接收到空格或者'就是表示结束
            System.out.print("请输入管理员的密码:");
            String admin_pwd = scanner.nextLine();
    
            Properties properties = new Properties();
            properties.load(new FileInputStream("src\\mysql.properties"));
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String driver = properties.getProperty("driver");
            String url = properties.getProperty("url");
            Connection connection = DriverManager.getConnection(url, user, password);
            Statement statement = connection.createStatement();
            String sql = "select name, pwd from admin where name = '" + admin_name + "' and pwd = '" + admin_pwd + "'";
            ResultSet resultSet = statement.executeQuery(sql);
            if (resultSet.next()) {
                System.out.println("恭喜!登录成功!");
            }else {
                System.out.println("sorry!登录失败!");
            }
            resultSet.close();
            statement.close();
            connection.close();
    
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    在这里插入图片描述

    6. PreparedStatement

    1. 基本介绍

    1. PreparedStatement 执行的SQL语句中的参数用问号(?)来表示,调用PreparedStatement 对象的setXxx()防范来设置这些参数.setXxx()防范有两个参数,第一个参数是要设置的SQL语句中的参数的索引(从1开始),第二个是设置的SQL语句中的参数的值
    2. 调用 executeQuery(),返回 ResultSet 对象
    3. 调用 executeUpdate(),执行更新,包括增、删、修改,返回受影响的行数

    2. 预处理好处

    1. 不再使用 + 拼接 SQL 语句,减少语法错误
    2. 有效的解决了 SQL 注入问题
    3. 大大减少了编译次数,效率较高

    3. 应用案例

    可以控制SQL注入

    package com.qdu.jdbc.preparedstatement_;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.sql.*;
    import java.util.Properties;
    import java.util.Scanner;
    
    /**
     * @author dell
     * @version 1.0
     */
    public class PreparedStatement_ {
    
        public static void main(String[] args) throws IOException, SQLException {
    
            Scanner scanner = new Scanner(System.in);
            System.out.print("请输入管理员的名字:");
            //用户输入用户名和密码
            String admin_name = scanner.nextLine();//如果希望看到SQL注入,这里需要使用nextLine(),next():接收到空格或者'就是表示结束
            System.out.print("请输入管理员的密码:");
            String admin_pwd = scanner.nextLine();
    
            Properties properties = new Properties();
            properties.load(new FileInputStream("src\\mysql.properties"));
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String driver = properties.getProperty("driver");
            String url = properties.getProperty("url");
            Connection connection = DriverManager.getConnection(url, user, password);
            String sql = "select name, pwd from admin where name = ? and pwd = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, admin_name);
            preparedStatement.setString(2, admin_pwd);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                System.out.println("恭喜!登录成功!");
            }else {
                System.out.println("sorry!登录失败!");
            }
            resultSet.close();preparedStatement.close();
            connection.close();
    
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    在这里插入图片描述

    7. JDBC的相关API小结

    在这里插入图片描述
    在这里插入图片描述

    8. 封装JDBCUtils

    在jdbc操作中,获取连接和释放资源是经常使用到,可以将其封装JDBC连接的工具类JDBCUtils

    package com.qdu.jdbc.utils;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.sql.*;
    import java.util.Properties;
    
    /**
     * @author dell
     * @version 1.0
     * JDBC 的工具类,完成 mysql 的链接和关闭资源
     */
    public class JDBCUtils {
    
        //定义相关的属性(4个)
        private static String user;
        private static String password;
        private static String url;
        private static String driver;
    
        //在static代码块中初始化
        static {
    
            Properties properties = new Properties();
            try {
                properties.load(new FileInputStream("src\\mysql.properties"));
                //读取相关的属性值
                user = properties.getProperty("user");
                password = properties.getProperty("password");
                url = properties.getProperty("url");
                driver = properties.getProperty("driver");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
    
        }
    
        //链接数据库,返回connection
        public static Connection getConnection() {
    
            try {
                return DriverManager.getConnection(url, user, password);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
    
        }
    
        //关闭相关资源
        //Statement 是 PreparedStatement 的父类
        public static void close(ResultSet resultSet, Statement statement, Connection connection) {
    
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
    
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    9. 事务

    1. 基本介绍

    1. JDBC 程序中当一个 Connection 对象创建时,默认情况下是自动提交事务:每次执行一个 SQL 语句时,如果执行成功,就会向数据库自动提交,而不能回滚。
    2. JDBC 程序中为了让多个 SQL 语句执行成功,将多个 SQL 语句作为一个整体执行,需要使用事务。
    3. 调用 Connection 的 setAutoCommit(false) 可以取消自动提交事务
    4. 在所有的 SQL 语句都成功执行后,调用 commit(); 方法提交事务
    5. 在其中某个操作失败或出现异常时,调用 rollback(); 方法回滚事务

    2. 应用实例

    模拟经典的转账业务

    create table account(
    id int primary key auto_increment,
    name varchar(32) not null default '',
    balance double not null default 0) character set utf8;
    
    insert into account values(null, '马云', 3000);
    insert into account values(null, '马化腾', 10000);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    未使用事务,执行一条 SQL 语句就会提交一条 SQL 语句(默认)

        @Test
        public void noTransaction() {
            Connection connection = null;
            String sql1 = "update account set balance = balance - 100 where id = 1";
            String sql2 = "update account set balance = balance + 100 where id = 2";
            PreparedStatement preparedStatement = null;
    
            try {
                connection = JDBCUtils.getConnection();
                preparedStatement = connection.prepareStatement(sql1);
                preparedStatement.executeUpdate();
    
                int i = 1 / 0;//抛出一个异常,下面的代码将不会再执行
                preparedStatement = connection.prepareStatement(sql2);
                preparedStatement.executeUpdate();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                JDBCUtils.close(null, preparedStatement, connection);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述

    使用事务,当操作出现异常时,进行回滚

        @Test
        public void useTransaction() {
            Connection connection = null;
            String sql1 = "update account set balance = balance - 100 where id = 1";
            String sql2 = "update account set balance = balance + 100 where id = 2";
            PreparedStatement preparedStatement = null;
    
            try {
                connection = JDBCUtils.getConnection();
                //将connection设置为不自动提交
                connection.setAutoCommit(false);//相当于开启了事务
                preparedStatement = connection.prepareStatement(sql1);
                preparedStatement.executeUpdate();
    
                int i = 1 / 0;//抛出一个异常,下面的代码将不会再执行
                preparedStatement = connection.prepareStatement(sql2);
                preparedStatement.executeUpdate();
    
                //在这里提交事务
                connection.commit();
            } catch (SQLException e) {
                //我们可以在这里进行回滚,即撤销执行的SQL
                //默认回滚到事务开始的状态
                System.out.println("执行发生了异常,撤销执行的 SQL");
                try {
                    connection.rollback();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
                throw new RuntimeException(e);
            } finally {
                JDBCUtils.close(null, preparedStatement, connection);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    在这里插入图片描述

    10. 批处理

    1. 基本介绍

    1. 当需要成批插入或者更新记录时。可以采用Java的批量更新机制,这一机制允许多条语句次性提交给数据库批量处理。通常情况下比单独提交处理更有效率。
    2. JDBC的批量处理语句包括下面方法:
      addBatch():添加需要批量处理的SQL语句或参数
      executeBatch():执行批量处理语句;
      clearBatch():清空批处理包的语句
    3. JDBC连接MySQL时,如果要使用批处理功能,请再url中加参数?rewriteBatchedStatements=true
    4. 批处理往往和 PreparedStatement — 起搭配使用,可以既减少编译次数,又减少运行次数,效率大大提高

    2. 应用案例

    向 admin2 表中添加 5000 条数据

    create table admin2
    (id int primary key auto_increment,
    username varchar(32) not null,
    password varchar(32) not null);
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    经典方法

        @Test
        public void noBatch() throws SQLException {
    
            Connection connection = JDBCUtils.getConnection();
            String sql = "insert into admin2 values (null, ?, ?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            System.out.println("开始执行");
            long start = System.currentTimeMillis();
            for (int i = 0; i < 5000; i++) {
                preparedStatement.setString(1, "jack" + i);
                preparedStatement.setString(2, "666");
                preparedStatement.executeUpdate();
            }
            long end = System.currentTimeMillis();
            System.out.println("添加结束 " + (end - start));
            JDBCUtils.close(null, preparedStatement, connection);
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    批处理

        @Test
        public void batch() throws SQLException {
    
            Connection connection = JDBCUtils.getConnection();
            String sql = "insert into admin2 values (null, ?, ?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            System.out.println("开始执行");
            long start = System.currentTimeMillis();
            for (int i = 0; i < 5000; i++) {
                preparedStatement.setString(1, "jack" + i);
                preparedStatement.setString(2, "666");
                //将sql语句加入到批处理包中
                preparedStatement.addBatch();
                //当有1000条记录时,再批量执行
                if ((i + 1) % 1000 == 0) {//批量执行1000条sql
                    preparedStatement.executeBatch();
                    //清空一把
                    preparedStatement.clearBatch();
                }
            }
            long end = System.currentTimeMillis();
            System.out.println("添加结束 " + (end - start));
            JDBCUtils.close(null, preparedStatement, connection);
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    批处理时一定要在 mysql.properties 配置文件中的 url 中加上 rewriteBatchedStatements=true
    在这里插入图片描述

  • 相关阅读:
    管理类联考——逻辑——知识+记忆篇——综合推理——考点+记忆
    BigDecimal 类型的计算方法
    mysql8压缩包安装
    5种移动网站测试的好方法....
    将本地文件夹添加到 Git 仓库
    DGIOT国内首家轻量级物联网开源平台——MQTT接入实战教程
    MobileViT模型简介
    大三Web课程设计——悬崖上的波妞(4页) HTML+CSS(可以很好的应付老师的作业)
    JAVA学习实战(十二)分库分表学习
    Missing artifact org.yaml:snakeyaml:jar:1.29
  • 原文地址:https://blog.csdn.net/qq_52354698/article/details/126509719