IBM DB2 Manual page 99

Table of Contents

Advertisement

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Each ResultSet contains:
v If the ResultSet is not null, it contains the automatically generated keys
for an execution of the batched SQL statement.
v If the ResultSet is null, an error occurred during retrieval of the
automatically generated keys for the SQL statement.
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.
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 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();
for (int i = 0; i < updateCounts.length; i++) {
System.err.println(" Statement " + i + ":" + updateCounts[i]);
}
ResultSet[] resultList =
((DBBatchUpdateException)buex).getDBGeneratedKeys();
for (i = 0; i < resultList.length(); i++)
{
if (resultList[i] == null)
continue; // Skip the ResultSet for which there was a failure
else {
java.math.BigDecimal idColVar = rs.getBigDecimal(1);
System.out.println("Automatically generated key value = " + idColVar);
}
}
System.err.println(" Message: " + buex.getMessage());
System.err.println(" SQLSTATE: " + buex.getSQLState());
System.err.println(" Error code: " + buex.getErrorCode());
SQLException ex = buex.getNextException();
while (ex != null) {
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();
}
}
Related tasks
"Making batch updates in JDBC applications" on page 28
// Get automatically generated key
// value
Chapter 3. JDBC application programming
1
2
3
4
5
83

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents