Spring Framework

org.springframework.jdbc.core
Class ColumnMapRowMapper

java.lang.Object
  extended by org.springframework.jdbc.core.ColumnMapRowMapper
All Implemented Interfaces:
RowMapper<java.util.Map<java.lang.String,java.lang.Object>>

public class ColumnMapRowMapper
extends java.lang.Object
implements RowMapper<java.util.Map<java.lang.String,java.lang.Object>>

RowMapper implementation that creates a java.util.Map for each row, representing all columns as key-value pairs: one entry for each column, with the column name as key.

The Map implementation to use and the key to use for each column in the column Map can be customized through overriding createColumnMap(int) and getColumnKey(java.lang.String), respectively.

Note: By default, ColumnMapRowMapper will try to build a linked Map with case-insensitive keys, to preserve column order as well as allow any casing to be used for column names. This requires Commons Collections on the classpath (which will be autodetected). Else, the fallback is a standard linked HashMap, which will still preserve column order but requires the application to specify the column names in the same casing as exposed by the driver.

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

Constructor Summary
ColumnMapRowMapper()
           
 
Method Summary
protected  java.util.Map<java.lang.String,java.lang.Object> createColumnMap(int columnCount)
          Create a Map instance to be used as column map.
protected  java.lang.String getColumnKey(java.lang.String columnName)
          Determine the key to use for the given column in the column Map.
protected  java.lang.Object getColumnValue(java.sql.ResultSet rs, int index)
          Retrieve a JDBC object value for the specified column.
 java.util.Map<java.lang.String,java.lang.Object> mapRow(java.sql.ResultSet rs, int rowNum)
          Implementations must implement this method to map each row of data in the ResultSet.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ColumnMapRowMapper

public ColumnMapRowMapper()
Method Detail

mapRow

public java.util.Map<java.lang.String,java.lang.Object> mapRow(java.sql.ResultSet rs,
                                                               int rowNum)
                                                        throws java.sql.SQLException
Description copied from interface: RowMapper
Implementations must implement this method to map each row of data in the ResultSet. This method should not call next() on the ResultSet; it is only supposed to map values of the current row.

Specified by:
mapRow in interface RowMapper<java.util.Map<java.lang.String,java.lang.Object>>
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)

createColumnMap

protected java.util.Map<java.lang.String,java.lang.Object> createColumnMap(int columnCount)
Create a Map instance to be used as column map.

By default, a linked case-insensitive Map will be created.

Parameters:
columnCount - the column count, to be used as initial capacity for the Map
Returns:
the new Map instance
See Also:
LinkedCaseInsensitiveMap

getColumnKey

protected java.lang.String getColumnKey(java.lang.String columnName)
Determine the key to use for the given column in the column Map.

Parameters:
columnName - the column name as returned by the ResultSet
Returns:
the column key to use
See Also:
ResultSetMetaData.getColumnName(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.

The default implementation uses the getObject method. Additionally, this implementation includes a "hack" to get around Oracle returning a non standard object for their TIMESTAMP datatype.

Parameters:
rs - is the ResultSet holding the data
index - is the column index
Returns:
the Object returned
Throws:
java.sql.SQLException
See Also:
JdbcUtils.getResultSetValue(java.sql.ResultSet, int, java.lang.Class)

Spring Framework