Java Reference
In-Depth Information
// Iterate over the ResultSet
while ( rs.next() ) {
// get the title_name, which is a String
System.err.println(“Title Name = “ + rs.getString(“title_name”));
// get the rating
System.err.println(“Title Rating = “ + rs.getString(“rating”));
// get the price
System.err.println(“Title Price = “ + rs.getString(“price”));
// get the quantity
System.err.println(“Title Quantity = “ + rs.getString(“quantity”)
+ “\n”);
}
// Close the ResultSet
rs.close();
The first thing you do is call the
ResultSet.next()
method. This method returns a Boolean
value, indicating whether the next row in the set is valid. If it is, you can access that row using
the
Get
accessors provided by the
ResultSet
object. In our example, you use only the
getString()
method; but the
Get
accessors all function the same except for their return type.
They take a string value representing the name of the column in the table and return the type
that is part of their method name. For example,
getString()
returns a
java.lang.String
and
getInt()
returns an
int
. You can continue iterating over the
ResultSet
until
next()
returns
false
; at that point you need to close the
ResultSet
object. When you execute this applica-
tion, the results will be similar to the following output:
Title Name = The Adventures of Buckaroo Banzai
Title Rating = PG
Title Price = 19.95
Title Quantity = 10
Title Name = Saving Private Ryan
Title Rating = R
Title Price = 19.95
Title Quantity = 12
Title Name = So I Married An Axe Murderer
Title Rating = PG
Title Price = 19.95
Title Quantity = 15
Title Name = Happy Gilmore
Title Rating = PG
Title Price = 19.95
Title Quantity = 9