【问题标题】:An error of my SQL syntax我的 SQL 语法错误
【发布时间】:2014-10-04 08:57:36
【问题描述】:
try {
    String sql = "Insert into eyeglass (Brand, Model, Size, Color, Type, Case, Lens, Style, Warranty, Remarks) values(?,?,?,?,?,?,?,?,?,? )";

    pst = conn.prepareStatement(sql);
    pst.setString(1, txtBrand.getText());
    pst.setString(2, txtModel.getText());
    pst.setString(3, txtSize.getText());
    pst.setString(4, txtColor.getText());
    pst.setString(5, txtType.getText());
    pst.setString(6, txtCase.getText());
    pst.setString(7, txtLens.getText());
    pst.setString(8, txtStyle.getText());
    pst.setString(9, txtWarranty.getText());
    pst.setString(10, txtRemarks.getText());
    pst.execute();
    JOptionPane.showMessageDialog(null, "Saved");
} catch (Exception e) {
    JOptionPane.showMessageDialog(null, e);
}

我收到了ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Case, Lens, Style, Warranty, Remarks) values(",",",",",",",",",")' in line 1,但我不明白为什么会这样。

【问题讨论】:

  • 你在mysql中试过同样的sql吗?例如:打印 sql,复制并在 mysql 中运行。我认为你有引号问题。单'或双"
  • 不相关...joptionpane 将如何使用“java.lang.Exception...”之类的东西 e.getMessage() 或 e.printStackTrace();

标签: java mysql sql


【解决方案1】:

CASE 是一个MySQL reserved word。转义单词或更改数据库列名,后者最好避免完全转义。

String sql = 
  Insert into eyeglass 
   (Brand, Model, Size, Color, Type, `Case`, Lens, Style, Warranty, Remarks) values(?,?,?,?,?,?,?,?,?,? )";

【讨论】:

  • 谢谢@Reimeus :) 我终于修复了错误。我真的不知道 CASE 已经是保留字了。呵呵,再次感谢:)
猜你喜欢
  • 1970-01-01
  • 2020-07-09
  • 2017-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多