【问题标题】:error while using delete query in spring mvc在spring mvc中使用删除查询时出错
【发布时间】:2016-12-29 23:38:50
【问题描述】:

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

【问题讨论】:

  • public int deleteuser(String user_name) { String f="delete from users where name="+user_name+"";返回模板。更新(f); }
  • 您可能想念 SDS3 周围的“”吗?

标签: spring model-view-controller


【解决方案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 也支持命名参数。你应该使用它。

【讨论】:

    猜你喜欢
    • 2016-05-18
    • 1970-01-01
    • 2015-09-28
    • 2016-05-27
    • 1970-01-01
    • 2011-05-23
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多