Java 转换到日期字段时出错:org.springframework.jdbc.badsqlgramarexception:PreparedStatementCallback;糟糕的SQL语法

Java 转换到日期字段时出错:org.springframework.jdbc.badsqlgramarexception:PreparedStatementCallback;糟糕的SQL语法,java,sql,db2,jdbctemplate,Java,Sql,Db2,Jdbctemplate,我需要如何发送字段的值,以便正确地对其进行浇铸。目前,我正在发送日期类型字段,带有值->2020-10-10 以下是查询: query.append("SELECT * from STUDENT where") .append(" CAST(STUDENT.ADM_DTTM AS DATE) BETWEEN") .append(" CAST(&quo

我需要如何发送字段的值,以便正确地对其进行浇铸。目前,我正在发送日期类型字段,带有值->2020-10-10
以下是查询:

                 query.append("SELECT * from STUDENT where")
                .append(" CAST(STUDENT.ADM_DTTM AS DATE) BETWEEN")
                .append(" CAST(")
                .append("?")
                .append(" AS DATE) AND")
                .append(" CURRENT_DATE-5 DAY");

             final Date[] args = new Date[]{getMyDate()};
             int[] types = new int[]{Types.DATE};
             result = jdbcTemplate.query(query, args, types, new Mapper());
以下是详细的stacktrace:

nested exception is com.ibm.db2.jcc.am.SqlException: DB2 SQL Error: SQLCODE=-313, SQLSTATE=07001, SQLERRMC=null, DRIVER=4.19.66
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:101) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1444) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:632) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:669) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:700) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:706) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
编辑1:添加传递的参数以匹配查询中的参数标记数

 final Date[] args = new Date[]{
            getMyDate(),
            getMyDate(),
            getMyDate(),
            getMyDate()
    };
    int[] types = new int[]{Types.DATE, Types.DATE, Types.DATE, Types.DATE};
错误:

nested exception is com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-401, SQLSTATE=42818, SQLERRMC=null, DRIVER=4.19.66
at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:93) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1444) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:632) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:669) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:700) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:706) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]

实际上,在DB2查询中根本不需要任何强制转换:

SELECT *
FROM STUDENT
WHERE ADM_DTTM >= ? AND ADM_DTTM < CURRENT_DATE - 5 DAY;
选择*
来自学生
其中ADM_DTTM>=?ADM_DTTM<当前日期-5天;
这样做的原因是,首先,范围比较的下限是一个有效的DB2日期文本。这不需要任何铸造来表示日期。其次,由于ADM\u DTTM是一个日期时间(大概),我们不需要强制转换它。如果它将作为datetime传递,则在强制转换到某个日期时也会传递


我省略了JDBC模板查询的实际更新代码。假设那里的语法已经正确,上面的查询应该不会给您任何错误。

SQLCODE=-313非常具体。在文档中查找SQL0313N。这意味着为绑定语句提供的值太多或太少。这通常是一个编程错误。谢谢@mao,我的查询中有4个位置有where子句,我是否需要传递它4次,我应该如何准确传递,请帮助。为运行查询提供的参数数量必须与查询中的参数标记数量完全匹配。即使每次都是相同的日期值。谢谢@mao,我更新了代码,但仍然有错误。请看一看,如果你改变了问题(带有新的症状),那么它就是一个移动的目标。新症状=>新问题。请学习如何在DB2KnowledgeCentreFreeOnline中查找错误消息(负的SQLCODE值),并根据那里记录的建议采取行动。现在,通过将参数的数量与参数标记的数量相匹配,可以解决原始症状。接下来,还必须匹配数据类型。