简体   繁体   中英

Caused by: java.sql.SQLException: Invalid column index

i am attempting to delete one record by passing 2 column as filter using Spring jdbctemplate. But i do not know what is the wrong with below code. I have mentioned exception below. i have checked in dedug, requestId and qtId values are coming.

public void deleteTxn(String sql, int requestId, int qtId) {
    try {
            jdbcTemplate.update(sql,
                    new Object[]{
                    requestId,
                    qtId
                    }); 
        } catch(Exception e) {
            //
        }
    }
}

String sql = "DELETE FROM TABLE1 WHERE COL1 = ? AND COL2 = ?";

Exception :

org.springframework.jdbc.InvalidResultSetAccessException: PreparedStatementCallback; invalid ResultSet access for SQL [DELETE FROM TABLE1 WHERE COL1 = ? AND COL2 = ?]; nested exception is java.sql.SQLException: Invalid column index at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:235) at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73) at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:660) at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:909) at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:970) at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:980)

jdbcTemplate.update has two similar method signatures:

  • update(java.lang.String sql, java.lang.Object... args)

  • update(java.lang.String sql, java.lang.Object[] args, int[] argTypes) .

In your case the first overloaded method is chosen because you didn't provide int[] argTypes , and hence your update statement has only one argument, which is new Object[]{requestId, qtId} .

The solution is simple: just write jdbcTemplate.update(sql, requestId, qtId);

Or, if you want to provide types, something like this:

jdbcTemplate.update(sql, new Object[]{requestId, qtId}, 
                         new int[]{Types.BIGINT, Types.BIGINT});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM