con.commit(); 4b
} catch(BatchUpdateException b) { 5
// process BatchUpdateException
}
In the following code fragment, a batched statement returns automatically
generated keys.
try {
...
PreparedStatement pStmt = con.prepareStatement( 1
"INSERT INTO DEPT (DEPTNO, DEPTNAME, ADMRDEPT) " +
"VALUES (?,?,?)",
Statement.RETURN_GENERATED_KEYS);
pStmt.setString(1,"X01"); 2a
pStmt.setString(2,"Finance");
pStmt.setString(3,"A00");
pStmt.addBatch(); 2b
pStmt.setString(1,"Y01");
pStmt.setString(2,"Accounting");
pStmt.setString(3,"A00");
pStmt.addBatch();
int [] numUpdates=prepStmt.executeBatch(); 3
for (int i=0; i < numUpdates.length; i++) { 4a
if (numUpdates[i] == SUCCESS_NO_INFO)
System.out.println("Execution"+i+
": unknown number of rows updated");
else
System.out.println("Execution"+i+
"successful: " numUpdates[i] + " rows updated");
}
con.commit(); 4b
ResultSet[] resultList =
((DB2PreparedStatement)pStmt).getDBGeneratedKeys(); 4c
if (resultList.length != 0) {
for (i = 0; i < resultList.length; i++) {
while (resultList[i].next()) {
java.math.BigDecimal idColVar = rs.getBigDecimal(1);
// Get automatically generated key
// value
System.out.println("Automatically generated key value = "
+ idColVar);
}
}
}
else {
System.out.println("Error retrieving automatically generated keys");
}
} catch(BatchUpdateException b) { 5
// process BatchUpdateException
}
Related tasks
“Retrieving information from a BatchUpdateException” on page 82
“Making batch updates in SQLJ applications” on page 119
“Making batch queries in JDBC applications” on page 34
“Committing or rolling back JDBC transactions” on page 74
Related reference
“JDBC differences between the current IBM Data Server Driver for JDBC and
SQLJ and earlier DB2 JDBC drivers” on page 370
30 Application Programming Guide and Reference for Java
™
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|