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. Although this implementation uses Maps to
store row contents, subclasses are encouraged to override the
createRow() method to convert the rows into any kind of object.
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.
To avoid these type issues you could subclass KeyedHandler and override
createRow() to store rows in Java bean instances (ie. a
Person class).
This class is thread safe.
See Also: org.apache.commons.dbutils.ResultSetHandler since: DbUtils 1.1 |