我将此查询作为属性常量:
get.policy.list=SELECT policy FROM table WHERE policy IN (:policyList) AND state = :state
在从jdbcTemplate获取和获取列表时,我得到:
原因:java.sql.SQLSyntaxErrorException:ORA-00900:无效的SQL 声明
在try块的第一行。
@Override
public List<MyVO> areValidPolicies(String state, List<String> policyList) {
MapSqlParameterSource namedParameters = new MapSqlParameterSource().addValue("state", state)
.addValue("policyList", policyList);
List<MyVO> retrievedPolicyList = new ArrayList<MyVO>();
try{
retrievedPolicyList = myNamedJdbcTemplate.query("SELECT policy FROM table WHERE policy IN (:policyList) AND state = :state", namedParameters, new RowMapper<MyVO>() {
@Override
public MyVO mapRow(ResultSet rs, int rowNum) throws SQLException {
MyVO myVO = new MyVO();
MyVO.setPolicy(rs.getString("policy"));
return myVO;
}
});
} catch (Exception ex){
System.out.println(ex.getMessage());
ex.printStackTrace();
}
retrievedPolicyList.stream().forEach(System.out::println);
return retrievedPolicyList;
}
堆栈跟踪:
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [get.policy.list]; nested exception is java.sql.SQLSyntaxErrorException: ORA-00900: invalid SQL statement
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:231)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:649)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:684)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:711)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:761)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.query(NamedParameterJdbcTemplate.java:192)
at com.project.policyImpl.areValidPolicies(ESRepublishDaoImpl.java:56)
at com.project.policyImpl$$FastClassBySpringCGLIB$$683fad37.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
请指导我在哪里弄错了。
答案 0 :(得分:2)
table
是SQL中的保留字。我建议重命名该表以避免将来出现麻烦,但是如果这不是一种选择,则可以使用双引号("
)来对其进行转义:
retrievedPolicyList =
myNamedJdbcTemplate.query("SELECT policy FROM \"table\" WHERE policy IN (:policyList) AND state = :state", namedParameters, new RowMapper<MyVO>() {
// Here --------------------------------------^^-----^^