Section 15.5. Getting Results


15.5. Getting Results

Returning to our example, we see that we can execute the query on the Statement object and then get out the results:

 ResultSet rs = stmt.executeQuery(); // read the results while(rs.next()) {   int id = rs.getInt("id");   String pw = rs.getString("pw");   System.out.println("docText">The results of a query are returned in a ResultSet object. The easiest way to think of it is to consider it an iterator over the rows that result from the query. In its simple form it is, like an iterator, a one-pass, once-through traversal of the data. Since the result set is iterating over rows, we need to get at the individual columns of results with a further method call on the ResultSet. You can see that inside the while loop of the example.

The query was built to retrieve the columns id and pw from our table. The getInt() and getString() methods use those column names to retrieve the data from the ResultSet.

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 getXXXX() 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 iterator 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, position it afterLast(), then getrow() to get the size, then position it back to beforeFirst() to be ready to read.



    Java Application Development with Linux
    Java Application Development on Linux
    ISBN: 013143697X
    EAN: 2147483647
    Year: 2004
    Pages: 292

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net