事情起因:用Mac提交云笔记项目到SVN后,使用台式机import SVN上的云笔记代码,发现到了台式机上,进行junit测试时无法连接Mysql数据库服务器,而Mac上是可以的。以下是报警内容和报警画面

测试连接失败画面:

报警内容:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Could not create connection to database server.)
### The error may exist in file [F:\workspace\Cloud_Note_Clyang\target\classes\Mapper\NoteMapper.xml]
### The error may involve com.boe.Dao.NoteDAO.findNoteByNoteBookId
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Could not create connection to database server.)
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
at com.sun.proxy.$Proxy19.selectList(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:128)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:68)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)
at com.sun.proxy.$Proxy22.findNoteByNoteBookId(Unknown Source)
at Test.testNoteDAO.test(testNoteDAO.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
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:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
以下略...

分析解决:

通过报警内容,发现报“org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection”,“Cannot create PoolableConnectionFactory (Could not create connection to database server.)“

(1)发现报警不能连接MySQL服务器,网上也有类似的问题,他们是通过修改mysql数据库里user表的方法,参考博客 https://blog.csdn.net/qq_41675973/article/details/84898061

修改mysql库的user表,将host项,从localhost改为%。%这里表示的是允许任意host访问,如果只允许某一个ip访问,则可改为相应的ip,比如可以将localhost改为192.168.1.123,这表示只允许局域网的192.168.1.123这个ip远程访问mysql。
mysql> use mysql;
mysql> update user set host = ‘%’ where user = ‘root’;
mysql> select host, user from user;
mysql> flush privileges;
(2)另外一种是Mysql连接驱动包的问题

通过对比发现,Mac上Mysql的版本为5.7,驱动版本5.1.34,而台式机上Mysql的版本为8.0,驱动也为5.1.34,尝试将驱动从5.1.34更新为最新的5.1.46

    <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>

更新后的驱动

重新进行测试连接:

发现可以通过笔记ID来查找到笔记Body内容,测试OK,原因为更换电脑后,Mysql版本更换,驱动包也需要随着更换。

最新文章

  1. 如何实现一个php框架系列文章【2】实现类的自动加载
  2. Java上等价类划分测试的实现
  3. SQL Server 事务日志传输
  4. git学习心得总结
  5. bat运行时不弹出那个黑框框的完美解决方案
  6. Ubuntu 下配置Ganglia监控
  7. 后半部分样式和JS前半部分脚本语言
  8. C#网络通信
  9. 三个流行MySQL分支的对比
  10. 层叠上下文 Stacking Context
  11. 工作的准备:atoi,itoa,strcpy,memcpy,strcmp,二分查找,strcat
  12. 译文:User-agent的历史
  13. MPI发送接收例子
  14. Linux软件安装管理
  15. java项目log4j日志打印配置
  16. 凸包--Graham扫描法
  17. linux 安装java环境
  18. linux audit (9)--生成audit报表
  19. 基于TCP(面向连接)的Socket编程
  20. PAT Basic 1069. 微博转发抽奖(20)

热门文章

  1. 使用Eclipse编译运行MapReduce程序 Hadoop2.6.0_Ubuntu/CentOS
  2. ORACLE ROWNUM解析[转]
  3. 《算法》第四章部分程序 part 12
  4. java 权重随机算法实现
  5. 使用Shiro登录成功后,跳转到之前访问的页面实现
  6. django之ReverseOneToOneDescriptor
  7. 在ls命令中使用通配符
  8. js检查字符串的包含关系
  9. MiniDao支持ID自增主键策略,使用讲解
  10. 1.Python基础知识小结: