我正在尝试创建一个函数来更新名为customers的数据库中的每个客户(lastEmailed列),并且我尝试过多次使用不同的查询输入和执行方法(更新,批量更新等)而我正在使用没运气。这是我的切碎/注释代码看起来像是乱糟糟的。任何建议将不胜感激,谢谢!此外,输入样式的格式为2014-12-09 14:20:47(datetime)。 **这是一个NamedParameterJdbcTemplate。
**这特别指的是updateTime函数,我只是包含了我的其余DAO来帮助 **我希望每次调用函数时,只为lastEmailed更新每个客户的相同值。
@Repository // creates the bean
public class CustomerDAO {
@Autowired
private NamedParameterJdbcTemplate jdbcTemplate;
private RowMapper<Customer> rowMapper = new RowMapper<Customer>() {
@Override
public Customer mapRow(ResultSet resultSet, int index) throws SQLException {
Customer customer = new Customer();
customer.setCustomerNumber(resultSet.getString("id"));
customer.setFirstName(resultSet.getString("fName"));
customer.setLastName(resultSet.getString("lName"));
customer.setEmail(resultSet.getString("email"));
customer.setStreet(resultSet.getString("street"));
customer.setCity(resultSet.getString("city"));
customer.setState(resultSet.getString("state"));
customer.setZip(resultSet.getString("zip"));
//Map Customer Fields Here
return customer;
}
};
public List<Customer> getCustomerList(){
String sql = "SELECT * FROM CUSTOMERS";
List<Customer> customerList =this.jdbcTemplate.query(sql, rowMapper);
return customerList;
}
// ** in another class?
// public String toString(){
// List<Customer> temp = this.getCustomerList();
//
// }
public void saveCustomer(Customer customer) {
String sql = "INSERT INTO CUSTOMERS " +
"(id, lname, fname, email, street,city, state,zip) VALUES (:id, :lname, :fname, :email, " +
":street, :city, :state, :zip)";
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("id", customer.getCustomerNumber());
parameters.put("lname", customer.getLastName());
parameters.put("fname", customer.getFirstName());
parameters.put("email", customer.getEmail());
parameters.put("street", customer.getStreet());
parameters.put("city", customer.getCity());
parameters.put("state", customer.getState());
parameters.put("zip", customer.getZip());
this.jdbcTemplate.update(sql, parameters);
}
public void updateTime(String timeSent){
String query = "UPDATE customers SET lastEmailed ='" + timeSent+"'";
// String query = "UPDATE customers SET lastEmailed = ?";
// String query = "INSERT INTO CUSTOMERS "
// this.jdbcTemplate.
// this.jdbcTemplate.executeppr
// this.jdbcTemplate.update
// jdbcTemplate.update("UPDATE customers SET lastEmailed=?",timeSent);
// SqlParameterSource [] parameterSource = SqlParameterSourceUtils.createBatch(this.getCustomerList().toArray());
// this.jdbcTemplate.batchUpdate("INSERT INTO CUSTOMERS (id,lastEmailed) VALUES (:customerNumber, " +timeSent + ")", parameterSource);
}
}
答案 0 :(得分:0)
我无法相信我忽略了这一点,我在之前的一个功能中几乎已经有了它,但没有意识到我只需要在地图中放置一个值。感谢所有花时间查看我的代码的人!
public void updateTime(String timeSent){
String query = "UPDATE customers SET lastEmailed ='" + timeSent+"'";
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("lastEmailed", timeSent);
this.jdbcTemplate.update(query,parameters);
}