gpt4 book ai didi

java - 你好,java/database near where syntax error

转载 作者:搜寻专家 更新时间:2023-10-30 23:28:16 26 4
gpt4 key购买 nike

每次我尝试更新它时都会说 - 在“WHERE”附近:语法错误。我已经成功地能够更新程序中类的其他部分,但这不知何故给了我这个错误。

我认为问题出在这里:

public void update_account(){
try { //start or try
//1)create a connection variable
Connection con;
//2)create an instance of the database class
Database db=new Database();
//3)pass the connection from DB to con
con=db.open_connection();
//4)create a statement variable to prepare the SQL
Statement statement=con.createStatement();
//5)create a query to insert the records
String query="UPDATE tblUsers SET fullname='" + txtFullname.getText()+"',"
+ "username='" + txtUsername.getText()+"',"
+ "password='" + txtPassword.getText()+"',"
+ "WHERE userID="+ accid +"";
//6) execute the SQL code
if(statement.executeUpdate(query)==1) { //query was successful
JOptionPane.showMessageDialog(null, "Reference successfully updated!");
//clear the inputs
new MainInterface(user);
frmAccountSett.dispose();

}
}//end of try
catch (Exception e){//start of catch
//display the error
JOptionPane.showMessageDialog(null,e.getMessage());
}//end of catch
}//end of save_recipe()

为了以防万一,下面是完整的代码;

public void update_account(){
try { //start or try
//1)create a connection variable
Connection con;
//2)create an instance of the database class
Database db=new Database();
//3)pass the connection from DB to con
con=db.open_connection();
//4)create a statement variable to prepare the SQL
Statement statement=con.createStatement();
//5)create a query to insert the records
String query="UPDATE tblUsers SET fullname='" + txtFullname.getText()+"',"
+ "username='" + txtUsername.getText()+"',"
+ "password='" + txtPassword.getText()+"',"
+ "WHERE userID="+ accid +"";
//6) execute the SQL code
if(statement.executeUpdate(query)==1) { //query was successful
JOptionPane.showMessageDialog(null, "Reference successfully updated!");
//clear the inputs
new MainInterface(user);
frmAccountSett.dispose();

}
}//end of try
catch (Exception e){//start of catch
//display the error
JOptionPane.showMessageDialog(null,e.getMessage());
}//end of catch
}//end of save_recipe()

最佳答案

对于sql表更新,语法如下:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

请注意,where 关键字前没有逗号。在您的代码中,您在导致错误的 WHERE 关键字之前添加了逗号

关于java - 你好,java/database near where syntax error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53344472/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com