天天看點

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