Jdbc系列八:批量处理
2019-05-25 19:14
369 查看
当需要成批插入或者更新记录时。可以采用Java的批量更新机制,这一机制允许多条语句一次性提交给数据库批量处理。通常情况下比单独提交处理更有效率。
JDBC的批量处理语句包括下面两个方法:
addBatch(String):添加需要批量处理的SQL语句或是参数;
executeBatch():执行批量处理语句;
通常我们会遇到两种批量执行SQL语句的情况:
多条SQL语句的批量处理;
一个SQL语句的批量传参;
测试:
向 Oracle 的 customers 数据表中插入 10 万条记录。测试如何插入, 用时最短。
1、使用 Statement,用时 39567ms
@Test public void testBatchWithStatement(){ Connection connection = null; Statement statement = null; String sql = null; try { connection = JDBCTools.getConnection(); JDBCTools.beginTx(connection); statement = connection.createStatement(); long begin = System.currentTimeMillis(); for(int i = 0; i < 100000; i++){ sql = "INSERT INTO customers VALUES(" + (i + 1) + ", 'name_" + i + "', '29-6月 -13')"; statement.addBatch(sql); } long end = System.currentTimeMillis(); System.out.println("Time: " + (end - begin)); //39567 JDBCTools.commit(connection); } catch (Exception e) { e.printStackTrace(); JDBCTools.rollback(connection); } finally{ JDBCTools.releaseDB(null, statement, connection); } }
2、使用 PreparedStatement。用时 9819ms
@Test public void testBatchWithPreparedStatement(){ Connection connection = null; PreparedStatement preparedStatement = null; String sql = null; try { connection = JDBCTools.getConnection(); JDBCTools.beginTx(connection); sql = "INSERT INTO customers VALUES(?,?,?)"; preparedStatement = connection.prepareStatement(sql); Date date = new Date(new java.util.Date().getTime()); long begin = System.currentTimeMillis(); for(int i = 0; i < 100000; i++){ preparedStatement.setInt(1, i + 1); preparedStatement.setString(2, "name_" + i); preparedStatement.setDate(3, date); preparedStatement.executeUpdate(); } long end = System.currentTimeMillis(); System.out.println("Time: " + (end - begin)); //9819 JDBCTools.commit(connection); } catch (Exception e) { e.printStackTrace(); JDBCTools.rollback(connection); } finally{ JDBCTools.releaseDB(null, preparedStatement, connection); } }
3、使用Batch批量操作,用时 569ms
@Test public void testBatch(){ Connection connection = null; PreparedStatement preparedStatement = null; String sql = null; try { connection = JDBCTools.getConnection(); JDBCTools.beginTx(connection); sql = "INSERT INTO customers VALUES(?,?,?)"; preparedStatement = connection.prepareStatement(sql); Date date = new Date(new java.util.Date().getTime()); long begin = System.currentTimeMillis(); for(int i = 0; i < 100000; i++){ preparedStatement.setInt(1, i + 1); preparedStatement.setString(2, "name_" + i); preparedStatement.setDate(3, date); //"积攒" SQL preparedStatement.addBatch(); //当 "积攒" 到一定程度, 就统一的执行一次. 并且清空先前 "积攒" 的 SQL if((i + 1) % 300 == 0){ preparedStatement.executeBatch(); preparedStatement.clearBatch(); } } //若总条数不是批量数值的整数倍, 则还需要再额外的执行一次. if(100000 % 300 != 0){ preparedStatement.executeBatch(); preparedStatement.clearBatch(); } long end = System.currentTimeMillis(); System.out.println("Time: " + (end - begin)); //569 JDBCTools.commit(connection); } catch (Exception e) { e.printStackTrace(); JDBCTools.rollback(connection); } finally{ JDBCTools.releaseDB(null, preparedStatement, connection); } }
相关文章推荐
- JDBC addbatch批量处理数据时有最大值限制
- JDBC系列:(4)使用CablleStatement调用存储过程
- JDBC addbatch批量处理数据时有最大值限制
- Java Web系列:JDBC 基础
- JSP初级系列--Eclipse3.21+SQl 2005 JDBC数据库连接配置方法
- Java 数据库系列教程--线程池与普通jdbc的 比较
- 1 开发一个注重性能的JDBC应用程序不是一件容易的事. 当你的代码运行很慢的时候JDBC驱动程序并不会抛出异常告诉你。 本系列的性能提示将为改善JDBC应用程序的性能介绍一些基本的指导原则,这其中的原则已经被许多现有的JDBC应用程序编译运行并验证过。 这些指导原则包括: 正确的使用数据库MetaData方法 只获取需要的数据 选用最佳性能的功能 管理连
- JDBC系列教程(六)---可调用语句
- Java Jdbc减少与Oracle之间交互提升批量处理性能,到底该如何优化才好?
- Java系列之JDBC和ODBC之间的区别与联系
- JDBC批量处理
- JDBC批量处理与结果集
- Java基础系列15:JDBC中使用元数据分析数据库
- 老调重弹:JDBC系列之<驱动加载原理全面解析)
- Apache POI系列教程2-2:初学JDBC__第七节(DatabaseMetaData和ParameterMetaData)
- JDBC的批量处理数据
- 关于使用原生JDBC批量处理数据的问题
- 利用JDBC批量处理语句
- JDBC系列教程(二)---驱动设置
- 大数据系列之数据仓库Hive命令使用及JDBC连接