1

Everytime i try to update it says - near "WHERE":syntax error. I have successfully been able to update other parts of the classes in the program but this somehow gives me this error.

I think the problem lies here:

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()

Here's the whole code just in case;

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()
4
  • 2
    You don't need a comma before where keyword Commented Nov 16, 2018 at 19:51
  • ahh ok! thank you very much! Commented Nov 16, 2018 at 19:54
  • You're welcome :) ! Commented Nov 16, 2018 at 19:54
  • Use PreparedStatement instead of concatenating a statement together, especially if the values come from user input - otherwise you're creating a Bobby Tables problem (SQL injection vulnerability). Commented Nov 16, 2018 at 22:11

1 Answer 1

1

For sql table update, the syntax is as follows:

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

Notice that there is no comma before the where keyword. In your code, you are adding the comma before your WHERE keyword leading to the error

Sign up to request clarification or add additional context in comments.

1 Comment

Yeap i have changed accordingly and removed the comma. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.