JDBC(10)—批处理
来源:互联网 发布:淘宝捉猫猫是干嘛的 编辑:程序博客网 时间:2024/06/03 22:41
- 批量处理JDBC语句,提高处理速度。
- 当需要成批的的插入或更新记录时可以采用java的批量更新机制,这一机制允许多条语句一次性提交给数据库批量处理,通常情况下比单独提交处理更有效率。
- 批量处理的方法:
- ——addBatch(String):添加需要批量处理的SQL语句或是参数。
- ——executeBatch():执行批量处理语句。
*通常我们会遇到两种批量执行SQL语句的情况: - ——多条SQL语句批量处理
- ——一条SQL语句的批量传参
- 以下三个方法使用了三种批量处理方式以及使用时间分别为:
*Statement使用时间:18271毫秒
*PreparedStatement使用时间:13808毫秒
*JDBC批处理:2046毫秒 - 实例
public class Volume_11 { /** * 1.向Sql Server中表customers插入100000条记录。 * 使用statement,花费时间:18271毫秒 */ @Test public void testBatchWithStatement(){ Connection conn = null; Statement statement = null; String sql = null; try { conn = TestTools.getConnection(); //事务开始 TestTools.beginTx(conn); statement = conn.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.executeUpdate(sql); } //计时结束 long end = System.currentTimeMillis(); System.out.println("时间:"+(end - begin)); //事务提交 TestTools.commit(conn); } catch (Exception e) { e.printStackTrace(); //事务回滚 TestTools.rollback(conn); }finally{ TestTools.release(statement, conn); } } /** * 2.向Sql Server中表customers插入100000条记录。 * 使用Preparedstatement,花费时间:13808毫秒 */ @Test public void testBatchWithPreparedstatament(){ Connection conn = null; PreparedStatement preparedstatement = null; String sql = null; try { conn = TestTools.getConnection(); //事务开始 TestTools.beginTx(conn); sql = "INSERT INTO customers VALUES(?,?,?)"; preparedstatement = conn.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("时间:"+(end - begin)); //事务提交 TestTools.commit(conn); } catch (Exception e) { e.printStackTrace(); //事务回滚 TestTools.rollback(conn); }finally{ TestTools.release(preparedstatement, conn); } } /** * 3.向Sql Server中表customers插入100000条记录。 * 使用JDBC批处理,花费时间:2046毫秒 */ @Test public void testBatch(){ Connection conn = null; PreparedStatement preparedstatement = null; String sql = null; try { conn = TestTools.getConnection(); //事务开始 TestTools.beginTx(conn); sql = "INSERT INTO customers VALUES(?,?,?)"; preparedstatement = conn.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); //"积攒"300条记录之后一块提交到数据库 preparedstatement.addBatch(); if((i + 1) % 300 == 0){ preparedstatement.executeBatch();//执行提交 preparedstatement.clearBatch();//清除积攒的记录 } } //若总条数不是积攒数的整数倍,则需要额外的在执行一次,比如总条数400,积攒数300,则执行一次之后还有100 //条记录,100%300不等于0无法提交,所以需要再判断一下是否需要再提交一次。 if(100000 % 300 != 0){ preparedstatement.executeBatch();//执行提交 preparedstatement.clearBatch();//清除积攒的记录 } //计时结束 long end = System.currentTimeMillis(); System.out.println("时间:"+(end - begin)); //事务提交 TestTools.commit(conn); } catch (Exception e) { e.printStackTrace(); //事务回滚 TestTools.rollback(conn); }finally{ TestTools.release(preparedstatement, conn); } }}
阅读全文
0 0
- JDBC(10)—批处理
- JDBC---PreparedStatement 批处理(二)
- JSP基础(十六)——使用JDBC进行批处理
- JDBC批处理
- jdbc批处理
- JDBC批处理
- JDBC批处理
- jdbc批处理
- JDBC批处理
- JDBC批处理
- JDBC批处理
- jdbc 批处理
- JDBC批处理
- JDBC 批处理
- JDBC批处理
- JDBC批处理
- jdbc 批处理
- JDBC批处理
- POJ1064 -- Cable master(二分)
- 【HDU1862】 EXCEL排序 (sort)
- Spring框架的设计理念与设计模式分析
- 用户 'IIS APPPOOL\IdealTest' 登录失败解决方案
- Hibernate复习(一)
- JDBC(10)—批处理
- js中call、apply、bind函数
- 图像处理中傅里叶变换以及频率域图像增强详解
- void 0 的理解
- 拒绝了对对象 'user' (数据库 '****',架构 'dbo')的 SELECT 权限
- netease YunXin
- Angular入门之环境搭建(VS code)
- Python decorator装饰器
- Radar Installation POJ