误用MySQL关键字导致的错误

使用Hibernate整合Spring的过程中,我使用explain作为表的字段,结果一直给我报错

报错如下:

ERROR: 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 'EXPLAIN varchar(255),
primary key (ID)
) ENGINE=InnoDB' at line 4

==============================

2017-9-24更,以上的是字段名称与MySQL的关键字相同引起的报错

下面的是表名称与MySQL的关键字相同引起的报错

其中ORDER 也是MySQL的关键字。当我有一个类的名称叫Order ,所以表的名称取为ORDER,其他的都正确。当我第一次执行保存操作。此时会先创建表,但是当创建ORDER表的时候,抛出了异常。如下(这里挑出关键的两个地方,完整的异常贴在后面)

一、

org.hibernate.exception.SQLGrammarException: could not execute statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:82)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert

这里说是SQL的语法异常,不能执行该语句。

二、

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 'ORDER (ORDER_NAME, CUSTOMER_ID) values ('Order-1', null)' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance

我觉得一处说的不能执行该语句应该指的是二处的标红的语句。因为这个语句有 ORDER 这个关键字,但这个关键字使用错误了,这里我把它当成一个表名称了。

总结:所以,以后再声明类名称或者属性名称时,一定要注意使用的名称是否和数据库中的关键字相同,如果相同就有可能出现错误,应尽量避免。

原文地址:https://www.cnblogs.com/GooPolaris/p/7918902.html