I am trying to insert data into a MySQL database, but Java throws an SQLException
signalling syntax error. But when I execute the exact same SQL code (copied, pasted) on MySQL Console, it executes successfully. I would like to ask what is causing the weird behaviour and how to resolve it.
Java code:
sql = "INSERT INTO record_t "
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);";
prepSql = this.conn.prepareStatement(sql);
prepSql.setString(1, id);
prepSql.setString(2, roomNum);
prepSql.setString(3, name);
prepSql.setString(4, ic);
prepSql.setString(5, tel);
prepSql.setString(6, email);
prepSql.setString(7, checkIn);
prepSql.setString(8, checkOut);
prepSql.setBoolean(9, false);
System.out.println(prepSql);
rowInserted = prepSql.executeUpdate(sql);
if (rowInserted > 0) {
System.out.println("success");
}
Java return message:
com.mysql.cj.jdbc.ClientPreparedStatement: INSERT INTO record_t VALUES ('RCD00003', 'GF3', 'asfd', 'asfd', 'asdfasdf', 'asdf', '2021-10-25', '2021-10-26', 0);
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 '?, ?, ?, ?, ?, ?, ?, ?, ?)' at line 1
Error Code: 1064
Error State: 42000
SQL Code:
INSERT INTO record_t VALUES ('RCD00003', 'GF3', 'asfd', 'asfd', 'asdfasdf', 'asdf', '2021-10-25', '2021-10-26', 0);
MySQL return message:
[1:1] Executed successfully in 0 s.
1 row affected.
Execution finished after 0.002 s, no errors occurred.