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

Retrieving Column Values with getXXX() Methods

This section describes how to use getXXX() methods to retrieve column values from ResultSet objects.

Once you have moved the cursor to a data row in a ResultSet object, you can use one of the getXXX() methods to retrieve the value of a given column. All getXXX() methods support two types of parameters to specify a column:

  • getXXX(int n) - Returns the value of column n. Column 1 is the first column in the ResultSet table.
  • getXXX(String name) - Returns the value of the column of the specified name.

JDBC supports many getXXX() methods, one for each Java data type, so that you can retrieve column values right into the desired Java data types without any further conversion. Here is a list of getXXX() methods:

  • getByte() - Returns the value of the specified column as Java byte type.
  • getShort() - Returns the value of the specified column as Java short type.
  • getInt() - Returns the value of the specified column as Java int type.
  • getLong() - Returns the value of the specified column as Java long type.
  • getFloat() - Returns the value of the specified column as Java float type.
  • getDouble() - Returns the value of the specified column as Java double type.
  • getBigDecimal() - Returns the value of the specified column as java.sql.BigDecimal type.
  • getBoolean() - Returns the value of the specified column as Java boolean type.
  • getString() - Returns the value of the specified column as Java String type.
  • getBytes() - Returns the value of the specified column as Java byte[] type.
  • getDate() - Returns the value of the specified column as java.sql.Date type.
  • getTime() - Returns the value of the specified column as java.sql.Time type.
  • getTimestamp() - Returns the value of the specified column as java.sql.Timestamp type.
  • getAsciiStream() - Returns the value of the specified column as a stream of ASCII characters.
  • getUnicodeStream() - Returns the value of the specified column as a stream of Unicode characters.
  • getBinaryStream() - Returns the value of the specified column as a stream of bytes.
  • getClob() - Returns the value of the specified column as Java Clob type.
  • getBlob() - Returns the value of the specified column as Java Blob type.
  • getArray() - Returns the value of the specified column as Java Array type.
  • getRef() - Returns the value of the specified column as Java Ref type.
  • getCharacterStream() - Returns the value of the specified column as java.io.Reader type.
  • getObject() - Returns the value of the specified column as Java Object type.

The following sample Java program shows you how to use different getXXX() methods to retrieve column values in different data types:

/**
 * DerbyGetMethods.java
 * Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
 */
import java.sql.*;
public class DerbyGetMethods {
  public static void main(String [] args) {
    Connection con = null;
    try {
      con = DriverManager.getConnection(
        "jdbc:derby://localhost/TestDB");
      Statement sta = con.createStatement(); 

// getting the data back
      ResultSet res = sta.executeQuery("SELECT * FROM Profile");
      System.out.println("List of Profiles: "); 
      while (res.next()) {
         int id = res.getInt("ID");
         String firstName = res.getString("FirstName");
         String lastName = res.getString("LastName");
         float points = res.getFloat("Point");
         Date birthDate = res.getDate("BirthDate");
         Timestamp modTime = res.getTimestamp("ModTime");
         System.out.println(
           "  "+id
           + ", "+firstName
           + ", "+lastName
           + ", "+points
           + ", "+birthDate
           + ", "+modTime);
      }
      res.close();

      sta.close();
      con.close();        
    } catch (Exception e) {
      System.err.println("Exception: "+e.getMessage());
    }
  }
}

Here is the output:

List of Profiles:
  2, Janet, Gates, 999.99, 1984-10-13, 2006-12-31 23:59:59.999
  4, 21d6, efd17, 289.4137, 1988-12-31, 2006-12-31 23:59:59.999
  6, 1002, e3873, 996.2754, 1988-12-31, 2006-12-31 23:59:59.999
  8, 11bd, 58ad0, 741.9156, 1988-12-31, 2007-04-01 23:59:59.999
  9, 1352, 17d9, 274.19855, 1988-12-31, 2007-04-01 23:59:59.999
  10, 13ba, 88356, 115.04227, 1988-12-31, 2007-04-01 23:59:59.999
  11, 1090, 3fb07, 767.21246, 1988-12-31, 2007-04-01 23:59:59.999
  12, 19c2, 8770b, 383.88382, 1988-12-31, 2007-04-01 23:59:59.999
  13, Keith, Harris, 0.0, 1980-02-29, 2007-04-01 23:59:59.999

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
Retrieving Column Values with getXXX() Methods