Home > Articles > Data

  • Print
  • + Share This
Like this article? We recommend

Retrieving Data from a ResultSet

It’s a simple matter getting the data from the Resultset object, as illustrated in Listing 4.

Listing 4 Looping Through a ResultSet object

while (rs.next())
for (int loop = 1; loop <= numColumns; loop++)
System.out.println("Column value: " + String)(rs.getString(loop)));

In Listing 4, I’m being a little lazy because I assume that all the items in the rs object are of type String.

The type of program output you should expect to see is illustrated in Listing 5.

Listing 5 The complete program output

EmbeddedSchema starting in embedded mode.
Loaded the appropriate driver.
Connected to and created database derbyDB
Created table ASSEMBLY_TABLE
Column value: COMPUTER
Column value: VENDOR1
Column value: ENTRY_LEVEL
Column value: 3
Column value: COMPUTER1
Column value: VENDOR2
Column value: ENTRY_LEVEL
Column value: 2
Column value: 1
Column value: MONITOR_VENDOR1
Column value: KEYBOARD_VENDOR3
Column value: 2
Column value: MONITOR_VENDOR3
Column value: KEYBOARD_VENDOR2
Dropped table ASSEMBLY_TABLE
Closed result set and statement
Committed transaction and closed connection
Database shut down normally

It is possible to extract type-specific data from a ResultSet (for example, by using the method rs.getInt(). The way I did it here is to just assume that all columns are Strings.

This can make it a little easier to retrieve data because the only requirement is to know in advance the number of columns to expect.

  • + Share This
  • 🔖 Save To Your Account