JDBC addbatch批量处理数据时有最大值限制

在用jdbc向数据灌入数据时,发现120000的数据每次只能灌入50000多条,其他的就没有了。 
在oracle 9i(windows 2003),oracle 10g(RHEL 4)上试验证有相同的结果。 

使用定量灌入的办法,每5W条定义为一个事务,进行提交,将120000数据循环灌入,成功。 
对于批量的update,delete操作两样有5W条左右的记录数限制。 
结论:jdbc批量数据处理的每个批次是有数量的限制的。 
我在本地环境中测试的限制量为每批54464条,其他配置的机器没有试过。 

以下是插入数据时的代码: 

[java]  view plain copy
  1. ConnDB cd = new ConnDB();    
  2.         Connection ct = null;    
  3.         PreparedStatement pst = null;    
  4.         ResultSet rs = null;    
  5.     
  6.         ct = cd.getConn();    
  7.     
  8.         String insertSql = "insert into mytable (name,age) values(?,?)";    
  9.     
  10.         System.out.println(insertSql);    
  11.         try {       
  12.             ct.setAutoCommit(false); // 开始事务    
  13.             pst = ct.prepareStatement(insertSql);       
  14.             for (int i = 0; i < 120000; i++) {    
  15.                 
  16.                 pst.setString(1"name" + i);    
  17.                 pst.setInt(2, i);    
  18.                 pst.addBatch();    
  19.                     
  20.                 System.out.println(i);    
  21.                 //分段提交    
  22.                 if((i%50000==0&& i!=0)||i== (120000 -1)){    
  23.                     pst.executeBatch();    
  24.                     ct.commit();    
  25.                     ct.setAutoCommit(false);// 开始事务    
  26.                     pst = ct.prepareStatement(insertSql);    
  27.                     System.out.println("------------>50000");    
  28.                 }    
  29.                     
  30.             }    
  31.             ct.commit();// 提交事务    
  32.     
  33.                 
  34.     
  35.     
  36.         } catch (SQLException e) {    
  37.     
  38.             try {    
  39.                 ct.rollback();    
  40.             } catch (SQLException e1) {    
  41.     
  42.                 e1.printStackTrace();    
  43.             }    
  44.             e.printStackTrace();    
  45.         } catch (RuntimeException e) {    
  46.             try {    
  47.                 ct.rollback();    
  48.             } catch (SQLException e1) {    
  49.     
  50.                 e1.printStackTrace();    
  51.             }    
  52.     
  53.         } finally {    
  54.           cd.close(ct, pst, rs);    
  55.         }    
  56.             
  57.     }    

以下是删除数据时的代码: 

