java,大数据批量插入、更新

public void exec(Connection conn){ 
  try { 
   conn.setAutoCommit(false); 
   Long beginTime = System.currentTimeMillis(); 
   //构造预处理statement
   PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)"); 
   //1万次循环
   for(int i=1;i<=100000;i++){    
    pst.setInt(1, i); 
    pst.addBatch(); 
    //每1000次提交一次
    if(i%1000==0){//可以设置不同的大小;如50,100,500,1000等等 
     pst.executeBatch(); 
     conn.commit(); 
     pst.clearBatch(); 
    } 
   }
   Long endTime = System.currentTimeMillis(); 
   System.out.println("pst+batch:"+(endTime-beginTime)/1000+"秒"); 
   pst.close(); 
   conn.close(); 
  } catch (SQLException e) { 
   e.printStackTrace(); 
  } 
} 

 ssh中hibernate框架虽然便于开发,但是在处理大数据的上面显得有些不足,这时还是应该用jdbc

posted @ 2014-08-26 13:39  请叫我胖子  阅读(3012)  评论(0编辑  收藏  举报