Result Sets

As we saw in the previous chapter, the SELECT statement extracts data from a database. Here's an example which should be prefaced with the warning that columns are numbered starting with 1, not zero. That is an SQL convention that really had to be respected by Java. If we run this Java code fragment,

ResultSet result;result = statement.executeQuery( " SELECT Person.name, Person.age "                                 + "FROM Person "                                 + "WHERE Person.age = 24 " );while (result.next()) {    String p = result.getString(1);    int a = result.getInt(2);    System.out.println( p + " is " + a + " years");}

we'll get output like this:

Robert Bellamy is 24 yearsTimothy French is 24 yearsElizabeth Kramer is 24 years ...

Get Just Java™ 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.