如果在代码中使用了批量更新、插入、删除,或者拼接了多条sql一次去执行,但是确遇到了下面的报错:
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 ‘‘upate tablle xxxxxx’’ at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
那么不要着急,先一步一步来排查吧
- 检查sql或者代码有没有基本的语法错误
- 检查sql中是否存在关键字或者中文字符(这一步需要特别耐心检查)
啥,完成这俩步还报错?
我的sql没问题呀,我的代码没问题呀,打印出来的sql放数据库执行也不报错呀。。。。
为啥还是报错。。。。。。。。。。。。。。。。。
那就换个思路,是不是少了配置或者配置错了
来来来,让我们试试这个:
- 在jdbc url参数中加 allowMultiQueries
像下面这样
String url = "jdbc:mysql://localhost:3306?allowMultiQueries=true";
重新启动,再试试是不是发现没报错了–__–
既然问题解决了,那是不是要学习下allowMultiQueries配置是干啥的呢?
官网解释: click here https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-configuration-properties.html
allowMultiQueries
Allow the use of ‘;’ to delimit multiple queries during one statement (true/false). Default is ‘false’, and it does not affect theaddBatch() and executeBatch() methods, which rely on rewriteBatchStatements instead.
Default: false
Since version: 3.1.1
翻译过来的大概意思是:允许使用;去做批量操作,默认值为false,为默认值时不会影响addBatch() and executeBatch()执行批量,但是动态拼接带;的sql就会不能执行
如果还不能解决你的问题,你可以看看这个:
Jdbc Url 设置allowMultiQueries为true和false时底层处理机制研究
啥?你还想看mysql连接还有哪些配置?那看看这个 jdbc url 配置