RoyalRyt
RoyalRyt

Reputation: 25

An error of my SQL syntax

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.

Upvotes: 2

Views: 79

Answers (1)

Reimeus
Reimeus

Reputation: 159754

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(?,?,?,?,?,?,?,?,?,? )";

Upvotes: 4

Related Questions