英文:
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar
问题
StringBuilder myquery = new StringBuilder();
myquery.append("SELECT * from Student stu")
.append(" WHERE stu.Roll IN ('120', '140') AND stuv2.Roll is null ")
.append(" AND date(stu.admission) between ")
.append("timestamp(?)").append(" AND CURRENT DATE-30 DAY");
final Object[] args = new Object[] {
getAdm()
}; //its returning '2020-10-01 03:00:00'
int[] types = new int[] { Types.CHAR };
List<Students> result = null;
result = jdbcTemplate.query(myquery, args, types, new Mapper());
Incorrect query is being built, I am expecting value from args to replace the '?':
SELECT * from Student stu WHERE stu.Roll IN ('120', '140') AND stuv2.Roll is null
AND date(stu.admission) between timestamp(?) AND CURRENT DATE-100 DAY
I am getting the following error:
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar; nested exception is com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-418, SQLSTATE=42610, SQLERRMC=null, DRIVER=4.19.66
nested exception is com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-418, SQLSTATE=42610, 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]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:758) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at com.optum.chy.component.DB2Service.selectData(DB2Service.java:278) ~[classes/:na]
英文:
StringBuilder myquery = new StringBuilder();
myquery.append("SELECT * from Student stu")
.append(" WHERE stu.Roll IN ( '120', '140') AND stuv2.Roll is null ")
.append(" AND date(stu.admission) between ")
.append("timestamp(").append("?").append(")").append(" AND CURRENT DATE- 30 DAY");
final Object[] args = new Object[] {
getAdm()
} ; //its returning '2020-10-01 03:00:00'
int[] types = new int[] { Types.CHAR };
List<Students> result = null;
result = jdbcTemplate.query(myquery, args, types, new Mapper());
Incorrect query is being built, I am expecting value from args to replace the "?" :
SELECT * from Student stu WHERE stu.Roll IN ( '120', '140') AND stuv2.Roll is null
AND date(stu.admission) between timestamp(?) AND CURRENT DATE-100 DAY
I am getting the following error :
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar; nested exception is com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-418, SQLSTATE=42610, SQLERRMC=null, DRIVER=4.19.66
nested exception is com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-418, SQLSTATE=42610, 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]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:758) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at com.optum.chy.component.DB2Service.selectData(DB2Service.java:278) ~[classes/:na]
答案1
得分: 2
看起来错误提示显示参数未被识别,或者至少不被视为日期...
尝试将你的参数转换为日期 CAST( "parameter_here" AS DATE)
另外,我更建议使用 MapSqlParameterSource parameters = new MapSqlParameterSource();
,但这是不同的主题。
编辑补充:jarlh 指出并更正了你的查询中的错误,你有一些拼写错误需要修复。
你可以查阅这个帖子,与之类似:https://stackoverflow.com/questions/25734378/namedjdbcparametertemplate-418-sql-error-with-setting-date
英文:
Looking at the error it seems like the argument is not recognized or not as a date at least ...
Try casting your argument as a date CAST( "parameter_here" AS DATE)
Also, I'd rather use MapSqlParameterSource parameters = new MapSqlParameterSource();
, but that's different subject
Edit bis : jarlh pointed out and corrected mistakes in your query, you have some typos you should fix.
You can look into this thread , which is similar : https://stackoverflow.com/questions/25734378/namedjdbcparametertemplate-418-sql-error-with-setting-date
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论