ORA-00936:缺少表达式错误的SQL语法

时间:2014-08-21 13:29:25

标签: sql oracle spring-mvc java-ee

当我执行我的方法时,我在我的日志文件中收到此消息:

org.springframework.jdbc.BadSqlGrammarException:StatementCallback;错误的SQL语法[DELETE FROM DEVICES WITH DEVICEID IN([3715,3716])];嵌套异常是java.sql.SQLSyntaxErrorException:ORA-00936:缺少表达式

有人可以帮忙吗?

    public int removeDevice(ArrayList<Long> deviceId) {


    StringBuffer query = new StringBuffer("DELETE FROM DEVICES ");

    query.append("WHERE DEVICEID IN ("+deviceId+") ");


    int rowsRemoved = 0;
    try {
        rowsRemoved = getSimpleJdbcTemplate().update(query.toString());
    } catch(Exception e) {
        log.error("query: " + query.toString());

    }
    return rowsRemoved;

}

2 个答案:

答案 0 :(得分:0)

也许deviceId.toString()会返回[3715,3716] ..它必须只是3715,3716。所以改变你的逻辑以形成一个逗号分隔的字符串

query.append("WHERE DEVICEID IN ("+ deviceId.toString().replaceAll("\\[|\\]", "") +") ");

这应该有帮助!抱歉未经测试!这将删除方括号。

答案 1 :(得分:0)

试试这个:

StringBuilder args = new StringBuilder();
for (Long device : deviceId) {
    args.append(device + ",");
}

StringBuffer query = new StringBuffer("DELETE FROM DEVICES ");

query.append("WHERE DEVICEID IN ("+args.toString().substring(0, args.toString().length() - 1) +") ");