JDBC的批处理操作

爱谁谁
发布: 2025-07-25 08:14:41
原创
379人浏览过

在jdbc中,批处理操作允许将多条sql语句一起执行,从而提高数据库操作的效率。以下是关于批处理的详细说明和示例代码:

1.1.1 什么是批处理

在之前的JDBC操作中,我们通常是一条SQL语句执行一次。现在,通过批处理,我们可以将多条SQL语句组合在一起,一次性执行。

1.1.2 批处理的基本使用

以下是批处理的基本使用示例:

package com.xdr630.jdbc.demo6;
<p>import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import org.junit.Test;
import com.xdr630.jdbc.utils.JDBCUtils;</p><p>/**</p><ul><li><p>批处理操作示例</p></li><li><p>@author xdr
*/
public class JDBCDemo6 {</p><p>@Test
/**</p><ul><li>批处理基本操作
*/
public void demo1() {
Connection conn = null;
Statement stmt = null;
try {
// 获得连接
conn = JDBCUtils.getConnection();
// 创建执行批处理对象
stmt = conn.createStatement();
// 编写一批SQL语句
String sql1 = "create database test1";
String sql2 = "use test1";
String sql3 = "create table user(id int primary key auto_increment, name varchar(20))";
String sql4 = "insert into user values (null, 'aaa')";
String sql5 = "insert into user values (null, 'bbb')";
String sql6 = "insert into user values (null, 'ccc')";
String sql7 = "update user set name = 'mmm' where id = 2";
String sql8 = "delete from user where id = 1";
// 添加到批处理
stmt.addBatch(sql1);
stmt.addBatch(sql2);
stmt.addBatch(sql3);
stmt.addBatch(sql4);
stmt.addBatch(sql5);
stmt.addBatch(sql6);
stmt.addBatch(sql7);
stmt.addBatch(sql8);
// 执行批处理
stmt.executeBatch();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.release(stmt, conn);
}
}
}
登录后复制

执行后的结果:

JDBC的批处理操作

微软爱写作
微软爱写作

微软出品的免费英文写作/辅助/批改/评分工具

微软爱写作 17
查看详情 微软爱写作

1.1.3 批量插入(使用PreparedStatement)

以下是使用PreparedStatement进行批量插入的示例:

@Test
/**</li></ul></li><li><p>批量插入记录</p></li><li></li><li><p>默认情况下MySQL批处理没有开启的,需要在url后面拼接一个参数即可。
*/
public void demo2() {
// 记录开始时间
long begin = System.currentTimeMillis();
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 获得连接
conn = JDBCUtils.getConnection();
// 编写SQL语句
String sql = "insert into user values (null, ?)";
// 预编译SQL
pstmt = conn.prepareStatement(sql);
for (int i = 1; i <= 10000; i++) {
pstmt.setString(1, "name" + i);
pstmt.addBatch();
if (i % 1000 == 0) {
pstmt.executeBatch();
pstmt.clearBatch();
}
}
// 执行剩余的批处理
pstmt.executeBatch();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.release(pstmt, conn);
}
// 记录结束时间
long end = System.currentTimeMillis();
System.out.println("批量插入10000条记录耗时:" + (end - begin) + "毫秒");
}
登录后复制

为了启用MySQL的批处理,需要修改db.properties配置文件:

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///test1?rewriteBatchedStatements=true
username=root
password=1234
登录后复制

执行完成后就会插入一万条记录:

JDBC的批处理操作

以上就是JDBC的批处理操作的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号