I'm coding in SpringMVC database application. When I try to insert data to database with JdbcTemplate object update method with parameter and I go error while inserting data. It was fine with out parameter coding as following:
String sql = "INSERT INTO contact (name, email, address, telephone)"
+ " VALUES ('" + contact.getName() + "', '" + contact.getEmail() + "', '"
+ contact.getAddress() + "', '" + contact.getTelephone() + "')";
jdbcTemplate.update(sql);
But when I use with parameters, it got error:
String sql = "INSERT INTO contact (name, email, address, telephone)"
+ " VALUES (?, ?, ?, ?)";
jdbcTemplate.update(sql, contact.getName(), contact.getEmail(),
contact.getAddress(), contact.getTelephone());
Please help me!
You have to menditoned parameters. Check below exampls.
//insert with named parameter
public void insertNamedParameter(Customer customer){
String sql = "INSERT INTO CUSTOMER " +
"(CUST_ID, NAME, AGE) VALUES (:custId, :name, :age)";
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("custId", customer.getCustId());
parameters.put("name", customer.getName());
parameters.put("age", customer.getAge());
getSimpleJdbcTemplate().update(sql, parameters);
}
you can refere below link for reference.
Spring Named Parameters examples in SimpleJdbcTemplate
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With