springjdbcjdbctemplate

Spring JdbcTemplate / NamedParameterJdbcTemplate passing null value as a parameter value


I have an issue passing a null value to NamedParameterJdbcTemplate using MapSqlParameterSource of the spring framework. Anyone knows how to do this?

Currently my code is :

String sql = "update person set project = :project where id = :id;";
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("project ", null);
params.addValue("id ", 1);
int count = newNamedParameterJDBCTemplate().update(sql, params);

This is where I get a NullPointerException.


Solution

  • This is my code on Spring 3.1

    String sql = "update user set name = :name where id = :id";
    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("name", null);
    params.addValue("id", 1);
    namedParameterJdbcTemplate.update(sql, params);
    

    works fine. Maybe a stack trace might help?