企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] # 简介 从JDBC2.0开始增加了一些功能:可滚动的结果集,可以利用结果集执行增加,更新,删除,批处理操作 所谓的批处理就是一次性向数据库发出多条操作指令,一起执行.如果要想操作批处理, 主要还是在Statement和PreparedStatement接口上定义的 第一种方式:Statement.addBatch(sql) ~~~ statement接口定义方法 | |---增加批处理语句 |---public void addBatch(String sql) throws SQLException | |---执行批处理语句 |---public int[] executeBatch() throws SQLException |---返回的数据是包含了所有批处理语句的执行结果;(几个批处理就是几个元素,每个语句的执行结果 ) ~~~ 第二种方式:PreparedStatement.addBatch() ~~~ PreparedStatement接口定义的方法 | |---增加批处理语句 |---public void addBatch() throws SQLException ~~~ 在批处理操作中,由于JDBC具有自动事务提交,所以一旦中间的语句由错误,错误之前的语句正常执行,错误后的语句就不执行了 # Statement完成批处理 1. 使用Statement对象添加要批量执行的SQL语句,如下: ~~~ Statement.addBatch(sql1); Statement.addBatch(sql2); Statement.addBatch(sql3); ~~~ 2. 执行批处理SQL语句 ~~~ Statement.executeBatch(); ~~~ 3. 清除批处理命令 ~~~ Statement.clearBatch(); ~~~ ## 范例 1. 编写测试的SQL脚本创建表。 ~~~ create table testbatch ( id varchar(40) primary key, name varchar(40) ); ~~~ 2. 编写测试代码,如下所示: ~~~ public class Demo3 { /* create table testbatch ( id varchar(40) primary key, name varchar(40) ); */ // 实现批处理的第一种方式 @Test public void test1() throws SQLException { Connection conn = null; Statement st = null; ResultSet rs = null; try { conn = JdbcUtils.getConnection(); String sql1 = "insert into testbatch(id,name) values('1','李阿云')"; // 1.可以发送不同的sql语句 2.发送的sql语句没有预编译 String sql2 = "update testbatch set name='李阿昀' where id='1'"; st = conn.createStatement(); // Statement内部维护了一个List集合,addBatch()方法加入的sql语句都加入到该List集合内了。 st.addBatch(sql1); st.addBatch(sql2); st.executeBatch(); // 返回int[] [3,4]——即第一条sql语句执行影响数据库表几行,第二条sql语句执行影响数据库表几行,... st.clearBatch(); // 清除清除批处理命令 } finally { JdbcUtils.release(conn, st, rs); } } } ~~~ ## 优缺点 采用Statement.addBatch(sql)方式实现批处理: * 优点:可以向数据库发送多条不同的SQL语句。 * 缺点: * SQL语句没有预编译。 * 当向数据库发送多条语句相同,但仅参数不同的SQL语句时,需重复写上很多条SQL语句。 例如: ~~~ Insert into user(name,password) values(‘aa’,’111’); Insert into user(name,password) values(‘bb’,’222’); Insert into user(name,password) values(‘cc’,’333’); Insert into user(name,password) values(‘dd’,’444’); ~~~ # PreparedStatement完成批处理 ## 范例 ~~~ public class Demo3 { /* create table testbatch ( id varchar(40) primary key, name varchar(40) ); */ // 实现批处理的第二种方式 @Test public void test2() throws SQLException { long starttime = System.currentTimeMillis(); Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try { conn = JdbcUtils.getConnection(); String sql1 = "insert into testbatch(id,name) values(?,?)"; // 1.适合做批量插入和批量更新 2.发送的sql语句都预编译过,性能会好一点。实际开发里,此种方式用到的多一点。 st = conn.prepareStatement(sql1); /* st.setString(1, "1"); st.setString(2, "李阿云"); st.addBatch(); // 把PreparedStatement对象内部封装的sql语句加入到PreparedStatement对象内部的List集合中 st.setString(1, "2"); st.setString(2, "叶一"); st.addBatch(); // 此时PreparedStatement对象内部的List集合中有两条sql语句 */ // 批处理一千万条sql语句,会出现OutOfMemoryError:Java heap space——内存溢出 /* for (int i = 1; i <= 10000000; i++) { st.setString(1, i+""); st.setString(2, "叶一"+i); st.addBatch(); } st.executeBatch(); */ for (int i = 1; i <= 10000006; i++) { st.setString(1, i+""); st.setString(2, "叶一"+i); st.addBatch(); if (i%1000==0) { st.executeBatch(); st.clearBatch(); } st.executeBatch(); // 剩下不到1000条sql语句也需要执行 } } finally { JdbcUtils.release(conn, st, rs); } long endtime = System.currentTimeMillis(); System.out.println("共花了:"+(endtime-starttime)/1000+"秒"); // 向MySQL数据库批插入1000万条记录,大概需要3个多小时。而Oracle数据库测试大概需要380秒 } } ~~~ ## 优缺点 采用 PreparedStatement.addBatch() 实现批处理 * 优点:发送的是预编译后的 SQL 语句,执行效率高。 * 缺点:只能应用在 SQL 语句相同,但参数不同的批处理中。因此此种形式的批处理经常用于在同一个表中批量插入数据,或批量更新表的数据。