使用JDBCTemplate执行IN()SQL查询

时间:2015-02-11 10:11:34

标签: sql spring-mvc jdbctemplate

运行以下查询时出错。我知道我需要使用'?'尽可能多的列表大小,但列表的大小不同,我不知道如何替换'?'。

public List<Object> findAllGrades(String pnr, List<String>codes) {
    return jdbcTemplate.query(
            "select CourseParticipantship.grade from [thd].[dbo].[CourseParticipantship] "
                    + "inner join thd.dbo.CourseMaterial on courseMaterial_id = CourseMaterial.id "
                    + "inner join thd.dbo.Course on course_id = Course.id and Course.code in (?)"
                    + "inner join thd.dbo.Student on student_id = Student.id "
                    + "where Student.personalNumber in (?)",new Object[] { codes, pnr }, new RowMapper() {
                public Object mapRow(ResultSet rs, int arg1) throws SQLException {
                    return rs.getObject("grade");
                }
            });
}

我收到此错误:Unable to convert between java.util.ArrayList and JAVA_OBJECT

1 个答案:

答案 0 :(得分:1)

您应该在这里使用:+ "where S.pnr in (?)"

你必须改变

+ "where S.pnr= ?",new Object[] { codes, pnr }, new RowMapper() {

 + "where S.pnr in (?)",new Object[] { codes, pnr }, new RowMapper() {