1

I tried to run preparedStatement ps to check whether the data is existed in database or not. Then it will run update if existed or insert if not existed. However, I receive syntax error

java.sql.SQLSyntaxErrorException: 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 'order WHERE date = '2019-05-22'' at line 1

I cannot spot where is the error. Anyone can help?

PreparedStatement ps,ps1;
        ps =con.prepareStatement("Select 1 from order WHERE date = ?");
        ps.setString(1, date);
        ResultSet rsOrder=ps.executeQuery();  
        if(rsOrder.next()) {  
            System.out.println("reOrder:  true");
            ps1 = con.prepareStatement("UPDATE order SET result = ?  WHERE date = ?");
            ps1.setString(1, json);
            ps1.setString(2, date);
            int rs=ps1.executeUpdate();

        }else {
            System.out.println("reOrder:  false");
            ps1 = con.prepareStatement("INSERT INTO order (date,result) VALUES (?,?)");
            ps1.setString(1, date);
            ps1.setString(2, json);
            int rs=ps1.executeUpdate();
        }
1
  • 3
    Chances are that order is a reserved keyword . You may have to escape it . Commented May 24, 2019 at 7:38

1 Answer 1

1

order is the reserved word in MySQL. So it will not allow, with out escaping the reserved words.

You can escape it by adding ` around the keyword as:

Select 1 from `order` WHERE date = ?

UPDATE `order` SET result = ? WHERE date = ?

INSERT INTO `order` (date, result) VALUES (?, ?)
Sign up to request clarification or add additional context in comments.

1 Comment

I just simply change the table name from order to orders but I accept your answer as well

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.