java批量更新小记录

  1.  public void addEmployees(Connection conn, List<Employee> empList, int batchSize)  
  2.             throws SQLException {  
  3.         long bt = System.currentTimeMillis();  
  4.         PreparedStatement stmt = null;  
  5.         try {  
  6.             String sql = SqlParser.getInstance().getSql("Employee.insert");  
  7.             stmt = conn.prepareStatement(sql);  
  8.             int count = 0;  
  9.             for (Employee emp : empList) {  
  10.                 stmt.setInt(1, emp.getId());  
  11.                 stmt.setString(2, emp.getName());  
  12.                 stmt.setInt(3, emp.getDepartment().getId());  
  13.                 stmt.setString(4, emp.getDescription());  
  14.                 stmt.addBatch();  
  15.                   
  16.                 count++;                  
  17.                 if (count % batchSize == 0) {  
  18.                     stmt.executeBatch();  
  19.                 }  
  20.             }  
  21.               
  22.             stmt.executeBatch();  
  23.   
  24.         } finally {  
  25.             long et = System.currentTimeMillis();  
  26.             System.out.println(String.format("用时%dms", et-bt));  
  27.             DBUtil.close(stmt);  
  28.             DBUtil.close(conn);  
  29.         }  
  30.     }  

猜你喜欢

转载自sdu-wizard.iteye.com/blog/1343110