Spring Framework

org.springframework.jdbc.core
Class SingleColumnRowMapper<T>

java.lang.Object
  extended by org.springframework.jdbc.core.SingleColumnRowMapper<T>
All Implemented Interfaces:
RowMapper<T>
Direct Known Subclasses:
ParameterizedSingleColumnRowMapper

public class SingleColumnRowMapper<T>
extends java.lang.Object
implements RowMapper<T>

RowMapper implementation that converts a single column into a single result value per row. Expects to operate on a java.sql.ResultSet that just contains a single column.

The type of the result value for each row can be specified. The value for the single column will be extracted from the ResultSet and converted into the specified target type.

Since:
1.2
Author:
Juergen Hoeller
See Also:
JdbcTemplate.queryForList(String, Class), JdbcTemplate.queryForObject(String, Class)

Constructor Summary
SingleColumnRowMapper()
          Create a new SingleColumnRowMapper.
SingleColumnRowMapper(java.lang.Class<T> requiredType)
          Create a new SingleColumnRowMapper.
 
Method Summary
protected  java.lang.Object convertValueToRequiredType(java.lang.Object value, java.lang.Class requiredType)
          Convert the given column value to the specified required type.
protected  java.lang.Object getColumnValue(java.sql.ResultSet rs, int index)
          Retrieve a JDBC object value for the specified column, using the most appropriate value type.
protected  java.lang.Object getColumnValue(java.sql.ResultSet rs, int index, java.lang.Class requiredType)
          Retrieve a JDBC object value for the specified column.
 T mapRow(java.sql.ResultSet rs, int rowNum)
          Extract a value for the single column in the current row.
 void setRequiredType(java.lang.Class<T> requiredType)
          Set the type that each result object is expected to match.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SingleColumnRowMapper

public SingleColumnRowMapper()
Create a new SingleColumnRowMapper.

See Also:
setRequiredType(java.lang.Class)

SingleColumnRowMapper

public SingleColumnRowMapper(java.lang.Class<T> requiredType)
Create a new SingleColumnRowMapper.

Parameters:
requiredType - the type that each result object is expected to match
Method Detail

setRequiredType

public void setRequiredType(java.lang.Class<T> requiredType)
Set the type that each result object is expected to match.

If not specified, the column value will be exposed as returned by the JDBC driver.


mapRow

public T mapRow(java.sql.ResultSet rs,
                int rowNum)
         throws java.sql.SQLException
Extract a value for the single column in the current row.

Validates that there is only one column selected, then delegates to getColumnValue() and also convertValueToRequiredType, if necessary.

Specified by:
mapRow in interface RowMapper<T>
Parameters:
rs - the ResultSet to map (pre-initialized for the current row)
rowNum - the number of the current row
Returns:
the result object for the current row
Throws:
java.sql.SQLException - if a SQLException is encountered getting column values (that is, there's no need to catch SQLException)
See Also:
ResultSetMetaData.getColumnCount(), getColumnValue(java.sql.ResultSet, int, Class), convertValueToRequiredType(Object, Class)

getColumnValue

protected java.lang.Object getColumnValue(java.sql.ResultSet rs,
                                          int index,
                                          java.lang.Class requiredType)
                                   throws java.sql.SQLException
Retrieve a JDBC object value for the specified column.

The default implementation calls JdbcUtils.getResultSetValue(java.sql.ResultSet, int, Class). If no required type has been specified, this method delegates to getColumnValue(rs, index), which basically calls ResultSet.getObject(index) but applies some additional default conversion to appropriate value types.

Parameters:
rs - is the ResultSet holding the data
index - is the column index
requiredType - the type that each result object is expected to match (or null if none specified)
Returns:
the Object value
Throws:
java.sql.SQLException - in case of extraction failure
See Also:
JdbcUtils.getResultSetValue(java.sql.ResultSet, int, Class), getColumnValue(java.sql.ResultSet, int)

getColumnValue

protected java.lang.Object getColumnValue(java.sql.ResultSet rs,
                                          int index)
                                   throws java.sql.SQLException
Retrieve a JDBC object value for the specified column, using the most appropriate value type. Called if no required type has been specified.

The default implementation delegates to JdbcUtils.getResultSetValue(), which uses the ResultSet.getObject(index) method. Additionally, it includes a "hack" to get around Oracle returning a non-standard object for their TIMESTAMP datatype. See the JdbcUtils#getResultSetValue() javadoc for details.

Parameters:
rs - is the ResultSet holding the data
index - is the column index
Returns:
the Object value
Throws:
java.sql.SQLException - in case of extraction failure
See Also:
JdbcUtils.getResultSetValue(java.sql.ResultSet, int)

convertValueToRequiredType

protected java.lang.Object convertValueToRequiredType(java.lang.Object value,
                                                      java.lang.Class requiredType)
Convert the given column value to the specified required type. Only called if the extracted column value does not match already.

If the required type is String, the value will simply get stringified via toString(). In case of a Number, the value will be converted into a Number, either through number conversion or through String parsing (depending on the value type).

Parameters:
value - the column value as extracted from getColumnValue() (never null)
requiredType - the type that each result object is expected to match (never null)
Returns:
the converted value
See Also:
getColumnValue(java.sql.ResultSet, int, Class)

Spring Framework