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”这个字段是个关键字,那么语句出问题就说得通了

 

快速解决办法就是在给关键字加上`,使其成为列名(如:`name` ,注意,这个不是单引号',是在1左边的`号)。否则,列名还是重新设置吧。

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

收获:

  • 记一下,以后我不可能再将”name“作为数据库的列名了!!!
  • 以后希望养成习惯,SQL语法错误,要第一时间想到在mysql上进行测试。
  • SQL关键字:desc(当成描述),table(当成桌子),name(当成名字),order(当成订单),等关键字慎用

你可能感兴趣的:(mysql)