The driver supports retrieving the values of auto-generated keys. An auto-generated key returned by the driver is the value of an identity column.
Note: Auto-generated keys are not supported for Microsoft Azure Synapse Analytics or Microsoft Analytics Platform System.
An application can return values of auto-generated keys when it executes an Insert statement. How you return these values depends on whether you are using an Insert statement with a Statement object or with a PreparedStatement object, as outlined in the following scenarios:
When using an Insert statement with a Statement object, the driver supports the following form of the Statement.execute and Statement.executeUpdate methods to instruct the driver to return values of auto-generated keys:
Statement.execute(String sql, int autoGeneratedKeys)
When using an Insert statement with a PreparedStatement object, the driver supports the following form of the Connection.prepareStatement method to instruct the driver to return values of auto-generated keys:
Connection.prepareStatement(String sql, int autoGeneratedKeys)
An application can retrieve values of auto-generated keys using the Statement.getGeneratedKeys() method. This method returns a ResultSet object with a column for each auto-generated key.