O'Reilly logo

Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition by Ivor Horton

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

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

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