1

When I tried to execute the following java code I face the issue

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 SET ord_name = 'OD1320210903122013', shipcost = '0.0', price='799.0', supp' at line 1
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)

Below the code :

String order = "INSERT INTO order SET ord_name = ?, shipcost = ?, price=?, supplierid=?, userid=?, suppliername=?, shippingdate=?, shipaddress=?, timestmp=?";
PreparedStatement ord_create = con.prepareStatement(order);
ord_create.setString(1,name);
ord_create.setString(2,String.valueOf(shippingCost));
ord_create.setString(3, String.valueOf(price));
ord_create.setString(4,String.valueOf(supplierid));
ord_create.setString(5,usr);
ord_create.setString(6,suppl_name);
ord_create.setString(7,shippingDate);
ord_create.setString(8,address);
ord_create.setString(9,timestamp);
int result = ord_create.executeUpdate(); 

The same statement compiled (took from debugger ) works perfectly if executed directly on MySQL Workbench

Can you help me?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
LittleLeg
  • 11
  • 1
  • Can you show the definition of you table please? – Maurice Perry Sep 03 '21 at 10:41
  • *`timestmp`* ? Is that really right? – g00se Sep 03 '21 at 10:42
  • 1
    **`INSERT INTO "order" ...`** as ORDER is a keyword (ORDER BY). – Joop Eggen Sep 03 '21 at 10:42
  • Also, with everything `setString` then let's hope your driver can handle that (assuming of course that you haven't for some reason modeled numeric types as character types in your actual table) – g00se Sep 03 '21 at 10:46
  • Also, you've used the syntax of an UPDATE for an INSERT. Try something like: `INSERT INTO order(ord_name, shipcost, price, supplierid, userid, suppliername, shippingdate, shipaddress, timestmp) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)` – Maurice Perry Sep 03 '21 at 10:52

0 Answers0