tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Spring > JDBC > Query for Map

Query for Map 

Spring JDBC Framework simplifies the use of JDBC and helps to avoid common errors. The following example shows querying for a map using JdbcTemplate class.


File Name  :  
/SpringJDBC001/conf/basic/applicationContext.xml 

File Name  :  
com/bethecoder/tutorials/spring3/tests/QueryForMapTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.spring3.tests;

import java.util.Map;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class QueryForMapTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    
    /**
     * Initialize context and get the JdbcTemplate
     */
    ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    DataSource dataSource = (DataSourceappContext.getBean("dataSource");
    JdbcTemplate template = new JdbcTemplate(dataSource);

    /**
     * The 'queryForMap' API expects a single resultant row from the query.
     */
    String statusToCheck = "RESOLVED";
    Map<String, Object> row = template.queryForMap(
        "select * from BUG_STAT where STATUS = ? ", statusToCheck);
    System.out.println(row);
    
    
    Map<String, Object> stat = template.queryForMap(
        "select count(*) as ROW_COUNT, sum(COUNT) as BUG_COUNT, " 
        "avg(COUNT) as AVG_BUG_COUNT from BUG_STAT");
    System.out.println(stat);
  }

}
   

It gives the following output,
{ID=3, STATUS=RESOLVED, COUNT=16}
{ROW_COUNT=6, BUG_COUNT=60, AVG_BUG_COUNT=10}



 
  


  
bl  br