|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.jdbc.core.SingleColumnRowMapper
RowMapper implementation that converts a single column into a single result value per row. Expects to work on a 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.
JdbcTemplate.queryForList(String, Class)
,
JdbcTemplate.queryForObject(String, Class)
Constructor Summary | |
---|---|
SingleColumnRowMapper()
Create a new SingleColumnRowMapper. |
|
SingleColumnRowMapper(Class requiredType)
Create a new SingleColumnRowMapper. |
Method Summary | |
---|---|
protected Object |
convertValueToRequiredType(Object value,
Class requiredType)
Convert the given column value to the specified required type. |
protected Object |
getColumnValue(ResultSet rs,
int index)
Retrieve a JDBC object value for the specified column, using the most appropriate value type. |
protected Object |
getColumnValue(ResultSet rs,
int index,
Class requiredType)
Retrieve a JDBC object value for the specified column. |
Object |
mapRow(ResultSet rs,
int rowNum)
Extract a value for the single column in the current row. |
void |
setRequiredType(Class 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 |
---|
public SingleColumnRowMapper()
setRequiredType(java.lang.Class)
public SingleColumnRowMapper(Class requiredType)
requiredType
- the type that each result object is expected to matchMethod Detail |
---|
public void setRequiredType(Class requiredType)
If not specified, the column value will be exposed as returned by the JDBC driver.
public Object mapRow(ResultSet rs, int rowNum) throws SQLException
Validates that there is only one column selected,
then delegates to getColumnValue()
and also
convertValueToRequiredType
, if necessary.
rs
- the ResultSet to map (pre-initialized for the current row)rowNum
- the number of the current row
SQLException
- if a SQLException is encountered getting
column values (that is, there's no need to catch SQLException)ResultSetMetaData.getColumnCount()
,
getColumnValue(java.sql.ResultSet, int, Class)
,
convertValueToRequiredType(Object, Class)
protected Object getColumnValue(ResultSet rs, int index, Class requiredType) throws SQLException
The default implementation calls ResultSet.getString(index)
etc
for all standard value types (String, Boolean, number types, date types, etc).
It calls ResultSet.getObject(index)
else.
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.
Explicit extraction of a String is necessary to properly extract an Oracle RAW value as a String, for example. For the other given types, it is also recommendable to extract the desired types explicitly, to let the JDBC driver perform appropriate (potentially database-specific) conversion.
rs
- is the ResultSet holding the dataindex
- is the column indexrequiredType
- the type that each result object is expected to match
(or null
if none specified)
SQLException
ResultSet.getString(int)
,
ResultSet.getObject(int)
,
getColumnValue(java.sql.ResultSet, int)
protected Object getColumnValue(ResultSet rs, int index) throws SQLException
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.
rs
- is the ResultSet holding the dataindex
- is the column index
SQLException
JdbcUtils.getResultSetValue(java.sql.ResultSet, int)
protected Object convertValueToRequiredType(Object value, Class requiredType)
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).
value
- the column value as extracted from getColumnValue()
(never null
)requiredType
- the type that each result object is expected to match
(never null
)
getColumnValue(java.sql.ResultSet, int, Class)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |