简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM