首页 文章

你的sql语法有错误

提问于
浏览
1

这是我的查询

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

我得到这个例外:

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

3 回答

  • 1

    订单是一个保留字 - 我不会将它用作表名,但如果你坚持使用它,只需在它周围放回蜱 . INSERT INTOorder...

  • 3

    你需要使用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

  • 0

    确保没有任何getter发送可能导致查询在语法上不正确的字符 . 如果你的getter返回一个保留字符,如下所示:

    reserved = gen-delims / sub-delims

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

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

    *然后声明将无法按预期运行 . 这就是为什么投入的卫生对任何声明都很重要的原因 .

    查看有关查询字符串中允许的未编码的更多信息:http://www.456bereastreet.com/archive/201008/what_characters_are_allowed_unencoded_in_query_strings/

相关问题