Class KeyedHandler<K>
- java.lang.Object
-
- org.apache.commons.dbutils.handlers.AbstractKeyedHandler<K,java.util.Map<java.lang.String,java.lang.Object>>
-
- org.apache.commons.dbutils.handlers.KeyedHandler<K>
-
- Type Parameters:
K
- The type of the key
- All Implemented Interfaces:
ResultSetHandler<java.util.Map<K,java.util.Map<java.lang.String,java.lang.Object>>>
public class KeyedHandler<K> extends AbstractKeyedHandler<K,java.util.Map<java.lang.String,java.lang.Object>>
ResultSetHandler
implementation that returns a Map of Maps.ResultSet
rows are converted into Maps which are then stored in a Map under the given key.If you had a Person table with a primary key column called ID, you could retrieve rows from the table like this:
ResultSetHandler h = new KeyedHandler("id"); Map found = (Map) queryRunner.query("select id, name, age from person", h); Map jane = (Map) found.get(new Long(1)); // jane's id is 1 String janesName = (String) jane.get("name"); Integer janesAge = (Integer) jane.get("age");
Note that the "id" passed to KeyedHandler and "name" and "age" passed to the returned Map's get() method can be in any case. The data types returned for name and age are dependent upon how your JDBC driver converts SQL column types from the Person table into Java types. </p>This class is thread safe.
- Since:
- DbUtils 1.1
- See Also:
ResultSetHandler
-
-
Field Summary
Fields Modifier and Type Field Description protected int
columnIndex
The column index to retrieve key values from.protected java.lang.String
columnName
The column name to retrieve key values from.protected RowProcessor
convert
The RowProcessor implementation to use when converting rows into Objects.
-
Constructor Summary
Constructors Modifier Constructor Description KeyedHandler()
Creates a new instance of KeyedHandler.KeyedHandler(int columnIndex)
Creates a new instance of KeyedHandler.KeyedHandler(java.lang.String columnName)
Creates a new instance of KeyedHandler.KeyedHandler(RowProcessor convert)
Creates a new instance of KeyedHandler.private
KeyedHandler(RowProcessor convert, int columnIndex, java.lang.String columnName)
Private Helper
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected K
createKey(java.sql.ResultSet rs)
This factory method is called byhandle()
to retrieve the key value from the currentResultSet
row.protected java.util.Map<java.lang.String,java.lang.Object>
createRow(java.sql.ResultSet rs)
This factory method is called byhandle()
to store the currentResultSet
row in some object.-
Methods inherited from class org.apache.commons.dbutils.handlers.AbstractKeyedHandler
createMap, handle
-
-
-
-
Field Detail
-
convert
protected final RowProcessor convert
The RowProcessor implementation to use when converting rows into Objects.
-
columnIndex
protected final int columnIndex
The column index to retrieve key values from. Defaults to 1.
-
columnName
protected final java.lang.String columnName
The column name to retrieve key values from. Either columnName or columnIndex will be used but never both.
-
-
Constructor Detail
-
KeyedHandler
public KeyedHandler()
Creates a new instance of KeyedHandler. The value of the first column of each row will be a key in the Map.
-
KeyedHandler
public KeyedHandler(RowProcessor convert)
Creates a new instance of KeyedHandler. The value of the first column of each row will be a key in the Map.- Parameters:
convert
- TheRowProcessor
implementation to use when converting rows into Maps
-
KeyedHandler
public KeyedHandler(int columnIndex)
Creates a new instance of KeyedHandler.- Parameters:
columnIndex
- The values to use as keys in the Map are retrieved from the column at this index.
-
KeyedHandler
public KeyedHandler(java.lang.String columnName)
Creates a new instance of KeyedHandler.- Parameters:
columnName
- The values to use as keys in the Map are retrieved from the column with this name.
-
KeyedHandler
private KeyedHandler(RowProcessor convert, int columnIndex, java.lang.String columnName)
Private Helper- Parameters:
convert
- TheRowProcessor
implementation to use when converting rows into MapscolumnIndex
- The values to use as keys in the Map are retrieved from the column at this index.columnName
- The values to use as keys in the Map are retrieved from the column with this name.
-
-
Method Detail
-
createKey
protected K createKey(java.sql.ResultSet rs) throws java.sql.SQLException
This factory method is called byhandle()
to retrieve the key value from the currentResultSet
row. This implementation returnsResultSet.getObject()
for the configured key column name or index.- Specified by:
createKey
in classAbstractKeyedHandler<K,java.util.Map<java.lang.String,java.lang.Object>>
- Parameters:
rs
- ResultSet to create a key from- Returns:
- Object from the configured key column name/index
- Throws:
java.sql.SQLException
- if a database access error occursjava.lang.ClassCastException
- if the class datatype does not match the column type
-
createRow
protected java.util.Map<java.lang.String,java.lang.Object> createRow(java.sql.ResultSet rs) throws java.sql.SQLException
This factory method is called byhandle()
to store the currentResultSet
row in some object. This implementation returns aMap
with case insensitive column names as keys. Calls tomap.get("COL")
andmap.get("col")
return the same value.- Specified by:
createRow
in classAbstractKeyedHandler<K,java.util.Map<java.lang.String,java.lang.Object>>
- Parameters:
rs
- ResultSet to create a row from- Returns:
- Object typed Map containing column names to values
- Throws:
java.sql.SQLException
- if a database access error occurs
-
-