IBM DB2 Manual page 75

Table of Contents

Advertisement

|
|
|
|
method call. The statement that is executed must be an INSERT statement or an
INSERT within SELECT statement. Otherwise, the JDBC driver ignores the
parameter that sets the flag.
Restriction: You cannot prepare an SQL statement for retrieval of automatically
generated keys and use the PreparedStatement object for batch updates. The IBM
Data Server Driver for JDBC and SQLJ version 3.50 or later throws an
SQLException when you call the addBatch or executeBatch method on a
PreparedStatement object that is prepared to return automatically generated keys.
To retrieve automatically generated keys from a table using JDBC 3.0 methods, you
need to perform these steps:
1. Use one of the following methods to indicate that you want to return
automatically generated keys:
v If you plan to use the PreparedStatement.executeUpdate method to insert
rows, invoke one of these forms of the Connection.prepareStatement method
to create a PreparedStatement object:
The following form is valid for a table on any data source that supports
identity columns.
Connection.prepareStatement(sql-statement,
Statement.RETURN_GENERATED_KEYS);
The following forms are valid only if the data source supports identity
columns and the INSERT within SELECT statement. With the first form, you
specify the names of the columns for which you want automatically
generated keys. With the second form, you specify the positions in the table
of the columns for which you want automatically generated keys.
Connection.prepareStatement(sql-statement, String [] columnNames);
Connection.prepareStatement(sql-statement, int [] columnIndexes);
v If you use the Statement.executeUpdate method to insert rows, invoke one
of these forms of the Statement.executeUpdate method:
The following form is valid for a table on any data source that supports
identity columns.
Statement.executeUpdate(sql-statement, Statement.RETURN_GENERATED_KEYS);
The following forms are valid only if the data source supports identity
columns and the INSERT within SELECT statement. With the first form, you
specify the names of the columns for which you want automatically
generated keys. With the second form, you specify the positions in the table
of the columns for which you want automatically generated keys.
Statement.executeUpdate(sql-statement, String [] columnNames);
Statement.executeUpdate(sql-statement, int [] columnIndexes);
2. Invoke the PreparedStatement.getGeneratedKeys method or the
Statement.getGeneratedKeys method to retrieve a ResultSet object that
contains the automatically generated key values.
The data type of the automatically generated keys in the ResultSet is
DECIMAL, regardless of the data type of the corresponding column.
The following code creates a table with an identity column, inserts a row into the
table, and retrieves the automatically generated key value for the identity column.
The numbers to the right of selected statements correspond to the previously
described steps.
Figure 18. Example of retrieving automatically generated keys
Chapter 3. JDBC application programming
59

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents