java 使用Spring NamedParameterJdbcTemplate,我如何更新数据库中每个客户的列?

p8ekf7hl  于 2022-10-30  发布在  Java
关注(0)|答案(1)|浏览(128)

我正在尝试创建一个函数来更新名为customers的数据库中的每个客户(lastEmailed列),并且我已经多次尝试使用不同的查询输入和执行方法(更新,批更新等),我没有运气。这是我的切碎/注解代码看起来像,在混乱之后。任何建议都将非常感谢,谢谢!还有,输入样式的格式为2014-12-09 14:20:47(日期时间)。**这是一个命名参数JdbcTemplate。

这特别指的是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);

}
}
2wnc66cl

2wnc66cl1#

我真不敢相信我竟然忽略了这一点,我在以前的一个函数中几乎就有了它,但是我没有意识到我只需要在map中放入一个值。感谢大家花时间看我的代码!

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);
 }

相关问题