FAQ(41):Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:

本文记录了一次Spring整合MyBatis过程中遇到的SQL语法错误,并详细展示了错误日志及堆栈跟踪信息。通过分析,发现是由于SQL语句中多条件查询时未正确使用and连接导致的问题。
欢迎扫二维码关注公众号,获取技术干货

2018/1/3

spring对Mybatis整合,

看Log:

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 'b.providerId=1,b.isPayment=2
		    and b.providerId=p.id' at line 3
### The error may exist in com/smbms/dao/BillMapper.xml
### The error may involve com.smbms.dao.BillMapper.billShowByCondition-Inline
### The error occurred while setting parameters
### SQL: select b.billCode,b.productName,p.proName,b.totalPrice,b.isPayment,b.creationDate     from smbms_bill b,smbms_provider p     where b.produceName=?,b.providerId=?,b.isPayment=?       and b.providerId=p.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 'b.providerId=1,b.isPayment=2
		    and b.providerId=p.id' at line 3
; 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 'b.providerId=1,b.isPayment=2
		    and b.providerId=p.id' at line 3
	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.$Proxy7.selectList(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230)
	at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)
	at com.sun.proxy.$Proxy8.billShowByCondition(Unknown Source)
	at com.smbms.service.BillServiceImpl.findBillsWithConditionsByThressConditions(BillServiceImpl.java:19)
	at com.smbms.service.BillServiceImplTest.test(BillServiceImplTest.java:32)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
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 'b.providerId=1,b.isPayment=2
		    and b.providerId=p.id' at line 3
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	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)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2487)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
	at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1197)
	at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172)
	at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:59)
	at com.sun.proxy.$Proxy10.execute(Unknown Source)
	at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:63)
	at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
	at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63)
	at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324)
	at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
	at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
	at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
	... 31 more


解释:
我的sql语句写错了!!
使用where多条件刷选时,条件之间要用and分割。

 

 

 

 

 

 

当查询超过10s出现 'Error querying database. Cause: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure' 错误,通常是由于数据库连接中断、网络问题、数据库服务器配置等原因导致的,以下是一些可能的解决方法: ### 检查网络连接 - **确保网络稳定**:检查应用程序服务器与 MySQL 数据库服务器之间的网络连接是否正常,可使用 `ping` 命令测试网络连通性,使用 `telnet` 命令测试端口是否开放。例如: ```bash ping <数据库服务器IP> telnet <数据库服务器IP> 3306 ``` - **检查防火墙设置**:确保防火墙没有阻止应用程序服务器与数据库服务器之间的通信。需要开放 MySQL 服务器使用的端口(默认是 3306)。如果是 Linux 系统,可以使用以下命令开放端口: ```bash sudo firewall-cmd --zone=public --add-port=3306/tcp --permanent sudo firewall-cmd --reload ``` ### 检查数据库服务器配置 - **调整连接超时时间**:在 MySQL 配置文件(通常是 `my.cnf` 或 `my.ini`)中,可以调整 `wait_timeout` 和 `interactive_timeout` 参数,增加连接的超时时间。例如: ```ini [mysqld] wait_timeout = 28800 interactive_timeout = 28800 ``` 修改后重启 MySQL 服务。 - **检查最大连接数**:确保数据库服务器的最大连接数足够。可以通过以下 SQL 语句查看和修改最大连接数: ```sql -- 查看当前最大连接数 SHOW VARIABLES LIKE 'max_connections'; -- 修改最大连接数 SET GLOBAL max_connections = 500; ``` ### 检查应用程序配置 - **调整连接池配置**:如果使用了连接池,需要调整连接池的配置参数,如最大空闲时间、最大连接数等。以 HikariCP 为例,可以在配置文件中进行如下配置: ```properties spring.datasource.hikari.maximum-pool-size=20 spring.datasource.hikari.idle-timeout=30000 ``` - **检查数据库连接 URL**:确保数据库连接 URL 正确,包括数据库服务器地址、端口、数据库名等信息。例如: ```java jdbc:mysql://<数据库服务器IP>:3306/<数据库名>?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai ``` ### 优化查询语句 - **分析慢查询**:使用 MySQL 的慢查询日志功能,找出执行时间超过 10s 的查询语句,并对其进行优化。可以通过以下步骤开启慢查询日志: - 在 `my.cnf` 或 `my.ini` 中添加以下配置: ```ini [mysqld] slow_query_log = 1 slow_query_log_file = /var/log/mysql/mysql-slow.log long_query_time = 1 ``` - 重启 MySQL 服务。 - 分析慢查询日志文件,找出执行时间长的查询语句,并添加合适的索引或优化查询逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

后台技术汇

对你的帮助,是对我的最好鼓励。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值