【问题标题】:you have an error in your sql syntax你的 sql 语法有错误
【发布时间】:2015-09-02 19:05:31
【问题描述】:

这是我的查询

result = s.executeUpdate("INSERT INTO order " + "VALUES ('" + id.getText() + "','" + name.getText() + "', '" + code.getText() + "','" + price.getText() + "')");

我遇到了这个异常:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 你有一个 SQL 语法错误;检查与您对应的手册 MySQL 服务器版本,用于在 'order VALUES 附近使用正确的语法 (('1'),('1'),('1'),('1')' 在第 1 行

【问题讨论】:

标签: mysql jdbc


【解决方案1】:

Order 是一个保留字——我不会将它用作表名,但如果你坚持使用它,只需在它周围放回勾号。 INSERT INTO `order` ...

【讨论】:

  • “单引号”是指“反引号”。
  • MySQL 原生使用反引号。
【解决方案2】:

reserved keywords 需要使用反引号,

result = s.executeUpdate("INSERT INTO `order` " + "VALUES ('" + id.getText() + "','" + name.getText() + "', '" + code.getText() + "','" + price.getText() + "')");

您的代码也容易受到 SQL 注入的影响。所以你也需要努力。我的建议是使用 prepared statement 来避免 SQL 注入。

也不错:Preventing SQL Injection in Java

【讨论】:

    【解决方案3】:

    确保没有任何 getter 发送可能导致查询语法错误的字符。 如果您的 getter 返回一个保留字符,如下所示:

    保留 = gen-delims / sub-delims

    gen-delims = ":" / "/" / "?" /“#”/“[”/“]”/“@”

    sub-delims = "!" /“$”/“&”/“'”/“(”/“)”/“*”/“+”/“”/“;” / "="

    *那么该语句将无法按预期运行。 这就是为什么输入的卫生对于任何语句都很重要。

    详细了解查询字符串中允许的未编码: http://www.456bereastreet.com/archive/201008/what_characters_are_allowed_unencoded_in_query_strings/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-19
      • 1970-01-01
      • 2023-02-24
      • 2012-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多