大家好,我想用java做一个简单的更新查询,但是一次又一次地出现同样的错误。 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 'WHERE id=1' at line 1
我不明白是什么问题,有人能解释一下我为什么会出现这个错误吗?谢谢大家!
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String name = jTextField2.getText();
int salary = Integer.parseInt(jTextField4.getText());
String WorkPosition = jTextField1.getText();
Random rand = new Random();
String Address = jTextField3.getText();
int id = Integer.parseInt(jTextField5.getText());
Employee user = new Employee(id , name ,Address, salary, WorkPosition);
try
{
// create a mysql database connection
String myDriver = "org.gjt.mm.mysql.Driver";
String myUrl = "jdbc:mysql://localhost/test";
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("JDBC:mysql://localhost:3306/demo", "root", "root");
// create a sql date object so we can use it in our INSERT statement
Calendar calendar = Calendar.getInstance();
java.sql.Date startDate = new java.sql.Date(calendar.getTime().getTime());
// the mysql insert statement
String query = "UPDATE Users SET name=?, salary=?, place=?, workposition=?, WHERE id=?";
// create the mysql insert preparedstatement
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setString (1, user.name);
preparedStmt.setInt (2, user.salary);
preparedStmt.setString (3,user.place);
preparedStmt.setString (4,user.workposition);
preparedStmt.setInt (5, user.id);
JOptionPane.showMessageDialog(null, "Record Updated! ");
// execute the preparedstatement
preparedStmt.execute();
conn.close();
}
catch (Exception e)
{
System.err.println("Got an exception!");
System.err.println(e.getMessage());
}
2条答案
按热度按时间0qx6xfy61#
看来你的语法错了。看看mysql语法指南,这将是如何做一个更新
看起来您的命令中多了一个逗号。你的应该像这样工作:
如果可行的话,那么请尝试查看mysql示例以获得更多帮助。
vltsax252#
移除最后一个,