JDBC 3.0's autogenerated keys feature provides a way to retrieve values from columns that are part of an index or have a default value assigned. Derby supports the autoincrement feature, which allows users to create columns in tables for which the database system automatically assigns increasing integer values. In JDBC 3.0, the method Statement.getGeneratedKeys can be called to retrieve the value of such a column. This method returns a ResultSet object with a column for the automatically generated key. Calling ResultSet.getMetaData on the ResultSet object returned by getGeneratedKeys produces a ResultSetMetaData object that is similar to that returned by IDENTITY_VAL_LOCAL. A flag indicating that any auto-generated columns should be returned is passed to the methods execute, executeUpdate, or prepareStatement when the statement is executed or prepared.
Statement stmt = conn.createStatement(); int rows = stmt.executeUpdate("INSERT INTO TABLE1 (C11, C12) VALUES (1,1)", Statement.RETURN_GENERATED_KEYS); ResultSet rs = stmt.getGeneratedKeys();
To use Autogenerated Keys in INSERT statements, pass the Statement.RETURN_GENERATED_KEYS flag to the execute or executeUpdate method. Derby does not support passing column names or column indexes to the execute, executeUpdate, or prepareStatement methods.