当前位置:网站首页><JDBC> 批量插入 的四种实现方式:你真的get到了吗?
<JDBC> 批量插入 的四种实现方式:你真的get到了吗?
2022-08-01 02:48:00 【努力的小鳴人】
本文收录与专栏:《JDBC》专栏
专栏目的是解释JDBC的关键点,与各位一路同行,会持续输出,欢迎免费订阅!!
一、批量插入
批量插入就是批量执行SQL语句
当需要成批插入或者更新记录时,可以采用Java的批量更新机制,这一机制允许多条语句一次性提交给数据库批量处理。通常情况下比单独提交处理更有效率
JDBC的批量处理语句包括下面三个方法:
●addBatch(String)
:添加需要批量处理的SQL语句或是参数
●executeBatch()
:执行批量处理语句
●clearBatch()
:清空缓存的数据
通常情况下我们会遇到两种批量执行SQL语句的情况:
- 多条SQL语句的批量处理
- 一个SQL语句的批量传参
二、举例
举例:向数据表中插入20000条数据
数据库中提供一个goods
表:创建如下:
CREATE TABLE goods(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20)
);
实现方式一
使用Statement
Connection conn = JDBCUtils.getConnection();
Statement st = conn.createStatement();
for(int i = 1;i <= 20000;i++){
String sql = "insert into goods(name) values('name_' + "+ i +")";
st.executeUpdate(sql);
}
实现方式二
使用PreparedStatement
long start = System.currentTimeMillis();
Connection conn = JDBCUtils.getConnection();
String sql = "insert into goods(name)values(?)";
PreparedStatement ps = conn.prepareStatement(sql);
for(int i = 1;i <= 20000;i++){
ps.setString(1, "name_" + i);
ps.executeUpdate();
}
long end = System.currentTimeMillis();
System.out.println("花费的时间为:" + (end - start));//82340
JDBCUtils.closeResource(conn, ps);
实现方式三
- 使用
addBatch() / executeBatch() / clearBatch()
- mysql服务器默认是关闭批处理的,我们需要通过一个参数,让mysql开启批处理的支持
?rewriteBatchedStatements=true 写在配置文件的url后面 - 使用更新的mysql 驱动:
mysql-connector-java-5.1.37-bin.jar
@Test
public void testInsert3() throws Exception{
long start = System.currentTimeMillis();
Connection conn = JDBCUtils.getConnection();
String sql = "insert into goods(name)values(?)";
PreparedStatement ps = conn.prepareStatement(sql);
for(int i = 1;i <= 1000000;i++){
ps.setString(1, "name_" + i);
//1.“攒”sql
ps.addBatch();
if(i % 500 == 0){
//2.执行
ps.executeBatch();
//3.清空
ps.clearBatch();
}
}
long end = System.currentTimeMillis();
System.out.println("花费的时间为:" + (end - start));//20000条:625 //1000000条:14733
JDBCUtils.closeResource(conn, ps);
}
实现方式四
在方式三的基础上操作
使用Connection 的 setAutoCommit(false) / commit()
@Test
public void testInsert4() throws Exception{
long start = System.currentTimeMillis();
Connection conn = JDBCUtils.getConnection();
//1.设置为不自动提交数据
conn.setAutoCommit(false);
String sql = "insert into goods(name)values(?)";
PreparedStatement ps = conn.prepareStatement(sql);
for(int i = 1;i <= 1000000;i++){
ps.setString(1, "name_" + i);
//1.“攒”sql
ps.addBatch();
if(i % 500 == 0){
//2.执行
ps.executeBatch();
//3.清空
ps.clearBatch();
}
}
//2.提交数据
conn.commit();
long end = System.currentTimeMillis();
System.out.println("花费的时间为:" + (end - start));//1000000条:4978
JDBCUtils.closeResource(conn, ps);
}
这四种实现方式总是忘,浅浅总结下,希望会有用~
边栏推荐
- Detailed explanation of TCP protocol
- opencv 缩小放大用哪种插值更好??
- Soft Exam Senior System Architect Series: Basic Knowledge of Information Systems
- Summary of JVM interview questions (continuously updated)
- 【 】 today in history: on July 31, "brains in vats" the birth of the participant;The father of wi-fi was born;USB 3.1 standard
- MYSQL Classic Interview Questions
- Four ways the Metaverse is changing the way humans work
- Guys, MySQL cdc source recycles replication slave and r in incremental process
- 初出茅庐的小李第114篇博客项目笔记之机智云智能浇花器实战(3)-基础Demo实现
- leetcode:1562. 查找大小为 M 的最新分组【模拟 + 端点记录 + 范围合并】
猜你喜欢
RTL8762DK uses DebugAnalyzer (four)
MYSQL logical architecture
树莓派 的 arm 版的 gcc 安装 和环境变量的配置
【搜索专题】看完必会的BFS解决最短路问题攻略
设备树——dtb格式到struct device node结构体的转换
每周小结(*67):为什么不敢发表观点
[Search topic] After reading the inevitable BFS solution to the shortest path problem
[uniCloud] Application and Improvement of Cloud Objects
Compiled on unbutu with wiringPi library and run on Raspberry Pi
[cellular automata] based on matlab interface aggregation cellular automata simulation [including Matlab source code 2004]
随机推荐
Chain programming, packages, access
Nmap 操作手册 - 完整版
带你体验一次类型编程实践
opencv 缩小放大用哪种插值更好??
test
HIRO: Hierarchical Reinforcement Learning 】 【 Data - Efficient Hierarchical Reinforcement Learning
Four ways the Metaverse is changing the way humans work
win10 固定本机IP
ARM 交叉编译
二舅
如何下载Keil包
MySQL modifies SQL statements to optimize performance
Inheritance Considerations
The fledgling Xiao Li's 113th blog project notes: Wisdom cloud smart flower watering device combat (2) - basic Demo implementation
IDEA 找不到或无法加载主类 或 Module “*“ must not contain source root “*“ The root already belongs to module “*“
Open source project site must-have & communication area function
The kernel of the decompression process steps
You need to know the TCP wave four times
pdb药物综合数据库
Take you to experience a type programming practice