Sunday, November 11, 2007

Statment::JAVA5

Performance Hints

The Statement interface has two methods that can be used to provide hints to a JDBC driver: setFetchDirection and setFetchSize. The values supplied to these methods are applied to each result set produced by the statement. The methods of the same name in the ResultSet interface can be used to supply hints for just that result set.
Hints provided to the driver via this interface may be ignored by the driver if they
are not appropriate. The methods getFetchDirection and getFetchSize return the current value ofthe hints. If either of these methods is called before the corresponding setter method has been called, the value returned is implementation-defined.

Retrieving Auto Generated Keys


Many database systems have a mechanism that automatically generates a unique key field when a row is inserted. The method Statement.getGeneratedKeys, which can be called to retrieve the value of such a key, returns a ResultSet object with a column for each automatically generated key. 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();
// indicate that the key generated is going to be returned
int rows = stmt.executeUpdate("INSERT INTO ORDERS " +
"(ISBN, CUSTOMERID) " +
"VALUES (195123018, ’BILLG’)",
Statement.RETURN_GENERATED_KEYS);
ResultSet rs = stmt.getGeneratedKeys();
boolean b = rs.next();
if (b == true) {
// retrieve the new key value
...
}

CODE EXAMPLE Retrieving auto generated keys Additional methods allow the ordinals or names of the columns that should be returned to be specified.

In CODE EXAMPLE the Statement method executeUpdate is called with two parameters, the first is the SQL statement to be executed, the second is an array of String containing the column name that should be returned when getGeneratedKeys is called:

String keyColumn[] = {"ORDER_ID"};
...
Statement stmt = conn.createStatement();
int rows = stmt.executeUpdate("INSERT INTO ORDERS " +
"(ISBN, CUSTOMERID) " +
"VALUES (966431502, ’BILLG’)",
keyColumn);
ResultSet rs = stmt.getGeneratedKeys();

....
CODE EXAMPLE Retrieving a named column using executeUpdate and getGeneratedKeys
See the API Specification for more details. Calling ResultSet.getMetaData on the ResultSet object returned by getGeneratedKeys will produce a ResultSetMetaData object that can be used
to determine the number, type and properties of the generated keys. In some cases, such as in an insert select statement, more than one key may be returned. The ResultSet object returned by getGeneratedKeys will contain a row for each key that a statement generated. If no keys are generated, an empty result set will be returned. The concurrency of the ResultSet object returned by getGeneratedKeys must be

CONCUR_READ_ONLY. The type of the ResultSet object must be either
TYPE_FORWARD_ONLY or TYPE_SCROLL_INSENSITIVE.
The method DatabaseMetaData.supportsGetGeneratedKeys returns true if a JDBC driver and underlying data source support the retrieval of automatically generated keys.

No comments: