Retrieving Automatically Generated Keys
Many DBMSs automatically generate a unique key field when a new row is inserted into a table. Methods and constants added in the JDBC 3.0 API make it possible to retrieve these keys, which is a two-step process. First the driver is alerted that it should make the keys available for retrieval. The second step is to access the generated keys by calling the
Statement method getGeneratedKey s. The rest of this section explains these two steps more fully. - Step One - Tell the driver that it should make automatically generated keys available for retrieval. This is done when an SQL statement is sent to the DBMS, which for
Statementobjects, is when the statement is executed. Three new versions of the methodexecuteUpdateand three new versions of the methodexecutesignal the driver about making automatically generated keys available. These six new methods take two parameters, the first being in all cases an SQLINSERTstatement. The second parameter is either a constant indicating whether to make all generated keys retrievable (Statement.RETURorN_GENERATED_KEY S Statement.NO_GE) or an array indicating which specific key columns should be made retrievable. The array elements are either the indexes of the columns to be returned or the names of the columns to be returned.NERATED_KEYS
Note that although it is possible to use the methodexecutefor executing a DML (Data Manipulation Language) statement, this method is generally reserved for executingCallableStatemeobjects that produce multiple return values.nt
- For a
PreparedStatemeobject, the SQL statement is sent to the DBMS to be precompiled when thent PreparedStatemeobject is created with one of thent Connection.prepmethods. Thus, the driver is notified about making automatically generated keys retrievable via these methods.areStatement
- For a
- Step Two - After the driver has been notified about making automatically generated keys available for retrieval, the keys can be retrieved by calling the
StatementmethodgetGeneratedKey. This method returns as ResultSetobject, with each row being a generated key. If there are no automaticallty generated keys, theResultSetobject will be empty.
The following code fragment creates aStatementobject and signals the driver that it should be able to return any keys that are automatically generated as a result of executing the statement. The example then retrieves the keys that were generated and prints them out. If there are no generated keys, the printout says that there are none.
String sql = "INSERT INTO AUTHORS (LAST, FIRST, HOME) VALUES " + "'PARKER', 'DOROTHY', 'USA', keyColumn"; int rows = stmt.executeUpdate(sql, Statement.RETUR N_GENERATED_KEY S); ResultSet rs = stmt.getGenerat edKeys(); int rows = stmt.executeUpd ate(sql, Statement.RETUR N_GENERATED_KEY S); ResultSet rs = stmt.getGenerat edKeys(); if (rs.next()) { ResultSetMetaDa ta rsmd = rs.getMetaData( ); int colCount = rsmd.getColumnC ount(); do { for (int i = 1; i <= colCount; i++) { String key = rs.getString(i) ; System.out.prin tln("key " + i + "is " + key); } } while (rs.next();) } else
{ System.out.prin tln("There are no generated keys."); }
Instead of telling the driver to make all automatically-g enerated keys available, it is possible to tell the driver to make particular columns retrievable. The following code fragment uses an array of column indexes (in this case, an array with one element) to indicate which columns with an automatically-g enerated key should be made available for retrieval.
String sql = "INSERT INTO AUTHORS (LAST, FIRST, HOME) VALUES " +
"'PARKER', 'DOROTHY', 'USA', keyColumn";
int [] indexes = {4};
int rows = stmt.executeUpd ate(sql, indexes);
The following code fragment shows a third alternative-sup plying an array of column names to indicate which enerated key in the column
ResultSet columns to make available. In this case, the driver is told to make the automatically-gAUTHOR_ID retrievable. String sql = "INSERT INTO AUTHORS (LAST, FIRST, HOME) VALUES " +
"'PARKER', 'DOROTHY', 'USA', keyColumn";
String [] keyColumn = {"AUTHOR_ID"};
int rows = stmt.executeUpd ate(sql, keyColumn);
The above has been taken from http://java.sun
1 comments:
How about using http://code.google.com/p/syntaxhighlighter/ to highlight your code.
Post a Comment