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


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


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

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 黄大仙区| 兰溪市| 金秀| 伊金霍洛旗| 泸水县| 耒阳市| 准格尔旗| 桂平市| 米脂县| 聂拉木县| 德兴市| 洪湖市| 滕州市| 黄山市| 肥东县| 泰州市| 朔州市| 贡山| 纳雍县| 长沙市| 光泽县| 泰州市| 新和县| 博白县| 和平区| 犍为县| 满城县| 綦江县| 报价| 诸城市| 来宾市| 正宁县| 永安市| 开阳县| 清河县| 清水河县| 武陟县| 启东市| 海门市| 包头市| 高尔夫|