25.4. The ResultSet

Now that you have a good understanding of the capabilities of the Statement and PreparedStatement interfaces, it's time to dig a little deeper into the details of getting the data back from the query. In this section, you'll add to what you learned about the ResultSet object in the last chapter. You'll explore the getXXX() methods in more depth and look at some of the special SQL data types and how they are handled. You'll also look at how to use streams with a ResultSet object.

25.4.1. Retrieving Column Data for Specified Data Types

So far you've retrieved data from a resultset as type String because data of any SQL type can be retrieved in this way. As you saw briefly in the previous chapter, like the Statement and PreparedStatement interfaces, the ResultSet interface provides methods for working with a variety of data types and retrieving data as a Java type that is more consistent with the original SQL type.

Most of these methods work in a similar way and come in two overloaded forms. One form specifies the column by the column name:

xxxType resultSet.getXXX(String columnName)

The other specifies the column name by its index position, the first position index being 1:

xxxType resultSet.getXXX(int columnPosition)

The mechanics of calling these methods is quite straightforward, but to use these methods effectively, you need to understand the possible mappings between Java data types and SQL data types in both directions.

The table in Figure 25-3 illustrates ...

Get Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition 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.