The ResultSet object provides several methods for obtaining column data for a row. All are of the form get<Type>, where <Type> is a Java™ data type. Some examples of these methods include getInt, getLong, getString, getTimestamp, and getBlob. Nearly all of these methods take a single parameter that is either the column index within the ResultSet or the column name.
ResultSet columns are numbered, starting with 1. If the column name is used and there is more than one column in the ResultSet with the same name, the first one is returned. There are some get<Type> methods that have additional parameters, such as the optional Calendar object, which can be passed to getTime, getDate, and getTimestamp. Refer to the Javadoc for the java.sql package for full details.
For get methods that return objects, the return value is null when the column in the ResultSet is null. For primitive types, null cannot be returned. In these cases, the value is 0 or false. If an application must distinguish between null, and 0 or false, the wasNull method can be used immediately after the call. This method can then determine whether the value was an actual 0 or false value, or if that value was returned because the ResultSet value was indeed null.
See Example: ResultSet interface for IBM® Developer Kit for Java for an example on how to use the ResultSet interface.
When the getMetaData method is called on a ResultSet object, the method returns a ResultSetMetaData object describing the columns of that ResultSet object. When the SQL statement being processed is unknown until runtime, the ResultSetMetaData can be used to determine what get methods should be used to retrieve the data. The following code example uses ResultSetMetaData to determine each column type in the result set:
Example: Use ResultSetMetaData to determine each column type in a result set
Note: Read the Code example disclaimer for important legal information.
ResultSet rs = stmt.executeQuery(sqlString); ResultSetMetaData rsmd = rs.getMetaData(); int colType [] = new int[rsmd.getColumnCount()]; for (int idx = 0, int col = 1; idx < colType.length; idx++, col++) colType[idx] = rsmd.getColumnType(col);
See Example: ResultSetMetaData interface for IBM Developer Kit for Java for an example of how to use the ResultSetMetaData interface.