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:

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) HerongYang.com. 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?

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 Installing and Running Java DB - Derby

 Derby (Java DB) JDBC Driver

 Derby (Java DB) JDBC DataSource Objects

 Java DB (Derby) - DML Statements

Java DB (Derby) - ResultSet Objects of Queries

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

 Java DB (Derby) - PreparedStatement

 Summary of JDBC Drivers and Database Servers

 Using Connection Pool with JDBC

 Archived Tutorials

 References

 Full Version in PDF/EPUB