java sql批量提交

          使用PreparedStatement批量提交SQL。
          Connection conn = null;
          PreparedStatement pstmt 
          = null;
          try {
              Class.forName(
          "oracle.jdbc.driver.OracleDriver");
              conn 
          = DriverManager.getConnection(
                              
          "jdbc:oracle:thin:@localhost:1521:orcl""scott",
                              
          "tigter");
              String sql 
          = "delete from table_tmp where id = ?";
              conn.setAutoCommit(
          false);
              pstmt 
          =   conn.prepareStatement(sql);
              
          for(int i = 100; i<200; i++{
                  pstmt.setString(
          1,i);
                  pstmt.addBatch();
              }

              pstmt.executeBatch();
              conn.commite();
          }
           catch(Exception e) {
              e.printStackTrace();
          }

          finally {
           
          try {
              
          if(pstmt != null)
                              pstmt.close();
                      }
           catch (SQLException e) {
                          e.printStackTrace();
                      }

                      
                      
          try {
                          
          if(conn != null{
                              conn.close();
                          }

                      }
            catch (SQLException e) {
                          e.printStackTrace();
                      }
          }

          finally

          批量處理時效率比較高,只進行一次數據庫連接。

          posted on 2011-10-20 11:48 hxwhui 閱讀(2839) 評論(2)  編輯  收藏

          評論

          # re: java sql批量提交 2012-04-17 09:32 mqyl1


          update t_a set name = ? where id = ?
          update t_a set age = ? where id = ?

          這兩個可以進行批量提交嗎?  回復  更多評論   

          # re: java sql批量提交[未登錄] 2012-04-17 09:36 hxwhui

          @mqyl1
          應該可以的 創建兩個 PreparedStatement 應該就可以,提交是通過Connection的對象進行提交的,只要兩個PreParedStatement是一個Connection的對象創建的就行。  回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2011年10月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 云和县| 德昌县| 武汉市| 万载县| 华坪县| 伊川县| 安新县| 句容市| 河西区| 江门市| 林周县| 永平县| 高邑县| 响水县| 文成县| 大庆市| 桐庐县| 盖州市| 农安县| 保德县| 栾城县| 昌都县| 华坪县| 丁青县| 旺苍县| 清流县| 襄汾县| 勐海县| 霞浦县| 长岭县| 冕宁县| 广灵县| 梓潼县| 青海省| 平江县| 海盐县| 利川市| 永宁县| 靖宇县| 淅川县| 宜良县|