Java Reference
In-Depth Information
TIP
The case (UPPER or lower) of the column name strings is ignored, so you could
write ID and pW and it would work fine. Some developers prefer, for example,
to use all-uppercase names of columns. We recommend using a consistent
case throughout to avoid confusing those who later have to read your code.
There is another form for each of those get XXXX () calls that takes as its
argument the column number rather than name. Since our query selected
"id, pw" , the id is column one and pw is column two, so we could have
written:
int id = rs.getInt(1);
String pw = rs.getString(2);
In addition to the get methods, ResultSet also has some boolean
methods that will help your application figure out how far in the result set
the iterator has reached: isBeforeFirst() , isFirst() , isLast() , and
isAfterLast() . There is, however, no way to tell how big the result set is
directly from this simple result set.
More complex manipulation of the ResultSet object is possible if we
create the PreparedStatement with a different method call, one that lets us
provide additional parameters to specify this more complex behavior. We
could use:
conn.prepareStatement(mySQL,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
which lets us specify a type of scrollable behavior and whether
( CONCUR_UPDATEABLE ) or not ( CONCUR_READ_ONLY ) the results set can be
updated.
Once we've built the prepared statement this way, we can move the itera-
tor forward or backward, to absolute (e.g., row 7) or relative (e.g., 3 rows back)
positions. For a good discussion of this topic, see page 257 and the following
pages in the Gallardo book.
If you're still hung up on the fact that you can't get the size, in rows, of
the result set from our first example, notice that you can now do that with this
more flexible, “scrollable” result set. To find its size before reading any data,
Search WWH ::




Custom Search