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
