Spring JdbcTemplate / NamedParameterJdbcTemplate passing null value as a parameter value

AlexLiesenfeld picture AlexLiesenfeld · Feb 15, 2012 · Viewed 37.4k times · Source

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.

Answer

Titi Wangsa Bin Damhore picture Titi Wangsa Bin Damhore · Feb 15, 2012

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?