Retrieval of Autogenerated Keys

JDBC 3.0 addresses the common need to obtain from columns the value of an autogenerated or autoincremented key.

To retrieve the autogenerated keys, pass the constant Statement.RETURN_GENERATED_KEYS as the second parameter of the Statement.execute() method.

After you have executed the statement, call Statement.getGeneratedKeys() to retrieve the generated keys. The result set contains a row for each generated key retrieved.

Note: Adaptive Server cannot return a result set of generated keys. If you execute a batch of insert commands, invoking Statement.getGeneratedKeys() returns only the value of the last generated key.

For more information about retrieving auto-generated keys, including a sample code, search for “retrieving automatically generated keys” on the Oracle Java Web site.