0

I'm getting the message

"MySQLSyntaxErrorExcetpion You have an error in your SQL syntax."

I'm following a tutorial online and I don't see what's anything different with my code and the code I'm following. Can anyone point out where I went wrong?

Let me know if additional information is needed.

Screenshot

btn_update.addActionListener(new ActionListener(){


            @Override
            public void actionPerformed(ActionEvent e) {

                try{
                    theQuery("update users set fname = '" + firstNameField.getText() + "', lname = '" + lastNameField.getText() +"', age=" + ageField.getText() + "where id = " + idField.getText());
                }
                catch(Exception ex)
                {
                    System.out.println(ex);
                    }

            }
        });
2
  • 2
    You've missed a space character before "where" Commented Jan 6, 2017 at 20:48
  • 4
    Mandatory comment: This code is wide open to SQL-injection attacks. Consider using a PreparedStatement instead. Commented Jan 6, 2017 at 20:50

3 Answers 3

1

because you don't have spaces here:

age=" + ageField.getText() + "where id = " + idField.getText());

You need to change it to

age= " + ageField.getText() + " where id = " + idField.getText());

I advice to use PreparedStatement instead of the native way

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

1 Comment

I was so close! lol I'll try prepared statement next time thanks!
0

Add a space in front of " where id = ". That should work

Comments

0

Use toString() function to convert values to string and then pass them to query, as follow:

firstNameField.getText().toString()

Comments

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.