你好,java / database附近哪里语法错误

时间:2018-11-16 19:50:45

标签: java database eclipse sqlite

我每次尝试更新时都说-在“ 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()

1 个答案:

答案 0 :(得分:1)

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

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

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