2

I'm getting an error by executing jdbc.update(sql, params) and I would like to see the sql that is executing jdbc.update but with the final values instead :param that is in the sql before execution.

Is there any method of JdbcTemplate that allows to do that?

public boolean create(User user) {
    BeanPropertySqlParameterSource params = 
        new BeanPropertySqlParameterSource(user);

    String sql = "INSERT INTO t_user (username, password, email)"
        + " VALUES (:username, :password, :email)";

    return jdbc.update(sql, params) == 1;
}

I need to get sql that is going to execute jdbcTemplate, and it is something like

INSERT INTO t_user (username, password, email)
       VALUES ("name", "password", "joe@mail.com")

I need this to check if the sql is right, because I have too many parameters to do all that by hand.

mamboking
  • 4,559
  • 23
  • 27
Joe
  • 7,749
  • 19
  • 60
  • 110

1 Answers1

0

This is how I do it in my code

String DELETE_BY_ID_QUERY = "delete from my_table where id = ?";
....
String id ="a123";

getJdbcTemplate().update(DELETE_BY_ID_QUERY, new Object[] { id });

EDIT: After reading your Edit to question

You can use the API

public int update(PreparedStatementCreator psc) throws DataAccessException

and then pass in an implementation of PreparedStatementCreator for e.g. (SimplePreparedStatementCreator) and print the sql when the PreparedStatement is created. I haven't tried this, but I think you should be able to get it.

Dhanush Gopinath
  • 5,652
  • 6
  • 37
  • 68