JDBC Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 2.11

What Is ResultSet?

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?

Sections in This Chapter

What Is ResultSet?

ResultSet Cursor and Scrollability

ResultSet Cursor Initial Position: Before First Row

Retrieving Column Values with getXXX() Methods

ResultSet Default Type: Forward-only

Scrollable ResultSet and Moving Cursor Backward

ResultSet Objects with Update Capability

insertRow() - Inserting New Rows through ResultSet Objects

updateXXX() - Updating Column Values for Row Update or Insert

deleteRow() - Deleting Rows through ResultSet Objects

Dr. Herong Yang, updated in 2007
What Is ResultSet?