Java Reference
In-Depth Information
Correlated queries are executed repeatedly (once for each row of the table identified in the outer-level
query), so they can be extremely inefficient. It is frequently worthwhile to rewrite correlated queries as
joins wherever possible, though in some cases the SQL engine may be able to optimize the correlated
subquery.
The
next section
explains how the SQL queries discussed in this chapter can be used in a JDBC
application.
JDBC ResultSets
The JDBC
ResultSet
holds the data, arranged in rows and columns, returned by a query. A
ResultSet
maintains a cursor that points to the current row of data. The cursor moves down one row
each time the
next()
method is called. You access the data by sequencing through the rows and
requesting data from the columns using
getter
methods, either by column name or by column
number. In general, using the column number will be more efficient than using the column name .
Caution
Columns are numbered from 1, not from 0.
The JDBC
ResultSet
provides getter methods that convert column data from SQL data types to the
specified Java types. Each
getter
method comes in these two flavors:
getXXx(String columnName)
getXXX(int columnNumber)
For clarity only one variant is shown in the
getter
method summary in
Table 7-6
.
Table 7-6: ResultSet getter Methods
Data Type
Method
getBigDecimal(String columnName, int scale)
BigDecimal
boolean
getBoolean(String columnName)
byte
getByte(String columnName)
getBytes(String columnName)
byte[]
getDouble(String columnName)
double
getFloat(String columnName)
float
int
getInt(String columnName)
java.io.InputStream
getAsciiStream(String columnName)
getUnicodeStream(String columnName)
java.io.InputStream
getBinaryStream(String columnName)
java.io.InputStream
getDate(String columnName)
java.sql.Date
java.sql.Time
getTime(String columnName)