This section describes what is a ResultSet object.
A ResultSet object represents the output table of data resulted from a SELECT query statement with following features:
The data in a ResultSet object is organized in rows and columns.
A cursor (pointer) is also maintained in a ResultSet object to identify the current data row.
A ResultSet object also contains ResultSet meta data which provides column definitions.
ResultSet objects support 3 types of scrollabilities: TYPE_FORWARD_ONLY (default), TYPE_SCROLL_INSENSITIVE, and TYPE_SCROLL_SENSITIVE.
ResultSet objects support 2 types of update capabilities: CONCUR_READ_ONLY (default) and CONCUR_UPDATABLE.
ResultSet objects support 3 types of fetch direction hints: FETCH_FORWARD (default), FETCH_REVERSE, and FETCH_UNKNOWN.
When you execute a SELECT statement with the executeQuery() method, it will return a ResultSet object to you.
Here is a Java example program that catches the ResultSet object:
/**
* DerbySelectCount.java
* Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
*/
import java.sql.*;
public class DerbySelectCount {
public static void main(String [] args) {
Connection con = null;
try {
con = DriverManager.getConnection(
"jdbc:derby://localhost/TestDB");
Statement sta = con.createStatement();
// Catch the ResultSet object
ResultSet res = sta.executeQuery(
"SELECT COUNT(*) FROM Profile");
// Move the cursor to the first row
res.next();
// Get the value from the first column of the current row
System.out.println("Number of profiles: "+res.getInt(1));
// Close the ResultSet object
res.close();
sta.close();
con.close();
} catch (Exception e) {
System.err.println("Exception: "+e.getMessage());
}
}
}
Here is the result of this program:
Number of profiles: 9
Question: How many rows and columns are there in this ResultSet object?