Spring JDBC Framework simplifies the use of JDBC and helps to avoid common errors.
The following example shows querying for an integer 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);
int rowCount = template.queryForInt("select count(*) from BUG_STAT");
System.out.println("Row count : " + rowCount);
int totalBugCount = template.queryForInt("select sum(COUNT) from BUG_STAT");
System.out.println("Total bug count : " + totalBugCount);
String statusToCheck = "RESOLVED"; int bugCount = template.queryForInt(
"select COUNT from BUG_STAT where STATUS = ? ", statusToCheck);
System.out.println(statusToCheck + " bug count : " + bugCount);
}