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 閱讀(1345) 評論(0)  編輯  收藏

          主站蜘蛛池模板: 施秉县| 龙川县| 鄂温| 浮梁县| 炉霍县| 固安县| 股票| 原阳县| 额敏县| 浮梁县| 名山县| 壶关县| 鹿邑县| 南江县| 贡嘎县| 长子县| 白银市| 方正县| 定州市| 山西省| 河曲县| 张北县| 香河县| 永平县| 登封市| 临邑县| 利辛县| 邹城市| 洞头县| 循化| 水富县| 南漳县| 平遥县| 台山市| 黎川县| 平塘县| 任丘市| 绥棱县| 黄梅县| 平乐县| 临高县|