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 閱讀(2840) 評論(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

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 张家口市| 星子县| 柳州市| 保定市| 星座| 邻水| 灵石县| 阳泉市| 桓台县| 新津县| 苍山县| 孟村| 临泉县| 黑河市| 内乡县| 洛扎县| 平乐县| 靖西县| 新泰市| 常山县| 铜山县| 大化| 弥渡县| 邹城市| 沂南县| 阜新市| 大埔区| 芦溪县| 教育| 高州市| 鄂托克旗| 临夏县| 浦东新区| 铁岭市| 临颍县| 个旧市| 东宁县| 新泰市| 黄冈市| 凤冈县| 永德县|