CC00107.bigdatajava——|Java&MySQL.数据库连接池和DBUtils.V16|——|MySQL.v17|批处理插入10000条数据|

一、什么是批处理
### --- 批处理(batch) 操作数据库

——>        批处理指的是一次操作中执行多条SQL语句,批处理相比于一次一次执行效率会提高很多。
——>        当向数据库中添加大量的数据时,需要用到批处理。
~~~     # 举例: 送货员的工作:

——>        未使用批处理的时候,送货员每次只能运送 一件货物给商家;
——>        使用批处理,则是送货员将所有要运送的货物, 都用车带到发放处派给客户。
### --- 实现批处理

——>        Statement和PreparedStatement都支持批处理操作,
——>        这里我们介绍一下PreparedStatement的批处理方式:
二、要用到的方法
方法说明
void addBatch()
将给定的 SQL 命令添加到此 Statement 对象的当前命令列表中。
通过调用方法 executeBatch 可以批量执行此列表中的命令。
int[] executeBatch()
每次提交一批命令到数据库中执行,如果所有的命令都成功执行了,
那么返回一个数组,这个数组是说明每条命令所影响的行数
### --- mysql 批处理是默认关闭的,所以需要加一个参数才打开mysql 数据库批处理,在url中添加

rewriteBatchedStatements=true
例如: url=jdbc:mysql://127.0.0.1:3306/db5?characterEncoding=UTF-8&rewriteBatchedStatements=true
### --- 创建一张表

CREATE TABLE testBatch (
    id INT PRIMARY KEY AUTO_INCREMENT,
    uname VARCHAR(50)
)
三、测试向表中插入 1万条数据
public class TestBatch {
    //使用批处理,向表中添加 1万条数据
    public static void main(String[] args) {
        try {

            //1.获取连接
            Connection con = DruidUtils.getConnection();

            //2.获取预处理对象
            String sql ="insert into testBatch(uname) values(?)";
            PreparedStatement ps = con.prepareStatement(sql);

            //3.创建 for循环 来设置占位符参数
            for (int i = 0; i < 10000 ; i++) {
                ps.setString(1,"小强"+i);

                //将SQL添加到批处理 列表
                ps.addBatch();
            }

            //添加时间戳 测试执行效率
            long start = System.currentTimeMillis();

            //统一 批量执行
            ps.executeBatch();
            long end = System.currentTimeMillis();
             System.out.println("插入10000条数据使用: " +(end - start) +" 毫秒!");
        } catch (SQLException e) {
             e.printStackTrace();
        }
    }
}
四、sql语句
package com.yanqi.testbatch;

        import com.yanqi.utils.DruidUtils;

        import java.sql.Connection;
        import java.sql.PreparedStatement;
        import java.sql.SQLException;

public class BatchInsert {

