I want to excute the following select statement
SELECT array_to_json(array_agg(row_to_json(opt)))
FROM (
    SELECT * FROM option WHERE optionid IN (:tags)
)opt
via
MapSqlParameterSource params = new MapSqlParameterSource();
Integer[] a={2200,23};
params.addValue("tags", Arrays.asList(a));
getSimpleJdbcTemplate().queryForObject(statement, String.class, params);
But I always run into the error:
class org.springframework.dao.InvalidDataAccessApiUsageException No value supplied for the SQL parameter 'tags': No value registered for key 'tags'
Same if I use:
Integer[] a = { 2200, 23 };
params.addValue("tags", Arrays.asList(a));
namedParameterJdbcTemplate.queryForObject(statement, params, String.class);
                The API javadoc states that you must
Pass in the values as a java.util.List of primitive objects
http://docs.spring.io/spring/docs/3.1.0.RELEASE/reference/html/jdbc.html#jdbc-in-clause
And you are using an array. You can convert the array to List using Arrays.asList
Also, I think that you need a NamedParameterJdbcTemplate instead of a SimpleJdbcTemplate for that query.
EDITED:
I've used this example project to test a solution http://www.tutorialspoint.com/spring/spring_jdbc_example.htm
This is the table used in that example
CREATE TABLE Student(
   ID   INT NOT NULL AUTO_INCREMENT,
   NAME VARCHAR(20) NOT NULL,
   AGE  INT NOT NULL,
   PRIMARY KEY (ID)
);
These are the records inserted in the DB
------Listing Multiple Records--------
ID : 1, Name : Zara, Age : 11
ID : 2, Name : Nuha, Age : 2
ID : 3, Name : Ayan, Age : 15
In class StudentJDBCTemplate, I've instantiated a NamedParameterJdbcTemplate and used it in a new method listStudentsNames
   public void setDataSource(DataSource dataSource) {
       this.dataSource = dataSource;
       this.jdbcTemplateObject = new JdbcTemplate(dataSource);
       this.namedJdbcTemplateObject = new NamedParameterJdbcTemplate(dataSource);
   }
   public List<String> listStudentsNames() {
       String SQL = "select name from Student where id IN (:tags)";
       Integer[] intArray = {1, 2, 3};
       List<Integer> intList = Arrays.asList(intArray);
       MapSqlParameterSource params = new MapSqlParameterSource();
       params.addValue("tags", intList);
       return namedJdbcTemplateObject.queryForList(SQL, params, String.class);
    }
I've also changed MainApp to invoke that new method and write the obtained results
public class MainApp {
     public static void main(String[] args) {
         ApplicationContext context =
         new ClassPathXmlApplicationContext("Beans.xml");
         StudentJDBCTemplate studentJDBCTemplate =
  (StudentJDBCTemplate) context.getBean("studentJDBCTemplate");
         System.out.println("------Listing Multiple Records--------" );
         List<String> studentsName = studentJDBCTemplate.listStudentsNames();
         for (String name : studentsName) {
              System.out.println("Name : " + name);
         }
     }
 }
If you run MainApp, the obtained results are

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With