Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JdbcTemplate update error when using with parameters

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!

like image 610
Thiha Zaw Avatar asked Mar 31 '26 20:03

Thiha Zaw


1 Answers

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

like image 182
Pratik Avatar answered Apr 02 '26 14:04

Pratik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!