Uploaded image for project: 'Commons DbUtils'
  1. Commons DbUtils
  2. DBUTILS-6

[dbutils] Create handler to return a Map of Maps

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Duplicate
    • None
    • None
    • None
    • Operating System: All
      Platform: All

    • 32449

    Description

      Turns a result set into a map of maps. I needed it to turn a
      result set into a map of state abbreviations keys with state
      information maps as values.

      It has 2 constructors. The empty constructor assumes that there
      is going to be a column named "key" that will serve as the map
      key. There is another constructor that takes a String that is
      used to identify the key column.

      Here is the code:
      package com.appriss.justicexchange.util.dbutils;

      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.util.*;

      import org.apache.commons.dbutils.BasicRowProcessor;
      import org.apache.commons.dbutils.ResultSetHandler;
      import org.apache.commons.dbutils.RowProcessor;
      import org.apache.commons.dbutils.handlers.MapListHandler;

      /**

      • <code>ResultSetHandler</code> implementation that converts a
      • <code>ResultSet</code> into a <code>Map</code> of <code>Map</code>s.
      • This class is thread safe.
        *
      • @see org.apache.commons.dbutils.ResultSetHandler
        *
      • @author David Graham
      • @author Norris Shelton
        */
        public class MapMapHandler extends MapListHandler implements ResultSetHandler {

      private String key = "key";

      /**

      • The RowProcessor implementation to use when converting rows
      • into Maps.
        */
        private RowProcessor convert = BasicRowProcessor.instance();

      /**

      • Creates a new instance of MapMapHandler using a
      • <code>BasicRowProcessor</code> for conversion. Assumes that the key for
      • the map will be "key".
        */
        public MapMapHandler() { super(); }

      /**

      • Creates a new instance of MapMapHandler using a
      • <code>BasicRowProcessor</code> for conversion. The provided key String
      • will be the key for each map.
        */
        public MapMapHandler(String key) { super(); this.key = key; }

      /**

      • Creates a new instance of MapMapHandler.
        *
      • @param convert The <code>RowProcessor</code> implementation
      • to use when converting rows into Maps. Assumes that the key for
      • the map will be "key".
        */
        public MapMapHandler(RowProcessor convert) { super(); this.convert = convert; }

      /**

      • Converts the <code>ResultSet</code> rows into a <code>Map</code> of
      • <code>Map</code> objects.
        *
      • @return A <code>Map</code> of <code>Map</code>s, never null.
        *
      • @throws java.sql.SQLException
        *
      • @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
        */
        public Object handle(ResultSet rs) throws SQLException {

      Map results = new HashMap();

      Map temp = null;
      while (rs.next())

      { temp = convert.toMap(rs); results.put(temp.get(key), temp); }

      return results;
      }
      }

      Attachments

        Activity

          People

            Unassigned Unassigned
            norrisshelton@yahoo.com Norris Shelton
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: