Reputation: 369
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();
}
Upvotes: 1
Views: 231
Reputation: 13237
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 (?, ?)
Upvotes: 1