Retrieving information from a BatchUpdateException

When an error occurs during execution of a statement in a batch, processing continues. However, executeBatch throws a BatchUpdateException.

Procedure

To retrieve information from the BatchUpdateException, follow these steps:

  1. Use the BatchUpdateException.getUpdateCounts method to determine the number of rows that each SQL statement in the batch updated before the exception was thrown.
    getUpdateCount returns an array with an element for each statement in the batch. An element has one of the following values:
    n
    The number of rows that the statement updated.
    Statement.SUCCESS_NO_INFO
    This value is returned if the number of updated rows cannot be determined. The number of updated rows cannot be determined if the following conditions are true:
    • The application is connected to a subsystem that is in Db2 for z/OS® Version 8 new-function mode, or later.
    • The application is using Version 3.1 or later of the IBM® Data Server Driver for JDBC and SQLJ.
    • The IBM Data Server Driver for JDBC and SQLJ uses multi-row INSERT operations to execute batch updates.
    Statement.EXECUTE_FAILED
    This value is returned if the statement did not execute successfully.
  2. If the batched statement can return automatically generated keys:
    1. Cast the BatchUpdateException to a com.ibm.db2.jcc.DBBatchUpdateException.
    2. Call the DBBatchUpdateException.getDBGeneratedKeys method to retrieve an array of ResultSet objects that contains the automatically generated keys for each execution of the batched SQL statement.
    3. Test whether each ResultSet in the array is null.
      Each ResultSet contains:
      • If the ResultSet is not null, it contains the automatically generated keys for an execution of the batched SQL statement.
      • If the ResultSet is null, execution of the batched statement failed.
  3. Use SQLException methods getMessage, getSQLState, and getErrorCode to retrieve the description of the error, the SQLSTATE, and the error code for the first error.
  4. Use the BatchUpdateException.getNextException method to get a chained SQLException.
  5. In a loop, execute the getMessage, getSQLState, getErrorCode, and getNextException method calls to obtain information about an SQLException and get the next SQLException.

Example

The following code fragment demonstrates how to obtain the fields of a BatchUpdateException and the chained SQLException objects for a batched statement that returns automatically generated keys. The example assumes that there is only one column in the automatically generated key, and that there is always exactly one key value, whose data type is numeric. The numbers to the right of selected statements correspond to the previously-described steps.
try {
  // Batch updates
} catch(BatchUpdateException buex) {
    System.err.println("Contents of BatchUpdateException:");
    System.err.println(" Update counts: ");
    int [] updateCounts = buex.getUpdateCounts();             1 
    for (int i = 0; i < updateCounts.length; i++) {
      System.err.println("  Statement " + i + ":" + updateCounts[i]);
    }
    ResultSet[] resultList = 
      ((DBBatchUpdateException)buex).getDBGeneratedKeys();    2 
    for (i = 0; i < resultList.length; i++)
    {
      if (resultList[i] == null)
         continue; // Skip the ResultSet for which there was a failure
      else {
        rs.next();
        java.math.BigDecimal idColVar = rs.getBigDecimal(1);     
                                      // Get automatically generated key 
                                      // value
        System.out.println("Automatically generated key value = " + idColVar);
      }
    }
    System.err.println(" Message: " + buex.getMessage());     3 
    System.err.println(" SQLSTATE: " + buex.getSQLState());
    System.err.println(" Error code: " + buex.getErrorCode());
    SQLException ex = buex.getNextException();                4 
    while (ex != null) {                                      5 
      System.err.println("SQL exception:");
      System.err.println(" Message: " + ex.getMessage());
      System.err.println(" SQLSTATE: " + ex.getSQLState());
      System.err.println(" Error code: " + ex.getErrorCode());
      ex = ex.getNextException();
    }
}