Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQLSyntaxErrorException with table name "condition" [closed]

Tags:

java

sql

mysql

I have a table called condition. I need to keep it with that name, but when I execute the command

Query qb = em.createQuery("SELECT c FROM Condition c ")

I get this exception:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'condition' at line 1
at sun.reflect.GeneratedConstructorAccessor5.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2683)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2144)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2310)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeSelect(DatabaseAccessor.java:931)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:607)

I know that MySQL has a command called condition and that's why this command throws me an exception.

Is there a form to keep my table named as condition, and that would allow me to execute the following command?

Query qb = em.createQuery("SELECT c FROM Condition c ")

2 Answers

This is because condition is a reserved keyword of MySQL.

Try this:

Query qb = em.createQuery("SELECT c FROM `Condition` c ")

Check Schema Object Names for more details.

I hope it may helps you!

like image 103
aizaz Avatar answered Sep 03 '26 22:09

aizaz


CONDITION is a reserved keyword. It must be escaped using backticks:

SELECT c FROM `Condition` c
  • MySQL Reserved Keywords List

If you have a chance to alter the identifier, change the table name which is not present on the reserved keyword list. This will avoid you from future headaches.

like image 45
John Woo Avatar answered Sep 03 '26 23:09

John Woo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!