Java Reference
In-Depth Information
As shown in Listing 5-9, I have used an overloaded version of the update method to
supply a prepared statement setter. The PreparedStatementSetter is a callback interface
used by Spring JDBC to set the bind variables in the SQL being submitted to the database
for processing. Note that the SQLException thrown by the setValues method will be han-
dled and converted to a runtime exception called DataAccessException by the framework.
Listing 5-10 shows the PreparedStatementSetter implementation class.
Listing 5-10. SavePolicyPreparedStatementSetter.java
public final class SavePolicyPreparedStatementSetter
implements PreparedStatementSetter{
private String productCd;
private String name;
private int age;
public SavePolicyPreparedStatementSetter(String productCd,String name,int age){
this.productCd = productCd;
this.name = name;
this.age = age;
}
public void setValues(PreparedStatement pstmt) throws SQLException {
pstmt.setString(0, productCd);
pstmt.setString(1, productCd);
pstmt.setInt(2, age);
}
}
I will now take a look at another case where all the policies for a given product code
need to be listed. As a first step, I'll add a new method in the existing interface, as shown
in Listing 5-11.
Listing 5-11. PolicyDetailDao.java
public interface PolicyDetailDao {
//other SQL statements
String LIST_POLICY_BY_PRODUCT_SQL =
" select * from T_POLICY_DETAIL where PRODUCT_CD = ?";
//other methods
public List listPolicyByProductCode(String productCode);
}
Listing 5-12 shows the implementation of the new method.
 
Search WWH ::




Custom Search