Java Reference
In-Depth Information
JdbcTemplate is the most important class in the Spring JDBC DAO support. This class
implements the GOF Template Method design pattern. The Template Method pattern
defines the workflow or algorithm for a specific operation. It allows the subclasses to
modify certain steps without changing the algorithm's core structure. JdbcTemplate con-
solidates all the common repeated code blocks typically associated with JDBC workflow.
As I will show later, the workflow defined by JdbcTemplate can be modified at appropriate
points to provide customized processing. Listing 5-4 shows the policy detail DAO imple-
mentation class.
Listing 5-4.
package com.apress.einusre.persistence.dao.impl;
public class PolicyDetailDaoImpl extends JdbcDaoSupport implements PolicyDetailDao{
public void savePolicyDetails(String productCd,String name,int age) {
Object args [] = {productCd,name,new Integer(age)};
From the simplified code in Listing 5-4, it is clear that JdbcTemplate fosters reuse, and
this has resulted in a significant code reduction in the DAO implementation. The tight
coupling with the JDBC and collection packages (as in Listing 5-1) has been removed.
The leakage of JDBC resource is no longer a problem because JdbcTemplate methods
ensure that database resources are released in the proper sequence after they have been
used. In addition, you are not forced to handle exceptions while using Spring DAO. The
JdbcTemplate class handles the SQLException and rethrows a runtime exception because in
most cases it is not possible to recover from these database errors.
The DAO implementation will be injected into the application service used by the
session bean. Listing 5-5 shows the application service code.
Listing 5-5.
public class UnderwritingApplicationServiceImpl implements
private PolicyDetailDao policyDetailDao;
public void underwriteNewPolicy(String productCd, String name, int age) {
//business rules - here
this.policyDetailDao.savePolicyDetails(productCd, name, age);
public PolicyDetailDao getPolicyDetailDao() {
Search WWH ::

Custom Search