Java Reference
In-Depth Information
A ResultSet is the data a SQL Query returns, consisting of all the rows that satisfy the
conditions of that query arranged in rows accessible through the ResultSet's
methods.
ResultSets are arranged as a table, with the column headings and values returned in
the order specified in the statement, satisfying the conditions of the query. For
example, if this is your query:
SELECT Name,Description,Qty,Cost FROM Stock
Your result set might look like Table 4-2 .
Table 4-2: Organization of a ResultSet
Name
Description
Qty
Cost
Steiner
10 x 50 Binoculars
10
799.95
Steiner
8 x 30 Binoculars
30
299.95
PYGMY-2
Night Vision Monocular
20
199.95
A ResultSet maintains a cursor that points to the row of data accessible through the
getter methods of the ResultSet. Each time the ResultSet.next() method is called, the
cursor moves down one row.
Initially, the cursor is positioned before the first row, so you need to call next() to set
the cursor on the first row, making it the current row. Since next()returns a boolean
true if a valid data row is available, this design approach makes for a clean while loop
for accessing row data, as shown in Listing 4-6 .
Listing 4-6: Retrieving a ResultSet
package java_databases.ch04;
import java.sql.*;
public class PrintResultSet{
public static void main(String args[]){
String query = "SELECT Name,Description,Qty,Cost FROM Stock";
PrintResultSet p = new PrintResultSet(query);
}
public PrintResultSet(String query){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection ("jdbc:odbc:Inventory");
Statement stmt = con.createStatement();
Search WWH ::




Custom Search