2011-03-07 28 views
3

我想執行下列確切SQL查詢:如何獲取包含在批處理例外

(從處理數據庫的讀取行轉移到PRESENTATION DB一排)

PreparedStatement stmt; 
    Statement st; 
    Connection conn = ConnectionManager.getInstance().getConnection(ConnectionManager.PRESENTATION_DB); 
    try { 
     conn.setAutoCommit(false); 
     stmt = conn.prepareStatement(
       "insert into customer (name,category,age) values (?,?,?)"); 
        stmt.clearParameters(); 
        stmt.setString(1,"name2"); 
        stmt.setString(2,"cat2"); 
        stmt.setInt(3,25); 
        stmt.addBatch(); 
        stmt.clearParameters(); 
        stmt.setString(1,"name3"); 
        stmt.setString(2,"cat3"); 
        stmt.setInt(3,25); 
        stmt.addBatch(); 
        stmt.clearParameters(); 
        stmt.setString(1,"name4"); 
        stmt.setString(2,"cat2"); 
        stmt.setInt(3,25); 
        stmt.addBatch(); 
        stmt.clearParameters(); 
        stmt.setString(1,"name4"); 
        stmt.setString(2,"cat2"); 
        //stmt.setInt(3,25); 

        //this is parameter with false input 
        stmt.setString(3,"null"); 

        stmt.addBatch(); 
        stmt.clearParameters(); 
        stmt.setString(1,"name5"); 
        stmt.setString(2,"cat5"); 
        stmt.setInt(3,25); 
        stmt.addBatch(); 
        stmt.clearParameters(); 

     int [] updateCounts = stmt.executeBatch(); 
     conn.commit(); 
     conn.setAutoCommit(true); 

        stmt.close(); 
     conn.close(); 
    } 
    catch(BatchUpdateException b) { 
        System.err.println("-----BatchUpdateException-----"); 
        System.err.println("SQLState: " + b.getSQLState()); 
        System.err.println("Message: " + b.getMessage()); 
        System.err.println("Vendor: " + b.getErrorCode()); 
        System.err.print("Update counts: "); 
        int [] updateCounts = b.getUpdateCounts(); 
        for (int i = 0; i < updateCounts.length; i++) { 
          System.err.print(updateCounts[i] + " "); 
        } 
        System.err.println(""); 
      } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 

如何識別導致異常能夠在沒有這個特定行的情況下重新執行批處理的行? (通過更新行的字段hasError)。

目前我不是通過批量保存對象,因此如果它導致錯誤並跳過該行並繼續處理(即保存/傳輸和刪除)另一行,則可以標記該行。

謝謝!

更新:

好的,我用下面的代碼來跟蹤行(如果有的話行導致錯誤):

 public static void processUpdateCounts(int[] updateCounts) { 
      for (int i=0; i<updateCounts.length; i++) { 
       if (updateCounts[i] >= 0) { 
        // Successfully executed; the number represents number of affected rows 
        logger.info("Successfully executed: number of affected rows: "+updateCounts[i]); 
       } else if (updateCounts[i] == Statement.SUCCESS_NO_INFO) { 
        // Successfully executed; number of affected rows not available 
        logger.info("Successfully executed: number of affected rows not available: "+updateCounts[i]); 
       } else if (updateCounts[i] == Statement.EXECUTE_FAILED) { 
        logger.info("Failed to execute: "+updateCounts[i]); 
        // Failed to execute 
       } 
      } 
     } 

而且增加了行:

 processUpdateCounts(updateCounts); 

所以我可以看到有或沒有任何錯誤的變化,但它看起來像BatchUpdateException不起作用?從的BatchUpdateException的的Javadoc

回答

0

行情:

元件中的 更新計數陣列的順序對應於其中 命令加入到 批次的順序。

在 無法正確執行的批處理更新的命令和一個 的BatchUpdateException被拋出後, 驅動器可以或者可以不繼續 過程在 批處理中的剩餘命令。如果駕駛員在發生故障後繼續 處理,陣列 通過該方法 讓BatchUpdateException.getUpdateCounts 批次中的返回將有一個元件,用於每個命令 而不是隻元件 該誤差之前執行 成功的命令。在驅動程序繼續執行 處理命令的 情況下,對於任何失敗的命令,數組元素 是 Statement.EXECUTE_FAILED。

+0

嗨,感謝您的即時回覆,我已更新我的問題,並希望能爲此輸入任何信息,謝謝! – eunique0216 2011-03-08 05:50:24

0

正如已經指出的那樣,您只能訪問在批處理期間失敗的SQL語句的索引。這意味着,爲了創建相同的語句執行失敗,你應該存儲在可轉位方式的參數,如:

List<...>paramList = new ArrayList<...>(); 
for (... params in paramList) { 
    stmt.clearParameters(); 
    stmt.setString(...something involving params...); 
    // etc. 
} 

這樣,getUpdateCountsEXECUTE_FAILED陣列的指數可以用來識別哪些參數導致失敗。

0

創建一個幫助執行命令的數據庫層,如果sql命令使用while循環重試幾次,如果幾次後仍然失敗,則記錄它。

0

我正在使用mysql-connector-java-3.1.14和MySQL。本主題頂部的更新(使用BatchUpdateException和getUpdateCounts)正在正常工作。現在我可以在批量插入/更新中找到有問題的行。

相關問題