Retrieval of automatically generated keys in JDBC applications
With the IBM® Data Server Driver for JDBC and SQLJ, you can retrieve automatically generated keys (also called auto-generated keys) from a table.
An automatically generated key is any value that is generated by the data server, instead of being specified by the user. One type of automatically generated key is the contents of an identity column. An identity column is a table column that provides a way for the data source to automatically generate a numeric value for each row. You define an identity column in a CREATE TABLE or ALTER TABLE statement by specifying the AS IDENTITY clause when you define a column that has an exact numeric type with a scale of 0 (SMALLINT, INTEGER, BIGINT, DECIMAL with a scale of zero, or a distinct type based on one of these types).
For connections to Db2® for z/OS® or Db2 on Linux®, UNIX, and Windows systems, the IBM Data Server Driver for JDBC and SQLJ supports the return of automatically generated keys for INSERT statements, for searched UPDATE or searched DELETE statements, or for MERGE statements. For UPDATE, DELETE, or MERGE statements, you can identify any columns as automatically generated keys, even if they are not generated by the data server. In this case, the column values that are returned are the column values for the rows that are modified by the UPDATE, DELETE, or MERGE statement.
DB2BaseDataSource.YES
(1),
you cannot prepare an SQL statement for retrieval of automatically
generated keys and use the PreparedStatement object
for batch updates. The IBM Data Server Driver for JDBC and
SQLJ version
3.50 or later throws an SQLException when you call
the addBatch or executeBatch method
on a PreparedStatement object that is prepared
to return automatically generated keys.