Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 745
AlexLiesenfeld Avatar asked Feb 15 '12 02:02

AlexLiesenfeld


2 Answers

Map.of() doesn't support null

String sql = "update person set project = :project where id = :id;";

// ISSUE: Map.of doesn't support null values, but HashMap does:
Map<String, Object> params = new HashMap<>();
params.put("project", null);
params.put("id", 1);
int count = newNamedParameterJDBCTemplate().update(sql, params);
like image 191
epox Avatar answered Oct 28 '22 02:10

epox


There is an extra space after parameter name:

params.addValue("project ", null);
                        ↑   
params.addValue("id ", 1);
                   ↑
like image 25
cnstntn Avatar answered Oct 28 '22 03:10

cnstntn