faisal pathan
faisal pathan

Reputation: 53

JdbcTemplate throws java.sql.SQLException:

String query = "Select count(*) from product where date_added in (?)";

Object[] params = {dates}; //dates is a list of java.sql.Date

Long productCount = jdbcTemplate.queryForObject(query, params, Long.class);

Last line throws:

java.sql.SQLException: Unable to convert between java.util.ArrayList and JAVA_OBJECT while querying the database with IN clause

Already tried using NamedParameterJdbcTemplate, it was asking for quotes within the list, which can cause sql injection. To overcome that using jdbcTemplate.

Any help is appreciated.

Upvotes: 3

Views: 1675

Answers (1)

Suresh
Suresh

Reputation: 1601

You need a parameter source:

Set<Integer> ids = ...;

MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("ids", ids);

List<Foo> foo = getJdbcTemplate().query("SELECT * FROM foo WHERE a IN (:ids)", 
parameters, getRowMapper());

This only works if getJdbcTemplate() returns an instance of type NamedParameterJdbcTemplate. Also make sure you capture your result into a list.

Upvotes: 1

Related Questions