JDBC-Batch 批量执行
JDBC 批处理 SQL 语句
首先在 jdbc 的 url 中加上 rewriteBatchedStatements=true,只有开启了这个 Mysql 才会执行批处理,否则还是一条一条执行
Statement 不使用 Batch
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import java.io.InputStream;
import java.sql.*;
import java.util.Date;
import java.util.Properties; public class BatchTest { private Connection connection;
private PreparedStatement preparedStatement;
private Statement statement; @BeforeEach
public void start() throws Exception {
Properties properties = new Properties();
InputStream in = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
properties.load(in); String driver = properties.getProperty("driver");
String jdbcUrl = properties.getProperty("jdbcUrl");
String user = properties.getProperty("user");
String password = properties.getProperty("password"); Class.forName(driver); connection = DriverManager.getConnection(jdbcUrl, user, password);
} @AfterEach
public void end() throws Exception {
if (statement != null) {
statement.close();
}
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
} @Test
public void testStatement() {
String sql = null;
try {
connection.setAutoCommit(false);
statement = connection.createStatement();
long begin = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
sql = "INSERT INTO batch_test (name) VALUES(" + i + ")";
statement.execute(sql);
}
long end = System.currentTimeMillis();
System.out.println("Time: " + (end - begin));
connection.commit();
} catch (Exception e) {
e.printStackTrace();
try {
connection.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
}

Statement 使用 Batch
@Test
public void testBatchWithStatement() {
String sql = null;
try {
connection.setAutoCommit(false);
statement = connection.createStatement();
long begin = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
sql = "INSERT INTO batch_test (name) VALUES('" + i + "')";
statement.addBatch(sql);
}
statement.executeBatch();
long end = System.currentTimeMillis();
System.out.println("Time: " + (end - begin));
connection.commit();
} catch (Exception e) {
e.printStackTrace();
try {
connection.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}

PreparedStatement 不使用 Batch
@Test
public void testPreparedStatement() {
String sql = "INSERT INTO batch_test (name) VALUES(?)";
try {
connection.setAutoCommit(false);
preparedStatement = connection.prepareStatement(sql);
long begin = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
preparedStatement.setString(1, "name_" + i);
preparedStatement.executeUpdate();
}
long end = System.currentTimeMillis();
System.out.println("Time: " + (end - begin));
connection.commit();
} catch (Exception e) {
e.printStackTrace();
try {
connection.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}

PreparedStatement 使用 Batch
@Test
public void testBatchWithPreparedStatement() {
String sql = "INSERT INTO batch_test (name) VALUES(?)";
try {
connection.setAutoCommit(false);
preparedStatement = connection.prepareStatement(sql);
long begin = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
preparedStatement.setString(1, "name_" + i);
preparedStatement.addBatch();
// 添加SQL到一定数量就统一的执行一次,清空之前添加的 SQL
if ((i + 1) % 300 == 0) {
preparedStatement.executeBatch();
preparedStatement.clearBatch();
}
}
// 总条数不是批量数值的整数倍需要再额外的执行一次
if (1000 % 300 != 0) {
preparedStatement.executeBatch();
preparedStatement.clearBatch();
}
long end = System.currentTimeMillis();
System.out.println("Time: " + (end - begin));
connection.commit();
} catch (Exception e) {
e.printStackTrace();
try {
connection.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}

https://blog.csdn.net/zhangyadick18/article/details/50294265
https://www.jianshu.com/p/04d3d235cb9f
JDBC-Batch 批量执行的更多相关文章
- JDBC batch批量Statement executeBatch 详细解释
JDBC提供了数据库batch处理的能力,在数据大批量操作(新增.删除等)的情况下能够大幅度提升系统的性能.我曾经接触的一个项目,在没有採用batch处理时,删除5万条数据大概要半个小时左右,后来对系 ...
- JDBC 复习4 批量执行SQL
1使用jdbc进行批量执行SQL在实际的项目开发中,有时候需要向数据库发送一批SQL语句执行,这时应避免向数据库一条条的发送执行,而应采用JDBC的批处理机制,以提升执行效率. package dbe ...
- JDBC批量执行executeBatch
JDBC事务 在数据库中,所谓事务是指一组逻辑操作单元,使数据从一种状态变换到另一种状态.为确保数据库中数据的一致性,数据的操纵应当是离散的成组的逻辑单元:当它全部完成时,数据的一致性可以保持,而当这 ...
- jdbc批量执行SQL insert 操作
package com.file; import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayLi ...
- JDBC连接MYSQL,批量执行SQL语句或在执行一个SQL语句之前执行一个SQL语句
conn = MysqlJdbcUtils.getConnection(); Statement ps=conn.createStatement(); ps.addBatch("trunca ...
- JDBC的批量批量插入
本文部分转载于:http://blog.itpub.net/29254281/viewspace-1151785/ http://www.cnblogs.com/chenjianjx/archive/ ...
- JDBC之批量处理
JDBC之批量处理 一.批量处理JDBC语句提高处理速度 当需要成批插入或者更新记录时.可以采用Java的批量更新机制,这一机制允许多条语句一次性提交给数据库批量处理.通常情况下比单独提交处理更有效率 ...
- MyBatis 通过 BATCH 批量提交
本文由 简悦 SimpRead 转码, 原文地址 https://www.jb51.net/article/153382.htm 很多人在用 MyBatis 或者 通用 Mapper 时,经常会问有没 ...
- Redis大幅性能提升之Batch批量读写
Redis大幅性能提升之Batch批量读写 提示:本文针对的是StackExchange.Redis 一.问题呈现 前段时间在开发的时候,遇到了redis批量读的问题,由于在StackExchange ...
- Could not execute JDBC batch update; SQL [delete from role where roleId=?]; constraint [null]; neste
今天在写多个删除功能的时候出现了这么一个错误:意思是删除操作的时候,没有找到对应的外键. Cannot delete or update a parent row: a foreign key con ...
随机推荐
- Codeforces543 B. Destroying Roads
传送门:>Here< 题意:给出一张无向图(边权为1),并给出两对起点和终点以及距离:s1,t1,l1; s2,t2,l2; 要求删除尽量多的边,使得dis(s1,t1)<=l1, ...
- 【XSY2166】Hope 分治 FFT
题目描述 对于一个\(1\)到\(n\)的排列\(a_1,a_2,a_3,\ldots,a_n\),我们定义这个排列的\(P\)值和\(Q\)值: 对于每个\(a_i\),如果存在一个最小的\(j\) ...
- qml(Qt Quick)做界面
qml(Qt Quick)做界面 来源 https://www.zhihu.com/question/24880681/answer/29324824 本人是Qt初学者,正在写一个会计小软件(Lin ...
- Java 类设计技巧
摘自<Java核心技术>卷I:基础知识 p140 第4章对象与类 - 类设计技巧 1)一定将数据设计为私有. 最重要的是:绝对不要破坏封装性.有时候,需要编写一个访问器方法或更改器方法,但 ...
- FLAG区
以下是一些flag(倒了我也不会怎么样): 更博客(对不起 您呼叫的flag是空号 请稍后再拨) CTS/APIO2019 Cu+ NOI2019 Ag+
- [2017-7-28]Android Learning Day7
View动画效果 透明动画效果 旋转动画效果 移动动画效果 缩放动画效果 混合动画效果 1.透明动画效果(AlphaAnimation) 有两种方法 第一种在活动中设置,不需要xml文件 public ...
- [2017-7-27]Android Learning Day5
总结篇! 吭哧吭哧了三天,最近不断研究<第一行代码:第二版>170多页的那个新闻实践项目,虽然也没有用到数据库和一些Web爬虫的知识,新闻数据都是随机生成的字符串...... 但还是很开心 ...
- 单片机的编程语言和开发环境 LET′S TRY“嵌入式编程”: 3 of 6
单片机的编程语言和开发环境 LET′S TRY“嵌入式编程”: 3 of 6 本连载讲解作为嵌入式系统开发技术人员所必需具备的基础知识.这些基础知识是硬件和软件技术人员都应该掌握的共通技术知识. 在“ ...
- 20165223 《JAVA程序设计》第六周学习总结
教材学习内容总结 第八章-常用实用类-要点 基础:String类 重点:StringTokenizer类,Scanner类 难点:Class类与Console类,Pattern类与Match类 其他特 ...
- Eureka
Consul vs. Eureka Eureka is a service discovery tool. The architecture is primarily client/server, w ...