org.springframework.jdbc.BadSqlGrammarException:报SQL语句拼写错误

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 'desc FROM test_photo WHERE id=1' at line 1
### The error may exist in file [C:\DDisk\javaC\MavenWebProject\target\classes\com\maven\testCommonFileUpload\FileBeanMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT name,url,desc FROM test_photo WHERE id=?
### 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 'desc FROM test_photo WHERE id=1' 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 'desc FROM test_photo WHERE id=1' at line 1
......下面的错误信息省略...

 各种测试,我的语法怎么可能有错呢。弄了好久,简直要疯掉,电脑编译器均重启过,仍解决不了问题。

直到我将这条sql语句放到navicat上执行。。。

结论:

很明显了,颜色不对,“name”和“desc”这个字段是个关键字

在这条语句中desc位置比较特殊,被当成了关键字。

解决:

  • 关键字加上反引号`,使其成为列名。
  • 或在查询的时候给列取别名 (AS | | 空格)。

怪我SQL没有学好,浪费了若干小时。 难受啊。

注:mysql使用反引号`(在键盘1左边的反引号`),oracle使用单引号'。

收获:

  • 记一下,以后我不能再将关键字作为数据库的列名了!!!
  • 希望养成习惯,SQL语法错误,要第一时间在navicat等工具上进行测试
  • SQL关键字:desc(当成描述),table(当成桌子),name(当成名字),order(当成订单),慎用作列名!
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值