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 ...
随机推荐
- 51Nod1824 染色游戏 【Lucas定理】【FMT】【位运算】
我的FMT是在VFleaKing的论文中学到的.51Nod的评测机好恶心. 题目分析: 题目很明显是要你求一个类似卷积的式子.但是我们可以注意到前面具有组合数,如果拆成阶乘会很大,在模意义下你无法判断 ...
- ionic更改端口号
—— 重新指定端口号为8888 serve [options] ............................... 启动本地服务器进行开发测试 dev/testing [--console ...
- MySQL 练习题 附答案,未完
综合练习题 表结构 整合一下方便查看 teacher student course scors 练习题 1.自行创建测试数据 create table student( sid int prima ...
- Qt Creator 中文编译失败 怎么办
在Qt Creator 中c++源码有中文字符,结果不能编译成功. 代码 QMessageBox::warning(this, "警告","用户名密码错误",Q ...
- 搜索引擎(Elasticsearch搜索详解)
学完本课题,你应达成如下目标: 掌握ES搜索API的规则.用法. 掌握各种查询用法 搜索API 搜索API 端点地址 GET /twitter/_search?q=user:kimchy GET /t ...
- luogu1196 银河英雄传说 (并查集)
并查集,不仅记fa,还记与fa的距离,还记根对应的尾节点 路径压缩的时候更新那个距离就行了 #include<bits/stdc++.h> #define pa pair<int,i ...
- Building Microservices with Spring Boot and Apache Thrift. Part 2. Swifty services
http://bsideup.blogspot.com/2015/04/spring-boot-thrift-part2.html In previous article I showed y ...
- codeforces Hello 2019(未写完)
A. Gennady and a Card Game a题惯例签到题 题意:给你一张牌,再给你5张牌,判断能不能出一次牌... 所以只要检查第二行中的某个卡是否与第一行中的卡具有共同字符 有就输出YE ...
- P3373 线段树模板
好,这是一个线段树模板. #include <cstdio> using namespace std; ; long long int sum[N],tag1[N],tag2[N],mo; ...
- saltstack常用命令
Salt通过公钥加密和认证minions.想要让minion从master端接受命令,minions的密钥需要被master接受 salt-key -L #列出master上的密钥; salt-key ...