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 ( {
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.


  1. Wonderful information.Jdbc is used to java beans i know the basics of java beans.It is for all to clearly express the java beans.

    java training in chennai

  2. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.

    Core JAVA Training in Chennai | JAVA Training in Chennai


Post a Comment

Popular posts from this blog

Task 12 - Create Java Program with MySQL Database Connectivity

Java Interfaces and Exception Handling - Practice Problem with Solution

Task 16 - Javafx Program to Create, Retrieve, Update and Delete records with Database