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框架雖然便於開發,可是在處理大數據的上面顯得有些不足,這時仍是應該用jdbcjava

相關文章
相關標籤/搜索