You can retrieve automatically generated keys (also called auto-generated keys or auto increment) from a table using JDBC 3.0 methods
getGeneratedKeys()provide a standard way to make auto-generated or identity column values available to an application that is updating a database table without a requiring a query and a second round-trip to the server. SQL Server allows only a single auto increment column per table.
ResultSetthat is returned by
getGeneratedKeysmethod will have only one column, with the returned column name of GENERATED_KEYS.
If generated keys are requested on a table that has no auto increment column, the JDBC driver will return a
When you insert rows by
executeUpdate or. Otherwise, the JDBC driver ignores the parameter that sets the flag.
executean INSERT statement or an
INSERT within SELECTstatement, you need to indicate that you will want to retrieve automatically generated key values. You do that by setting a flag in a Connection.prepareStatement, Statement.executeUpdate, or Statement.execute method call. The statement that is executed must be an INSERT statement or an
INSERTwithin SELECT statement