Java Reference
In-Depth Information
Listing 5-12.
public class PolicyDetailDaoImpl extends JdbcDaoSupport implements PolicyDetailDao{
//other implementation methods
public List listPolicyByProductCode(String productCode) {
return this.getJdbcTemplate().queryForList
new Object[]{productCode});
The queryForList method returns a list of Map objects representing each row of the
record fetched. The keys in this map object are the same as the retrieved column names
in the result set. This is a convenient solution, but passing and retrieving a Map object
would force the code to know the keys in the Map . Hence, you might have lots of constants
declared for the keys. But all of this will change if you rename any of the columns, leading
to changes in the constant file. A better approach would be to use a callback object to
retrieve the data from the result set and return a JavaBean. This callback object must
implement the RowMapper interface, as shown in Listing 5-13.
Listing 5-13.
public class ListPolicyByProductRowMapper implements RowMapper{
public Object mapRow(ResultSet rs, int rowCount) throws SQLException {
long policyId = rs.getLong(1);
String productCode = rs.getString(2);
String name = rs.getString(3);
int age = rs.getInt(4);
PolicyDetail policyDetail = new PolicyDetail(policyId,
return policyDetail;
You should observe a few things in this row mapper callback object. The values in a
row are accessed using the positional index instead of the column name. This is more
effective in terms of performance, but changes in the position of the column in the row
will make the code vulnerable to change. You can use column names instead. The data
retrieved from the result set row is used to populate a JavaBean. Listing 5-14 shows the
Search WWH ::

Custom Search