import org.springframework.jdbc.object.SqlUpdate;
public class UpdateContact extends SqlUpdate {
private static final String SQL_UPDATE_CONTACT =
"update contact set first_name=:first_name, last_name=:last_name,
birth_date=:birth_date where id=:id";
public UpdateContact(DataSource dataSource) {
super(dataSource, SQL_UPDATE_CONTACT);
super.declareParameter(new SqlParameter("first_name", Types.VARCHAR));
super.declareParameter(new SqlParameter("last_name", Types.VARCHAR));
super.declareParameter(new SqlParameter("birth_date", Types.DATE));
super.declareParameter(new SqlParameter("id", Types.INTEGER));
}
}
Listing 8-41 should be familiar to you now. An instance of SqlUpdate class is constructed with the
query, and the named parameters are declared too.
Listing 8-42 shows the implementation of the update() method in the JdbcContactDao class.
Listing 8-42. Using SqlUpdate
package com.apress.prospring3.ch8.dao.jdbc.annotation;
// Import statements omitted
@Repository("contactDao")
public class JdbcContactDao implements ContactDao {
private UpdateContact updateContact;
@Resource(name="dataSource")
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
selectAllContacts = new SelectAllContacts(dataSource);
selectContactByFirstName = new SelectContactByFirstName(dataSource);
updateContact = new UpdateContact(dataSource);
}
public void update(Contact contact) {
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("first_name", contact.getFirstName());
paramMap.put("last_name", contact.getLastName());
paramMap.put("birth_date", contact.getBirthDate());
paramMap.put("id", contact.getId());
updateContact.updateByNamedParam(paramMap);
log.info("Existing contact updated with id: " + contact.getId());
}
// Other code omitted
}
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home