Java Reference
In-Depth Information
The result of the query is returned as a
ResultSet
object. The
ResultSet
works like an iter-
ator in that it lets you access all the rows of the result that match the query. This process is
shown in
Figure 18-1
.
Figure 18-1. ResultSet illustrated
Typically, you use it like this:
while (rs.next( )) {
int i = rs.getInt(1); // or getInt("UserID");
As the comment suggests, you can retrieve elements from the
ResultSet
either by their
column index (which starts at one, unlike most Java things, which typically start at zero) or
column name. In JDBC 1, you must retrieve the values in increasing order by the order of the
SELECT (or by their column order in the database if the query is SELECT *). In JDBC 2,
you can retrieve them in any order (and, in fact, many JDBC 1 drivers don't enforce the re-
trieving of values in certain orders). If you want to learn the column names (a sort of intro-
spection), you can use a
ResultSet
's
getResultSetMetaData()
method, described in