O'Reilly logo

Just Java™ 2 by Peter van der Linden

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required