错误的SQL语法附近')'同时使用Spring和WHERE IN子句

时间:2016-06-14 18:15:10

标签: java sql-server spring jdbc jdbctemplate

我一直得到这个例外

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback;  bad SQL grammar [SELECT id, name FROM pts WHERE id IN ()]; nested exception is java.sql.SQLException: Incorrect syntax near ')'.

这是执行SQL的函数

public List<ParentPt> getParentPtsFromWorkPackage(String workPackage) {
    Set<Integer> ptNums = getParentPtNums(workPackage);

    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("ptNums", ptNums);

    String sql = "SELECT id, name FROM pts WHERE id IN (:ptNums)";

    NamedParameterJdbcTemplate namedJdbcTemplate = new NamedParameterJdbcTemplate(this.dataSource);
    List<ParentPt> pts = namedJdbcTemplate.query(sql, params, new ParentPtMapper());

    return pts;
}

我的助手功能被称为:

public Set<Integer> getParentPtNums(String workPackage) {
    String sql = "SELECT DISTINCT predecessor FROM pts WHERE RTRIM(LTRIM(work_package_name)) = ? AND predecessor IS NOT NULL";

    List<Integer> ptNums = jdbcTemplateObject.queryForList(
            sql, new Object[] {workPackage}, Integer.class);

    Set<Integer> ptNumSet = new HashSet<Integer>(ptNums);

    return ptNumSet;
}

我已经查看了How to execute IN() SQL queries with Spring's JDBCTemplate effectivly?,并且我遵循他们用来进行查询的相同约定,因此我无法确切地告诉他们出了什么问题。

0 个答案:

没有答案