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的對象創建的就行。  回復  更多評論   


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


          網站導航:
           
          <2012年4月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 读书| 上虞市| 香港 | 灵宝市| 吉安市| 三亚市| 普定县| 常州市| 麦盖提县| 兴海县| 太仓市| 清远市| 南宫市| 潞西市| 瑞安市| 理塘县| 重庆市| 得荣县| 远安县| 政和县| 珲春市| 呼图壁县| 黄梅县| 沾化县| 绥江县| 平远县| 平定县| 乐山市| 天台县| 玉龙| 苏州市| 蒙山县| 北川| 东乡县| 望都县| 漳州市| 祁阳县| 九龙县| 华坪县| 澄城县| 奈曼旗|