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(当成订单),慎用作列名!