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.
/**
* Initialize context and get the JdbcTemplate
*/
ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource) appContext.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);
}