EasyManuals Logo

IBM DB2 User Manual

IBM DB2
585 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #75 background imageLoading...
Page #75 background image
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
|
|
|
|

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the IBM DB2 and is the answer not in the manual?

IBM DB2 Specifications

General IconGeneral
DeveloperIBM
Initial Release1983
LicenseProprietary
Written inC, C++
CategoryDatabase Management System (RDBMS)
Operating SystemLinux, Windows
Programming LanguagesC, C++
Data ModelsRelational
EditionsCommunity, Standard, Advanced
Key Featureshigh availability, scalability