Target types for inline methods that query databases: Map objects

You can store the results of a query in a java.util.Map<String, Object> object.

When more than one column or expression appears in a SELECT statement's select-list, there might be no natural transformation of the columns to any existing class. In such a situation, you must to choose between creating a special-use class or using a general purpose class such as java.util.Map.

Example

For example, assume a report is needed giving the average number of years of formal education (EDLEVEL) for each department (WORKDEPT) having more than three employees. A List of Map objects, having keys of average_edlevel and workdept would result from application logic like the following:

Connection con = DriverManager.getConnection(...);
Data db = DataFactory.getData(con);
List<Map<String, Object>> edLevelReport = db.queryList(
 "SELECT AVG(EDLEVEL) AS AVERAGE_EDLEVEL, WORKDEPT FROM " +
 " SAMPLE_SCHEMA.EMP GROUP BY WORKDEPT HAVING COUNT(*) > 3"
  );

The above uses an invocation of the method queryList, defined as public List<Map<String, Object>> queryList(String sql, Object... parameters)

Each row from the query's query result becomes a Map. The List of all the Map objects contains the entire query result. Note that the keys of the map are created using lowercase names.


Feedback