Java Reference
In-Depth Information
Result Set
cursor
row one
row two
row three
row four
row xxx
Figure 10-8.
The cursor is moved sequentially between rows with the next method. Assuming that the ResultSet variable is
named rs, executing the next method as follows:
rs.next();
positions the cursor to the first row as seen in Figure 10-9 . Each time the next method is executed, the cursor is moved to
the next row. In addition, when the cursor is successfully moved to the next row, a Boolean value of true is returned by the
next method. If the cursor is at the last row and the next method is performed, a Boolean value of false will be returned.
This Boolean return value is very useful. For example, it can be used to determine if the result set is empty.
Result Set
cursor
row one
row two
row three
row four
row xxx
Figure 10-9.
Yes, it is possible for a select statement to return a result set with no rows. Therefore, to ensure that there
is data to retrieve, you will want to check the returned value each time the next method is executed. Also, trying
to retrieve data when the cursor is not pointing at a row, results in an SQL exception and the message " Cursor
position not valid."
Once the cursor is pointing at a valid row, there is a series of ResultSet getter methods ::to retrieve the individual
column values in the row. The programmer must use the correct getter method for the data type being retrieved and
must specify which column is being retrieved. The three getters we will work with are getString, getInt, and getDouble.
There are, of course, getters for the other data types. (A quick trip to the Oracle documentation website will show you
all the available ResultSet methods.)
 
Search WWH ::




Custom Search