background image
<< java.sql.Statement interface | java.sql.CallableStatement interface >>

Autogenerated keys

<< java.sql.Statement interface | java.sql.CallableStatement interface >>
Derby Reference Manual
310
An error that occurs when a SELECT statement is first executed prevents a ResultSet
object from being opened on it. The same error does not close the ResultSet if it occurs
after the ResultSet has been opened.
For example, a divide-by-zero error that happens while the executeQuery method is
called on a java.sql.Statement or java.sql.PreparedStatement throws an exception and
returns no result set at all, while if the same error happens while the next method is
called on a ResultSet object, it does not cause the result set to be closed.
Errors can happen when a ResultSet is first being created if the system partially executes
the query before the first row is fetched. This can happen on any query that uses more
than one table and on queries that use aggregates, GROUP BY, ORDER BY, DISTINCT,
INTERSECT, EXCEPT, or UNION.
Closing a Statement causes all open ResultSet objects on that statement to be closed as
well.
The cursor name for the cursor of a ResultSet can be set before the statement is
executed. However, once it is executed, the cursor name cannot be altered.
Autogenerated keys
JDBC's auto-generated 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 auto-increment
feature, which allows users to create columns in tables for which the database
system automatically assigns increasing integer values. Users can call the method
Statement.getGeneratedKeys 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
.
Users can indicate that auto-generated columns should be made available for
retrieval by passing one of the following values as a second argument to the
Connection.prepareStatement, Statement.execute, or Statement.executeUpdate
methods:
· A constant indicating that auto-generated keys should be made available. The
specific constant to use is
Statement.RETURN_GENERATED_KEYS
.
· An array of the names of the columns in the inserted row that should be made
available. If any column name in the array does not designate an auto-increment
column, Derby will throw an error with the Derby embedded driver. With the client
driver, the one element column name is ignored currently and the value returned
corresponds to the identity column. To ensure compatibility with future changes an
application should ensure the column described is the identity column. If the column
name corresponds to another column or a non-existent column then future changes
may result in a value for a different column being returned or an exception being
thrown.
· An array of the positions of the columns in the inserted row that should be made
available. If any column position in the array does not correlate to an auto-increment
column, Derby will throw an error with the Derby embedded driver. With the client
driver, the one element position array is ignored currently and the value returned
corresponds to the identity column. To ensure compatibility with future changes
an application should ensure the column described is the identity column. If the
position corresponds to another column or a non-existent column then future
changes may result in a value for a different column being returned or an exception
being thrown.
Example