K
- The type of the keypublic class KeyedHandler<K> extends AbstractKeyedHandler<K,Map<String,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.
ResultSetHandler
Modifier and Type | Field and Description |
---|---|
protected int |
columnIndex
The column index to retrieve key values from.
|
protected String |
columnName
The column name to retrieve key values from.
|
protected RowProcessor |
convert
The RowProcessor implementation to use when converting rows
into Objects.
|
Constructor and Description |
---|
KeyedHandler()
Creates a new instance of KeyedHandler.
|
KeyedHandler(int columnIndex)
Creates a new instance of KeyedHandler.
|
KeyedHandler(RowProcessor convert)
Creates a new instance of KeyedHandler.
|
KeyedHandler(String columnName)
Creates a new instance of KeyedHandler.
|
Modifier and Type | Method and Description |
---|---|
protected K |
createKey(ResultSet rs)
This factory method is called by
handle() to retrieve the
key value from the current ResultSet row. |
protected Map<String,Object> |
createRow(ResultSet resultSet)
This factory method is called by
handle() to store the
current ResultSet row in some object. |
createMap, handle
protected final RowProcessor convert
protected final int columnIndex
protected final String columnName
public KeyedHandler()
public KeyedHandler(int columnIndex)
columnIndex
- The values to use as keys in the Map are
retrieved from the column at this index.public KeyedHandler(RowProcessor convert)
convert
- The RowProcessor
implementation
to use when converting rows into Mapspublic KeyedHandler(String columnName)
columnName
- The values to use as keys in the Map are
retrieved from the column with this name.protected K createKey(ResultSet rs) throws SQLException
handle()
to retrieve the
key value from the current ResultSet
row. This
implementation returns ResultSet.getObject()
for the
configured key column name or index.createKey
in class AbstractKeyedHandler<K,Map<String,Object>>
rs
- ResultSet to create a key fromSQLException
- if a database access error occursClassCastException
- if the class datatype does not match the column typeprotected Map<String,Object> createRow(ResultSet resultSet) throws SQLException
handle()
to store the
current ResultSet
row in some object. This
implementation returns a Map
with case insensitive column
names as keys. Calls to map.get("COL")
and
map.get("col")
return the same value.createRow
in class AbstractKeyedHandler<K,Map<String,Object>>
resultSet
- ResultSet to create a row fromSQLException
- if a database access error occursCopyright © 2002–2023 The Apache Software Foundation. All rights reserved.