    //使用批处理 向表中添加 10000条数据
    public static void main(String[] args) throws SQLException {

        //1.获取连接
        Connection connection = DruidUtils.getConnection();

        //2.获取预处理对象
        PreparedStatement ps = connection.prepareStatement("insert into testBatch(uname) values(?)");

        //3.执行批量插入操作
        for (int i = 0; i < 10000 ; i++){
            ps.setString(1,"小强" + i);

            //将SQL添加到批处理列表
            ps.addBatch();
        }
        //添加时间戳 测试执行效率
        long start = System.currentTimeMillis();

        //4.统一执行 批量插入操作
        ps.executeBatch();

        long end = System.currentTimeMillis();

        System.out.println("插入10000条数据需要使用: " + (end - start) + " 毫秒!");
        //5.关闭连接
        DruidUtils.close(connection,ps);
    }

}
五、打印输出
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=51623:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath "E:\NO.Z.10000——javaproject\NO.H.00002.mysql\mysql\out\production\mysql.jdbc_task06;F:\NO.Z.02000——soft——IT.实验\20210801——Hadoop\20210801.2——Hadoop——第二阶段——预科班\预科班第二阶段模块一\05 任务五 (JDBC)\03_软件\myJar\dom4j-1.6.1.jar;F:\NO.Z.02000——soft——IT.实验\20210801——Hadoop\20210801.2——Hadoop——第二阶段——预科班\预科班第二阶段模块一\05 任务五 (JDBC)\03_软件\myJar\druid-1.0.9.jar;F:\NO.Z.02000——soft——IT.实验\20210801——Hadoop\20210801.2——Hadoop——第二阶段——预科班\预科班第二阶段模块一\05 任务五 (JDBC)\03_软件\myJar\c3p0-0.9.5.2.jar;F:\NO.Z.02000——soft——IT.实验\20210801——Hadoop\20210801.2——Hadoop——第二阶段——预科班\预科班第二阶段模块一\05 任务五 (JDBC)\03_软件\myJar\commons-dbcp-1.4.jar;F:\NO.Z.02000——soft——IT.实验\20210801——Hadoop\20210801.2——Hadoop——第二阶段——预科班\预科班第二阶段模块一\05 任务五 (JDBC)\03_软件\myJar\jaxen-1.1-beta-6.jar;F:\NO.Z.02000——soft——IT.实验\20210801——Hadoop\20210801.2——Hadoop——第二阶段——预科班\预科班第二阶段模块一\05 任务五 (JDBC)\03_软件\myJar\commons-pool-1.5.6.jar;F:\NO.Z.02000——soft——IT.实验\20210801——Hadoop\20210801.2——Hadoop——第二阶段——预科班\预科班第二阶段模块一\05 任务五 (JDBC)\03_软件\myJar\commons-dbutils-1.6.jar;F:\NO.Z.02000——soft——IT.实验\20210801——Hadoop\20210801.2——Hadoop——第二阶段——预科班\预科班第二阶段模块一\05 任务五 (JDBC)\03_软件\myJar\mchange-commons-java-0.2.12.jar;F:\NO.Z.02000——soft——IT.实验\20210801——Hadoop\20210801.2——Hadoop——第二阶段——预科班\预科班第二阶段模块一\05 任务五 (JDBC)\03_软件\myJar\mysql-connector-java-5.1.37-bin.jar;C:\Users\Administrator\.m2\repository\org\testng\testng\6.14.3\testng-6.14.3.jar;C:\Users\Administrator\.m2\repository\com\beust\jcommander\1.72\jcommander-1.72.jar;C:\Users\Administrator\.m2\repository\org\apache-extras\beanshell\bsh\2.0b6\bsh-2.0b6.jar;C:\Users\Administrator\.m2\repository\org\junit\jupiter\junit-jupiter\5.4.2\junit-jupiter-5.4.2.jar;C:\Users\Administrator\.m2\repository\org\junit\jupiter\junit-jupiter-api\5.4.2\junit-jupiter-api-5.4.2.jar;C:\Users\Administrator\.m2\repository\org\apiguardian\apiguardian-api\1.0.0\apiguardian-api-1.0.0.jar;C:\Users\Administrator\.m2\repository\org\opentest4j\opentest4j\1.1.1\opentest4j-1.1.1.jar;C:\Users\Administrator\.m2\repository\org\junit\platform\junit-platform-commons\1.4.2\junit-platform-commons-1.4.2.jar;C:\Users\Administrator\.m2\repository\org\junit\jupiter\junit-jupiter-params\5.4.2\junit-jupiter-params-5.4.2.jar;C:\Users\Administrator\.m2\repository\org\junit\jupiter\junit-jupiter-engine\5.4.2\junit-jupiter-engine-5.4.2.jar;C:\Users\Administrator\.m2\repository\org\junit\platform\junit-platform-engine\1.4.2\junit-platform-engine-1.4.2.jar;C:\Users\Administrator\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\Administrator\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.yanqi.testbatch.BatchInsert
8月 05, 2021 10:42:26 下午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
插入10000条数据需要使用: 3205 毫秒!                                            //不准确

Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yanqi_vip

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值