Top Description Fields Constructors Methods
org.springframework.jdbc.core

public Class SingleColumnRowMapper<T>

extends Object
implements RowMapper<T>
Class Inheritance
All Implemented Interfaces
org.springframework.jdbc.core.RowMapper
Type Parameters
<T>
the result type
Imports
java.sql.ResultSet, .ResultSetMetaData, .SQLException, org.springframework.core.convert.ConversionService, org.springframework.core.convert.support.DefaultConversionService, org.springframework.dao.TypeMismatchDataAccessException, org.springframework.jdbc.IncorrectResultSetColumnCountException, org.springframework.jdbc.support.JdbcUtils, org.springframework.lang.Nullable, org.springframework.util.ClassUtils, .NumberUtils

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.

Authors
Juergen Hoeller, Kazuki Shimizu
Since
1.2
See Also
JdbcTemplate#queryForList(String, Class), JdbcTemplate#queryForObject(String, Class)

Field Summary

Modifier and TypeField and Description
private ConversionService
private Class<?>

Constructor Summary

AccessConstructor and Description
public
SingleColumnRowMapper()

Create a new SingleColumnRowMapper for bean-style configuration.

public
SingleColumnRowMapper(Class<T>
the type that each result object is expected to match
requiredType
)

Create a new SingleColumnRowMapper.

Method Summary

Modifier and TypeMethod and Description
protected Object

Returns:

the converted value
convertValueToRequiredType
(Object
the column value as extracted from getColumnValue() (never null)
value
,
Class<?>
the type that each result object is expected to match (never null)
requiredType
)

Convert the given column value to the specified required type.

protected Object

Returns:

the Object value
getColumnValue
(ResultSet
is the ResultSet holding the data
rs
,
int
is the column index
index
,
Class<?>
the type that each result object is expected to match (or null if none specified)
requiredType
)

Retrieve a JDBC object value for the specified column.

protected Object

Returns:

the Object value
getColumnValue
(ResultSet
is the ResultSet holding the data
rs
,
int
is the column index
index
)

Retrieve a JDBC object value for the specified column, using the most appropriate value type.

public T
mapRow(ResultSet
the ResultSet to map (pre-initialized for the current row)
rs
,
int
the number of the current row
rowNum
)

Implements org.springframework.jdbc.core.RowMapper.mapRow.

Extract a value for the single column in the current row.
public static <T> SingleColumnRowMapper<T>
newInstance(Class<T>
the type that each result object is expected to match
requiredType
)

Static factory method to create a new SingleColumnRowMapper.

public static <T> SingleColumnRowMapper<T>
newInstance(Class<T>
the type that each result object is expected to match
requiredType
,
ConversionService
the ConversionService for converting a fetched value, or null for none
conversionService
)

Static factory method to create a new SingleColumnRowMapper.

public void
setConversionService(ConversionService conversionService)

Set a ConversionService for converting a fetched value.

public void
setRequiredType(Class<T> requiredType)

Set the type that each result object is expected to match.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

conversionServiceback to summary
private ConversionService conversionService
Annotations
@Nullable
requiredTypeback to summary
private Class<?> requiredType
Annotations
@Nullable

Constructor Detail

SingleColumnRowMapperback to summary
public SingleColumnRowMapper()

Create a new SingleColumnRowMapper for bean-style configuration.

See Also
setRequiredType
SingleColumnRowMapperback to summary
public SingleColumnRowMapper(Class<T> requiredType)

Create a new SingleColumnRowMapper.

Parameters
requiredType:Class<T>

the type that each result object is expected to match

Method Detail

convertValueToRequiredTypeback to summary
protected Object convertValueToRequiredType(Object value, 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). Otherwise, the value will be converted to a required type using the ConversionService.

Parameters
value:Object

the column value as extracted from getColumnValue() (never null)

requiredType:Class<?>

the type that each result object is expected to match (never null)

Returns:Object

the converted value

Annotations
@SuppressWarnings:unchecked
@Nullable
See Also
getColumnValue(java.sql.ResultSet, int, Class)
getColumnValueback to summary
protected Object getColumnValue(ResultSet rs, int index, Class<?> requiredType) throws 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:ResultSet

is the ResultSet holding the data

index:int

is the column index

requiredType:Class<?>

the type that each result object is expected to match (or null if none specified)

Returns:Object

the Object value

Annotations
@Nullable
Exceptions
SQLException:
in case of extraction failure
See Also
org.springframework.jdbc.support.JdbcUtils#getResultSetValue(java.sql.ResultSet, int, Class), getColumnValue(java.sql.ResultSet, int)
getColumnValueback to summary
protected Object getColumnValue(ResultSet rs, int index) throws 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:ResultSet

is the ResultSet holding the data

index:int

is the column index

Returns:Object

the Object value

Annotations
@Nullable
Exceptions
SQLException:
in case of extraction failure
See Also
org.springframework.jdbc.support.JdbcUtils#getResultSetValue(java.sql.ResultSet, int)
mapRowback to summary
public T mapRow(ResultSet rs, int rowNum) throws SQLException

Implements org.springframework.jdbc.core.RowMapper.mapRow.

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.

Parameters
rs:ResultSet

Doc from org.springframework.jdbc.core.RowMapper.mapRow.

the ResultSet to map (pre-initialized for the current row)

rowNum:int

Doc from org.springframework.jdbc.core.RowMapper.mapRow.

the number of the current row

Returns:T

Doc from org.springframework.jdbc.core.RowMapper.mapRow.

the result object for the current row (may be null)

Annotations
@Override
@SuppressWarnings:unchecked
@Nullable
Exceptions
SQLException:

Doc from org.springframework.jdbc.core.RowMapper.mapRow.

if an SQLException is encountered while getting column values (that is, there's no need to catch SQLException)

See Also
java.sql.ResultSetMetaData#getColumnCount(), getColumnValue(java.sql.ResultSet, int, Class), convertValueToRequiredType(Object, Class)
newInstanceback to summary
public static <T> SingleColumnRowMapper<T> newInstance(Class<T> requiredType)

Static factory method to create a new SingleColumnRowMapper.

Parameters
requiredType:Class<T>

the type that each result object is expected to match

Since
4.1
See Also
newInstance(Class, ConversionService)
newInstanceback to summary
public static <T> SingleColumnRowMapper<T> newInstance(Class<T> requiredType, ConversionService conversionService)

Static factory method to create a new SingleColumnRowMapper.

Parameters
requiredType:Class<T>

the type that each result object is expected to match

conversionService:ConversionService

the ConversionService for converting a fetched value, or null for none

Since
5.0.4
See Also
newInstance(Class), setConversionService
setConversionServiceback to summary
public void setConversionService(ConversionService conversionService)

Set a ConversionService for converting a fetched value.

Default is the DefaultConversionService.

Since
5.0.4
See Also
DefaultConversionService#getSharedInstance
setRequiredTypeback to summary
public void setRequiredType(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.