jdbc批量插入操作(addBatch)

 1 /**
 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     }

 

转载于:https://www.cnblogs.com/yihaifutai/p/6772467.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值