在spring mvc中使用delete query时出错

时间:2016-12-29 23:38:51

标签: spring model-view-controller

HTTP状态500 - 请求处理失败;嵌套异常是org.springframework.jdbc.BadSqlGrammarException:StatementCallback;错误的SQL语法[从name = sds3的用户中删除];嵌套异常是java.sql.SQLException:ORA-00904:" SDS3":无效标识符

1 个答案:

答案 0 :(得分:1)

正确的查询将是

delete from users where name = 'sds3'

请注意字符串值周围的引号。

您需要学习using prepared statements,这样可以避免该错误,即使值包含引号也能正常工作,并阻止SQL injection attacks

PreparedStatement stmt = connection.prepareStatement(
    "delete from users where name = ?");
stmt.setString(1, userName);
stmt.executeUpdate();

请注意,Spring JDBC模板确实使用预准备语句,而NamedParameterJdbcTemplate也支持命名参数。你应该使用它。