2
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);
}

I'm getting 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 and I don't understand why it happens.

2
  • 3
    Have you tried the same sql in mysql? Ex.: print the sql, copy it and run it in mysql. I think you have quotes problem. single ' or double ". Commented Oct 4, 2014 at 9:01
  • unrelated...the joptionpane will how something like "java.lang.Exception...." use e.getMessage() or e.printStackTrace(); Commented Oct 4, 2014 at 9:22

1 Answer 1

4

CASE is a MySQL reserved word. Either escape the word or alter the database column name, the latter being preferable to avoid escaping completely.

String sql = 
  Insert into eyeglass 
   (Brand, Model, Size, Color, Type, `Case`, Lens, Style, Warranty, Remarks) values(?,?,?,?,?,?,?,?,?,? )";
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Reimeus :) I finally fixed the error. I really don't know that CASE was already a reserved word. Hehehe, thanks a lot again :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.