上回我们讲到sql语法基础知识,但是有的时候我们编写SQL语句时经常会遇到语法报错的情况,小伙伴们知道一般是因为什么才报错呢?下面通过几个常见的错误例子一起来了解一下吧。
例1:
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java: 760) at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java: 747) at org.springframework.boot.SpringApplication.run(SpringApplication.java: 315) at org.springframework.boot.SpringApplication.run(SpringApplication.java: 1162) at org.springframework.boot.SpringApplication.run(SpringApplication.java: 1151) at com.lk.ActivemqApplication.main(ActivemqApplication.java: 12) Caused by: org.springframework.jdbc.BadSqlGrammarException: ###Error querying database.Cause: 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 'AS SELECT queue_message_entity WHERE `insert_time` < ' 1970 - 01 - 01 08: 00: 55 '' at line 1### The error may exist in com / lk / loms / mq / service / dao / mapper / HistoryMsgMapper.java(best guess)### The error may involve com.lk.loms.mq.service.dao.mapper.HistoryMsgMapper.insertQueueBackup - Inline### The error occurred while setting parameters### SQL: INSERT INTO queue_message_entity_bak AS SELECT queue_message_entity WHERE `insert_time` < ? ###Cause : 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 'AS SELECT queue_message_entity WHERE `insert_time` < ' 1970 - 01 - 01 08: 00: 55 '' at line 1 ; bad SQL grammar[]; nested exception is 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 'AS SELECT queue_message_entity WHERE `insert_time` < ' 1970 - 01 - 01 08: 00: 55 '' at line 1 at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java: 231) at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java: 73) at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java: 73) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java: 446) at com.sun.proxy.$Proxy120.selectOne(Unknown Source) at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java: 166) at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java: 83) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java: 59) at com.sun.proxy.$Proxy121.insertQueueBackup(Unknown Source) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java: 62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java: 43) at java.lang.reflect.Method.invoke(Method.java: 498) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java: 333) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java: 190) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java: 157) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java: 92) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java: 179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java: 213) at com.sun.proxy.$Proxy123.insertQueueBackup(Unknown Source) at com.lk.BackupCommandLineRunner.insertQueueBackup(BackupCommandLineRunner.java: 61) at com.lk.BackupCommandLineRunner.run(BackupCommandLineRunner.java: 27) at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java: 776) ...6 common frames omitted 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 'AS SELECT queue_message_entity WHERE `insert_time` < ' 1970 - 01 - 01 08: 00: 55 '' at line 1 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java: 62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java: 45) at java.lang.reflect.Constructor.newInstance(Constructor.java: 423) at com.mysql.jdbc.Util.handleNewInstance(Util.java: 425) at com.mysql.jdbc.Util.getInstance(Util.java: 408) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java: 943) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java: 3973) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java: 3909)
如以上这个错误,看着很复杂,其实就是粗心导致的,
INSERT INTO queue_message_entity_bak AS SELECT queue_message_entity WHERE `insert_time` < ?
SQL语句中多了个as导致语法报错
例2::insert into user(name,password,describe) values("ss","ss","ww") ;
如以上SQL插入语句,因为使用了describe所以报错,describe1sql中的关键词,不能在SQL语句编写时出现。
例3:Unknown column 'g.sales_volume' in 'field list
我们看这个报错,它说的十分清楚,在表内不存在的列,查看sales_volume列是否存在,这些都是粗心导致的。
其实说到这里我们都差不多了解了,SQL语法的报错一般都是由于编写SQL语句时粗心大意编写错误导致的,或者是因为表结构创建时没有设计完好所导致报错,以后有SQL语句报错我们只要第一时间检查相应的SQL语句及表结构就行啦。
以上就是今天的全部内容了,关于SQL语句你都了解清楚了吗?更多详细java入门知识请持续关注我们网站了解吧。
推荐阅读: