当前位置:Gxlcms > 数据库问题 > jdbc批量插入操作(addBatch)

jdbc批量插入操作(addBatch)

时间:2021-07-01 10:21:17 帮助过:25人阅读

/** 2 * 批量插入 3 */ 4 @Test 5 public void testInsert(){ 6 Connection conn=null; 7 Statement st=null; 8 PreparedStatement pst=null; 9 try { 10 conn=DBUtils.getConn(); 11 conn.setAutoCommit(false);//事物不能自动提交 12 st=conn.createStatement(); 13 long start = System.currentTimeMillis(); 14 String sql="insert into stu1 values(?,?)"; 15 pst=conn.prepareStatement(sql); 16 int count=0; 17 for(int i=1;i<=10008;i++){ 18 pst.setInt(1,i); 19 pst.setString(2,"Tmo"+i); 20 pst.addBatch(); 21 count++; 22 if(count==2000){ 23 pst.executeBatch(); 24 pst.clearBatch(); 25 count=0; 26 } 27 } 28 pst.executeBatch();//对最后不足2000的进行增加 29 pst.clearBatch(); 30 conn.commit();//提交事务 31 System.out.println(System.currentTimeMillis()-start); 32 } catch (SQLException e) { 33 // TODO Auto-generated catch block 34 e.printStackTrace(); 35 } 36 finally{ 37 DBUtils.closeAll(null, st, conn); 38 } 39 }

 

jdbc批量插入操作(addBatch)

标签:time   stat   toc   dba   span   rate   count   sea   void   

人气教程排行