Java Reference
In-Depth Information
Portability Tip 24.4
Some JDBC drivers do not support updatable
ResultSet
s. In such cases, the driver typi-
cally returns a read-only
ResultSet
. For more information, see your database driver doc-
umentation.
Common Programming Error 24.9
Attempting to update a
ResultSet
when the database driver does not support updatable
ResultSet
s causes
SQLFeatureNotSupportedException
s.
This example uses a
ResultSet
that is scrollable, insensitive to changes and read only.
Line 45 (Fig. 24.25) invokes method
setQuery
(lines 144-163) to perform the default query.
ResultSetTableModel
Method
getColumnClass
Method
getColumnClass
(lines 49-69) returns a
Class
object that represents the superclass
of all objects in a particular column. The
JTable
uses this information to configure the de-
fault cell renderer and cell editor for that column in the
JTable
. Line 58 uses
ResultSet-
MetaData
method
getColumnClassName
to obtain the fully qualified class name for the
specified column. Line 61 loads the class and returns the corresponding
Class
object. If an
exception occurs, the
catch
in lines 63-66 prints a stack trace and line 68 returns
Ob-
ject.class
—the
Class
instance that represents class
Object
—as the default type. [
Note:
Line 58 uses the argument
column + 1
. Like arrays,
JTable
row and column numbers are
counted from 0. However,
ResultSet
row and column numbers are counted from 1. Thus,
when processing
ResultSet
rows or columns for use in a
JTable
, it's necessary to add 1 to
the row or column number to manipulate the appropriate
ResultSet
row or column.]
ResultSetTableModel
Method
getColumnCount
Method
getColumnCount
(lines 72-89) returns the number of columns in the model's un-
derlying
ResultSet
. Line 81 uses
ResultSetMetaData
method
getColumnCount
to obtain
the number of columns in the
ResultSet
. If an exception occurs, the
catch
in lines 83-
86 prints a stack trace and line 88 returns 0 as the default number of columns.
ResultSetTableModel
Method
getColumnName
Method
getColumnName
(lines 92-109) returns the name of the column in the model's un-
derlying
ResultSet
. Line 101 uses
ResultSetMetaData
method
getColumnName
to obtain
the column name from the
ResultSet
. If an exception occurs, the
catch
in lines 103-106
prints a stack trace and line 108 returns the empty string as the default column name.
ResultSetTableModel
Method
getRowCount
Method
getRowCount
(lines 112-119) returns the number of rows in the model's under-
lying
ResultSet
. When method
setQuery
(lines 144-163) performs a query, it stores the
number of rows in variable
numberOfRows
.
ResultSetTableModel
Method
getValueAt
Method
getValueAt
(lines 122-141) returns the
Object
in a particular row and column of
the model's underlying
ResultSet
. Line 132 uses
ResultSet
method
absolute
to position
the
ResultSet
cursor at a specific row. Line 133 uses
ResultSet
method
getObject
to ob-
tain the
Object
in a specific column of the current row. If an exception occurs, the
catch
in
lines 135-138 prints a stack trace and line 140 returns an empty string as the default value.