|NO.Z.00107|——————————|BigDataEnd|——|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
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
分类:
bdv005-mysql
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通