Posts

Showing posts from 2010

How to Get Map Collection using Spring Jdbc Template

I wanted to access Map collection using Spring JdbcTemplate, but the method jdbcTemplate.queryForMap(...) returns a single Map instance (i.e. Map with only one Entry). Here is how I proceeded to extract Map with multiple entries. 1. Define a MapExtracter class public class MapExtractor implements ResultSetExtractor { public Object extractData(ResultSet rs) throws SQLException, DataAccessException { Map map = new LinkedHashMap(); while (rs.next()) { Integer key = rs.getInt("id"); String value = rs.getString("name"); map.put(key, value); } return map; } } 2. Use the MapExtracter class with a simple JdbcTemplate as follows: Map map = (Map)getJdbcTemplate().query( "SELECT id, name FROM business WHERE type_id=10", new MapExtractor()); The returned map is a collection of multiple entries.