Java Reference
In-Depth Information
Once a query has been executed, if any data was returned, the result set data
can be processed for any of the selected column values.
Processing a Result Set from an SQL Query
Results of a query are processed on a row-by-row basis. A ResultSet object
maintains a type of pointer called a cursor to point to the current row of data.
A cursor initially is positioned before the first row. The next() method of a
ResultSet moves the cursor to the next row, returning true if the next row is valid
and false if no rows remain. Because this method returns a Boolean value, it is
used within an if() to test if any records were returned, as shown in lines 202
and 208, or within a loop to iterate through the result set, shown in line 175. A
default ResultSet object has a cursor that moves forward only, from the first row
to the last. Only a single iteration through the ResultSet is allowed.
A ResultSet object implements a number of getter, or accessor, methods for
data of various data types. These methods are used to retrieve column values
from the current row by passing the column name to the method appropriate
for the data type of the column. The ResultSet contains only column values for
those columns selected in the query. Because an asterisk was used to select all
columns in the table, values for all columns are available for each row of data
returned. The getString() method (in lines 178, 180, and 181) returns the String
value in the given row at the named column, while getBoolean() in line 182 like-
wise returns a Boolean value. Line 183 displays the original password value
entered, because the Password object stores only an encrypted value and will not
display the password value at any rate. Because no method exists to obtain an
object from the ResultSet, another method, deserializeObj(), must be created to
deserialize the Password object, reversing the process of the serializeObj()
method mentioned earlier.
Line 187 uses the getBytes() method of the ResultSet object, rs, to retrieve
the value of the column, pswd, as a byte array. This technique should not be used
with JDK 1.2.2 using the JDBC-ODBC Bridge because an SQL NULL data value
is not handled correctly. If a null value is returned for the byte array, this means
the database had no value for the Password object, which should not happen
unless the database has been corrupted. If a byte array is returned (not null),
then the byte array is deserialized into an object using the deserializeObj()
method, yet to be written. This method returns an Object which then is down-
cast to a Password object and assigned to an appropriate reference variable. It is
safe to downcast to a Password object because the deserialized object originally
was serialized from a Password object. Once a Password object is obtained, it can
be used to extract information about the Password — such as whether it auto-
matically expires, whether it is expiring now, and the number of remaining
uses — by calling the appropriate accessor methods of the Password class.
The steps on the next page enter code to process the results of a query
returned in a ResultSet object.
Search WWH ::




Custom Search