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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access