Spring JDBCTemplate抛出BadSqlGrammarException

时间:2015-10-20 16:10:43

标签: java jdbc spring-jdbc jdbctemplate

我有一个使用Spring JDBCTemplate执行的SQL查询,以从Oracle DB中检索数据。执行语句时,会抛出org.springframework.jdbc.BadSqlGrammarException

在SQL Plus控制台中运行时,相同的SQL语句会返回正确的结果。

以下是我要执行的代码:

String query = "SELECT DISTINCT O.ORGID FROM ORGANISATIONS O WHERE O.ORGTYPE NOT IN ('P','G') AND O.USERID = 40";

ArrayList<Organisations> orgsList = new  ArrayList<Organisations>();
Object[] paramsArray = new Object[]{};

orgsList = (ArrayList<Organisations>) getJdbcTemplate().query(query.toString(), paramsArray ,
            new RowMapper<Organisations>(){
        public MetricsRoleMap mapRow(ResultSet rs, int rowNum) throws SQLException {
            // Data retrieval code;
        };
    });

以下是生成的错误:

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [ SELECT DISTINCT O.ORGID FROM ORGANISATIONS O WHERE O.ORGTYPE NOT IN ('P','G') AND O.USERID = 40]; nested exception is java.sql.SQLException: Invalid column name
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:231) [spring-jdbc-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73) [spring-jdbc-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:660) [spring-jdbc-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:695) [spring-jdbc-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:727) [spring-jdbc-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:737) [spring-jdbc-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:787) [spring-jdbc-4.0.5.RELEASE.jar:4.0.5.RELEASE]

我为另一种方法编写的类似代码效果很好。我认为这里有一个小错误,但我无法找到它。

0 个答案:

没有答案