tbwshc

采用jdbc批处理 提高jdbc效率 .

1.将jdbc操作改成批处理  addBatch(); //添加批处理

 

2.使用PreparedStatement

 

 

代码:

eg:

 

 

  1. Connection conn = DBUtils.getInstance().getConnetion();  
  2.   
  3. conn.setAutoCommit(false );    
  4.   
  5. PreparedStatement pstmt = null;  
  6.   
  7. try   
  8.   
  9. pstmt = conn.preparedStatement("insert into test1(a,b) vlaues (?,?)");  
  10.   
  11. pstmt.clearBatch();  
  12.   
  13. for(int i = 0; i<100000;i++){  
  14.   
  15.      pstmt.setInt(1,i);  
  16.   
  17.      pstmt.setString(2,"value"+i);  
  18.   
  19.      pstmt.addBatch();  
  20.   
  21.     if(i % 10000){  
  22.   
  23.             pstmt.executbeBatch();  
  24.   
  25.     }  
  26.   
  27.   
  28.   
  29. }  
  30.   
  31.   
  32.   
  33.   
  34.   
  35. pstmt.executeBatch();  
  36.   
  37.   
  38.   
  39. conn.commit();  
  40.   
  41. catch(Exception e) {  
  42.   
  43.       conn.rollback();  
  44.   
  45. }  finally {  
  46.   
  47.      conn.setAutocommit(true);  
  48.   
  49. }  

posted on 2012-08-10 14:59 chen11-1 阅读(1329) 评论(0)  编辑  收藏