使用jdbc向數據庫插入100000條記錄

分別使用statement,PreparedStatement,及PreparedStatement+批處理3種方式進行測試:


//1.使用statement插入100000條記錄 

Java代碼  收藏代碼
  1. public void exec(Connection conn){   
  2.   try {   
  3.    //開始時間  
  4.    Long beginTime = System.currentTimeMillis();   
  5.    //設置手動提交   
  6.    conn.setAutoCommit(false);  
  7.       
  8.    Statement st = conn.createStatement();   
  9.   
  10.    for(int i=0;i<100000;i++){   
  11.       String sql="insert into t1(id) values ("+i+")";   
  12.       st.executeUpdate(sql);    
  13.    }   
  14.      
  15.    //結束時間  
  16.    Long endTime = System.currentTimeMillis();   
  17.   
  18.    System.out.println("st:"+(endTime-beginTime)/1000+"秒");//計算時間   
  19.   
  20.    st.close();   
  21.    conn.close();   
  22.   } catch (SQLException e) {   
  23.    e.printStackTrace();   
  24.   }    
  25. }   
 

//2.使用PreparedStatement對象 

Java代碼  收藏代碼
  1. public void exec2(Connection conn){   
  2.   try {   
  3.    Long beginTime = System.currentTimeMillis();   
  4.    conn.setAutoCommit(false);//手動提交   
  5.    PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)");   
  6.    for(int i=0;i<100000;i++){   
  7.     pst.setInt(1, i);   
  8.     pst.execute();      
  9.    }   
  10.    conn.commit();   
  11.    Long endTime = System.currentTimeMillis();   
  12.    System.out.println("pst:"+(endTime-beginTime)/1000+"秒");//計算時間   
  13.    pst.close();   
  14.    conn.close();   
  15.   } catch (SQLException e) {   
  16.    e.printStackTrace();   
  17.   }   
  18.   
  19. }   
 

//3.使用PreparedStatement + 批處理 

Java代碼  收藏代碼
  1. public void exec3(Connection conn){   
  2.   try {   
  3.    conn.setAutoCommit(false);   
  4.    Long beginTime = System.currentTimeMillis();   
  5.    //構造預處理statement  
  6.    PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)");   
  7.    //1萬次循環  
  8.    for(int i=1;i<=100000;i++){      
  9.     pst.setInt(1, i);   
  10.     pst.addBatch();   
  11.     //每1000次提交一次  
  12.     if(i%1000==0){//可以設置不同的大小;如50,100,500,1000等等   
  13.      pst.executeBatch();   
  14.      conn.commit();   
  15.      pst.clearBatch();   
  16.     }   
  17.    }  
  18.    Long endTime = System.currentTimeMillis();   
  19.    System.out.println("pst+batch:"+(endTime-beginTime)/1000+"秒");   
  20.    pst.close();   
  21.    conn.close();   
  22.   } catch (SQLException e) {   
  23.    e.printStackTrace();   
  24.   }   
  25. }   
 
在Oracle 10g中測試,結果: 
1.使用statement  耗時142秒; 
2.使用PreparedStatement 耗時56秒; 
3.使用PreparedStatement + 批處理耗時: 
   a.50條插入一次,耗時5秒; 
    b.100條插入一次,耗時2秒; 
    c.1000條以上插入一次,耗時1秒; 
通過以上可以得出結論,在使用jdbc大批量插入數據時,明顯使用第三種方式(PreparedStatement + 批處理)性能更優。 

 

 

-------------------------------------------------------------------------------------------------------------------------------

Java代碼  收藏代碼
  1. /*  
Java代碼  收藏代碼
  1. 普通方式處理大量數據的insert時,處理速度相當慢。  
  2. */  
  3. PreparedStatement ps = null;  
  4. //循環10000次  
  5. for(int i = 0; i < 100000; i++) {  
  6.     ps = con.prepareStatement(sql);  
  7.     ps.executeUpdate();  
  8. }  

 /*

 

Java代碼  收藏代碼
  1. 方法二:通過addBatch()的方式,將數據緩存在對象裏面,通過最後執行executeBatch();方法提交,因此速度會快很多!  
  2. */  
  3. PreparedStatement ps = con.prepareStatement(sql);  
  4. for(int i = 0; i < 100000; i++) {  
  5.     ps.setString(1"1");  
  6.     ps.setString(2"2");  
  7.     ps.addBatch();  
  8. }  
  9. ps.executeBatch(); 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章