[java]  view plain copy
  1. ConnDB cd = new ConnDB();    
  2. Connection ct = null;    
  3. PreparedStatement pst = null;    
  4. ResultSet rs = null;    
  5.     
  6. ct = cd.getConn();    
  7.     
  8. String deleteSql = "delete from mytable t where t.name=?";    
  9.     
  10. System.out.println(deleteSql);    
  11.     
  12. int[] intArray = new int[120000];    
  13.     
  14. try {    
  15.     ct.setAutoCommit(false); // 开始事务    
  16.     pst = ct.prepareStatement(deleteSql);    
  17.     for (int i = 0; i < 120000; i++) {    
  18.     
  19.         pst.setString(1"name" + i);    
  20.         pst.addBatch();    
  21.     
  22.         System.out.println(i);    
  23.         // 分段提交    
  24.         if ((i % 50000 == 0 && i != 0) || i == (120000 - 1)) {    
  25.             intArray = pst.executeBatch();    
  26.             ct.commit();    
  27.             ct.setAutoCommit(false);// 开始事务    
  28.             pst = ct.prepareStatement(deleteSql);    
  29.             System.out.println("------------>50000");    
  30.     
  31.         }    
  32.     
  33.     }    
  34.     
  35.     ct.commit();// 提交事务    
  36.     
  37. catch (SQLException e) {    
  38.     
  39.     try {    
  40.         ct.rollback();    
  41.     } catch (SQLException e1) {    
  42.     
  43.         e1.printStackTrace();    
  44.     }    
  45.     e.printStackTrace();    
  46. catch (RuntimeException e) {    
  47.     try {    
  48.         ct.rollback();    
  49.     } catch (SQLException e1) {    
  50.     
  51.         e1.printStackTrace();    
  52.     }    
  53.     
  54. finally {    
  55.     cd.close(ct, pst, rs);    
  56. }    

以下是更新数据的代码:

[java]  view plain copy
  1. ConnDB cd = new ConnDB();    
  2.         Connection ct = null;    
  3.         PreparedStatement pst = null;    
  4.         ResultSet rs = null;    
  5.     
  6.         ct = cd.getConn();    
  7.         String deleteSql = "update  mytable t set t.age =20 where t.name=?";    
  8.     
  9.         System.out.println(deleteSql);    
  10.     
  11.         int[] intArray = new int[120000];    
  12.     
  13.         try {    
  14.             ct.setAutoCommit(false); // 开始事务    
  15.             pst = ct.prepareStatement(deleteSql);    
  16.             for (int i = 0; i < 120000; i++) {    
  17.     
  18.                 pst.setString(1"name"+i);    
  19.                 pst.addBatch();    
  20.     
  21.                 System.out.println(i);    
  22.                 // 分段提交    
  23.                 if ((i % 50000 == 0 && i != 0) || i == (120000 - 1)) {    
  24.                     intArray = pst.executeBatch();    
  25.                     ct.commit();    
  26.                     ct.setAutoCommit(false);// 开始事务    
  27.                     pst = ct.prepareStatement(deleteSql);    
  28.                     System.out.println("------------>50000");    
  29.     
  30.                 }    
  31.     
  32.             }    
  33.     
  34.             ct.commit();// 提交事务    
  35.     
  36.         } catch (SQLException e) {    
  37.     
  38.             try {    
  39.                 ct.rollback();    
  40.             } catch (SQLException e1) {    
  41.     
  42.                 e1.printStackTrace();    
  43.             }    
  44.             e.printStackTrace();    
  45.         } catch (RuntimeException e) {    
  46.             try {    
  47.                 ct.rollback();    
  48.             } catch (SQLException e1) {    
  49.     
  50.                 e1.printStackTrace();    
  51.             }    
  52.     
  53.         } finally {    
  54.             cd.close(ct, pst, rs);    
  55.         }    
  56.     
  57.     }
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用JDBC进行批量插入数据,你可以按照以下步骤操作: 1. 获取数据库连接:使用JDBC驱动程序获取与数据库的连接。例如,使用`DriverManager.getConnection()`方法。 2. 创建PreparedStatement对象:使用连接对象创建`PreparedStatement`对象,并指定插入语句,但是不指定参数。例如,`connection.prepareStatement("INSERT INTO table_name (column1, column2) VALUES (?, ?)")`。 3. 设置参数:使用`PreparedStatement`对象的`setXxx()`方法为每个参数设置。例如,`preparedStatement.setInt(1, value1)`和`preparedStatement.setString(2, value2)`。 4. 添加批量操作:使用`addBatch()`方法将每个`PreparedStatement`对象添加到批处理中。例如,`preparedStatement.addBatch()`。 5. 执行批处理:使用`executeBatch()`方法执行批处理操作。例如,`preparedStatement.executeBatch()`。 6. 关闭连接和声明对象:在操作完成后,关闭连接和所有相关的声明对象。例如,`preparedStatement.close()`和`connection.close()`。 以下是一个示例代码片段,演示如何使用JDBC进行批量插入数据: ```java Connection connection = null; PreparedStatement preparedStatement = null; try { connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", "username", "password"); String insertQuery = "INSERT INTO table_name (column1, column2) VALUES (?, ?)"; preparedStatement = connection.prepareStatement(insertQuery); // 设置参数并添加到批处理 preparedStatement.setInt(1, value1); preparedStatement.setString(2, value2); preparedStatement.addBatch(); // 可以继续添加更多批处理操作 // 执行批处理 int[] batchResult = preparedStatement.executeBatch(); // 处理处理结果 } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭连接和声明对象 if (preparedStatement != null) { try { preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } ``` 请注意,以上代码只是一个示例,你需要根据实际的表结构和数据来修改插入语句和参数设置。确保在使用JDBC进行数据库操作时,遵循相关的安全和最佳实践。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值