精髓!關于 executeBatch() 的具體詳解用法
executeBatch()詳解
JDBC提供了數據庫batch處理的能力,在數據大批量操作(新增、刪除等)的情況下可以大幅度提升系統的性能。
// 關閉自動執行
con.setAutoCommit(false);
Statement stmt = con.createStatement();
stmt.addBatch(“INSERT INTO employees VALUES (1000, ‘Joe Jones’)”);
stmt.addBatch(“INSERT INTO departments VALUES (260, ‘Shoe’)”);
stmt.addBatch(“INSERT INTO emp_dept VALUES (1000, 260)”);
// 提交一批要執行的更新命令
int[] updateCounts = stmt.executeBatch();
本例中禁用了自動執行模式,從而在調用 Statement.executeBatch() 時可以防止 JDBC 執行事務處理。禁用自動執行使得應用程序能夠在發生錯誤及批處理中的某些命令不能執行時決定是否執行事務處理。因此,當進行批處理更新時,通常應該關閉自動執行。
在JDBC 2.0 中,Statement 對象能夠記住可以一起提交執行的命令列表。創建語句時,與它關聯的命令列表為空。Statement.addBatch() 方法為調用語句的命令列表添加一個元素。如果批處理中包含有試圖返回結果集的命令,則當調用 Statement. executeBatch() 時,將拋出 SQLException。只有 DDL 和 DML 命令(它們只返回簡單的更新計數)才能作為批處理的一部分來執行。如果應用程序決定不提交已經為某語句構
1
2
3
造的命令批處理,則可以調用方法 Statement.clearBatch()(以上沒有顯示)來重新設置批處理。
Statement.executeBatch() 方法將把命令批處理提交給基本 DBMS 來執行。命令的執行將依照在批處理中的添加順序來進行。ExecuteBatch() 為執行的命令返回更新計數數組。數組中對應于批處理中的每個命令都包含了一項,而數組中各元素依據命令的執行順序(這還是和命令的最初添加順序相同)來排序。調用executeBatch() 將關閉發出調用的 Statement 對象的當前結果集(如果有一個結果集是打開的)。executeBatch() 返回后,將重新將語句的內部批處理命令列表設置為空。
如果批處理中的某個命令無法正確執行,則 ExecuteBatch() 將拋出BatchUpdateException。可以調用BatchUpdateException.getUpdateCounts() 方法來為批處理中成功執行的命令返回更新計數的整型數組。因為當有第一個命令返回錯誤時,Statement.executeBatch() 就中止,而且這些命令是依據它們在批處理中的添加順序而執行的。所以如果 BatchUpdateException.getUpdateCounts() 所返回的數組包含 N 個元素,這就意味著在調用 executeBatch() 時批處理中的前 N 個命令被成功執行。用PreparedStatement 可以象下面這樣寫代碼:
// 關閉自動執行
con.setAutoCommit(false);
PreparedStatement stmt = con.prepareStatement(“INSERT INTO employees VALUES (?, ?)”);
stmt.setInt(1, 2000);
stmt.setString(2, “Kelly Kaufmann”);
stmt.addBatch();
// 提交要執行的批處理
int[] updateCounts = stmt.executeBatch();
========================================
PrepareStatement 也是接口
PrepareStatement extends Statement
PrepareStatement 本身沒有 int[] executeBatch() throws SQLException 方法
而是繼承了Statement的方法,且它們都是接口沒有實際實現方法,但Statement
接口對executeBatch()方法做了規范
/**
* Submits a batch of commands to the database for execution and
* if all commands execute successfully, returns an array of update counts.
每次提交一批命令到數據庫中執行,如果所有的命令都成功執行了,那么返回一個
數組,這個數組是說明每條命令所影響的行數
* The int elements of the array that is returned are ordered
* to correspond to the commands in the batch, which are ordered
* according to the order in which they were added to the batch.
返回的數組中每個整型值都是排過序的,它們的順序和批量處理中的命令們是一致的,
命令的順序是按照它們被加到批處理中的順序一致。
* The elements in the array returned by the method executeBatch
* may be one of the following:
executeBatch方法返回的數組中的元素可能是下面幾種情形之一:
A number greater than or equal to zero – indicates that the
* command was processed successfully and is an update count giving the
* number of rows in the database that were affected by the command’s
* execution
一個大于或等于零的數字,簡單說來命令成功執行后就返回它所影響到的行的數目
A value of SUCCESS_NO_INFO – indicates that the command was
* processed successfully but that the number of rows affected is
* unknown
* The constant indicating that a batch statement executed successfully
* but that no count of the number of rows it affected is available.
int SUCCESS_NO_INFO = -2;
常量SUCCESS_NO_INFO代表的值=-2,也就是說命令執行成功了但命令影響到的行數
無法統計,是未知的,只能返回SUCCESS_NO_INFO來說明命令執行情況。
* If one of the commands in a batch update fails to execute properly,
* this method throws aBatchUpdateException, and a JDBC
* driver may or may not continue to process the remaining commands in
* the batch.
如果批量處理時其中一個命令執行失敗,則會拋出一個異常BatchUpdateException
JDBC驅動可能會停止剩余的命令,也可能繼續執行剩余的命令。
* However, the driver's behavior must be consistent with a
* particular DBMS, either always continuing to process commands or never
* continuing to process commands.
不管怎樣,驅動要怎么做取決于數據庫管理系統的細節,總是執行或總是不執行兩者其一。
* If the driver continues processing
* after a failure, the array returned by the method
*BatchUpdateException.getUpdateCounts
* will contain as many elements as there are commands in the batch, and
* at least one of the elements will be the following:
發生失敗后如果驅動繼續執行,通過BatchUpdateException.getUpdateCounts()方法返回
的數組應該包括批處理中有的那些命令的結果,并且至少有一個元素的值是下面的情況:
A value ofEXECUTE_FAILED-- indicates that the command failed
* to execute successfully and occurs only if a driver continues to
* process commands after a command fails
int EXECUTE_FAILED = -3;
指示命令沒有成功執行的常量值EXECUTE_FAILED,并且只有在命令出錯后驅動繼續執行的情況下才會出現,
如果出錯后不再執行,則返回的結果中沒有錯誤信息只有那些被成功執行后的結果。
* A driver is not required to implement this method.
* The possible implementations and return values have been modified in
* the Java 2 SDK, Standard Edition, version 1.3 to
* accommodate the option of continuing to proccess commands in a batch
* update after aBatchUpdateExceptionobejct has been thrown.
驅動不實現此方法,可能會出現的實現和返回值在Java 2 SDK,Standard Edition,
version 1.3 ,以適應批處理時拋出BatchUpdateException 異常后是繼續執行還是
終止執行的選項。
* @return an array of update counts containing one element for each
* command in the batch. The elements of the array are ordered according
* to the order in which commands were added to the batch.
返回一個和添加命令時的順序一樣的數組結果
* @exception SQLException if a database access error occurs or the
* driver does not support batch statements. Throws {@link BatchUpdateException}
* (a subclass ofSQLException) if one of the commands sent to the
* database fails to execute properly or attempts to return a result set.
* @since 1.3
*/
如果數據庫訪問異常或驅動不支持批處理命令,或者如果一個命令發送到數據庫時失敗或嘗試取得結果,即使失敗,都會拋一個異常BatchUpdateException 它是SQLException的子類。