Home > Articles

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

Table Access

Listing 10 populates the derbyDB table with two rows.

Listing 10 Table population

s.execute("insert into derbyDB values (1956,’Webster St.’)");
s.execute("insert into derbyDB values (1910,’Union St.’)");

Listing 11 retrieves the inserted data from the derbyDB table and creates an object of the ResultSet class. If you examine Listing 11, you’ll notice that it contains an embedded SQL command. The result of the SQL command is to retrieve all instances of num and addr columns from the derbyDB table and order the results by num.

Listing 11 Table data retrieval into a ResultSet object

ResultSet rs = s.executeQuery("SELECT num, addr FROM derbyDB ORDER BY num");

One way to think about the ResultSet object is as a cursor—the entity that stores the data retrieved from the database. So, you can iterate through the cursor, displaying the data as required, as illustrated in Listing 12.

Listing 12 Extracting data from a cursor

while (rs.next())
{
  System.out.println("Cursor value " + rs.getInt(1) + " " + rs.getString("addr"));
}

The code in Listing 12 produces output similar to that in Listing 13.

Listing 13 Iterating through a ResultSet object

Cursor value: 300 Lakeshore Ave.
Cursor value: 1910 Union St.

So, it’s a pretty straightforward process to get data into and then out of a Derby database. Shutting down the database is also easy enough.

  • + Share This
  • 🔖 Save To Your Account