我收到以下错误:
...check the syntax that corresponds to your MySQL server version for the right syntax to use near "Item1" at line 1
这是代码的相关部分:
String e = e_id.getSelectedItem().toString();
String value1 = e;
String o = o_code.getSelectedItem().toString();
String value2 = o;
String value3 = o_credit.getText();
// String value4 = session.getText();
// String value5 = designation.getText();
// String value6 = phd_com_id.getText();
String sql = "update passes_optional set o_code='"+value2+"', o_credit='"+value3+"' where e_id='"+value1+"'";
pst = conn.prepareStatement(sql);
pst.execute();
e_id
是一个下拉列表,其值为“Item1”、“Item2”、“Item3”和“Item4”。该表包含有关 Item1 的信息。
最佳答案
那是因为对于 prepareStatement
,您必须使用 PreparedStatements
。
String sql="update passes_optional set o_code=?, o_credit=? where e_id=?";
PreparedStatement pst=conn.prepareStatement(sql);
pst.setString(1, value2);
pst.setString(2, value3);
pst.setString(3, value1);
pst.execute();
Oracle official Prepared Statements tutorial
更新
正如 JonK 所指出的,您还应该使用 pst.executeUpdate()
而不是 pst.execute()
:
String sql="update passes_optional set o_code=?, o_credit=? where e_id=?";
PreparedStatement pst=conn.prepareStatement(sql);
pst.setString(1, value2);
pst.setString(2, value3);
pst.setString(3, value1);
pst.executeUpdate();
关于java - MySQL 服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32007790/