Java Reference
In-Depth Information
Table 7-6: ResultSet getter Methods
Data Type
Method
getTimestamp(String columnName)
java.sql.Timestamp
getLong(String columnName)
long
Object
getObject(String columnName)
short
getShort(String columnName)
getString(String columnName)
String
Caution
Each column can be read only once with getter method. Subsequent reads return
unpredictable results.
A
ResultSet
maintains a cursor that points to the current row of data. Initially the cursor is positioned
before the first row. The
next()
method moves the cursor to the next row and must be called before
the first
getter
method is called.
When you access data with a basic, nonscrollable
ResultSet
, the table rows are retrieved
sequentially.
ScrollableResultSets
add absolute positioning and reverse scrolling capabilities to
the basic
ResultSet
. Within a row, you can access the column values in any order.
For the "
getter
" methods, the JDBC driver attempts to convert the underlying data to the specified
Java type and returns a suitable Java value. Column names used as input to "
getter
" methods are
case insensitive, in accordance with normal SQL rules.
Caution
When performing a "
getXXX
" using a column name, if several columns have the
same name, the value of the first matching column will be returned.
A basic
ResultSet
is automatically closed by the statement that generated it when that statement is
closed, re-executed, or is used to retrieve the next result from a sequence of multiple results. This
behavior may be modified in some of the JDBC Extension API
ResultSets
, as discussed in
Chapter
4
.
ResultSetMetaData
The
ResulSetMetaData
object returned by the
getMetaData()
method provides information about
a
ResultSet
's columns, such as number, types, and properties,.
Chapter 4
discusses the
ResulSetMetaData
object in some detail.
The following are some of the methods available to access
ResultSetMetaData
:
getColumnCount()
— returns the number of columns in the ResultSet
getColumnLabel(int column)
— returns the column title for use in printouts and displays
getColumnName(int column)
— returns the column name
getColumnTypeName(int column)
- returns the name of the column's SQL data type