Java Reference
In-Depth Information
public class CustomerDaoImpl extends JdbcTemplate implements CustomerDao {
private static final String BY_ATTRIBUTES =
"select * from customer where firstName = ? " +
"and lastName = ? and zip = ?";
@SuppressWarnings("unchecked")
public Customer getCustomerByNameAndZip(String firstName, String lastName, String zip) {
List<Customer> customers = query(BY_ATTRIBUTES,
new Object []{
firstName,
lastName,
zip},
new RowMapper() {
public Object mapRow(ResultSet rs, int arg1) throws SQLException {
Customer result = new Customer();
result.setFirstName(rs.getString("firstName"));
result.setLastName(rs.getString("lastName"));
result.setAddress(rs.getString("address"));
result.setCity(rs.getString("city"));
result.setState(rs.getString("state"));
result.setZip(rs.getString("zip"));
result.setId(rs.getLong("id"));
return result;
}});
if(customers != null && customers.size() > 0) {
return customers.get(0);
} else {
return null;
}
}
}
The CustomerDao queries the Customer table via first name, last name, and ZIP code to find the
customer you received. From there, it uses Spring's RowMapper to create a new Customer object
containing the results of the query. Although you probably don't need the full object returned in this
scenario, since the Customer object is not a very large object, passing the entire object back allows this
method to be a little more reusable.
The AccountExecutiveDao is the other DAO you are using and is listed in Listing 8-20.
Listing 8-20. AccountExecutiveDao
package com.apress.springbatch.chapter8;
import java.sql.ResultSet;
import java.sql.SQLException;
 
Search WWH ::




Custom Search