在这里贴一下异常信息,并说一下我的解决办法。
情况:
spring+mybatis,使用mysql8.0。
异常信息:
: nested exception is :
###Error querying database. Cause: : Could not get JDBC Connection; nested exception is : Cannot create PoolableConnectionFactory (Could not create connection to database server.)
###The error may exist in file [D:\eclipse\workspaceForTeduStore\target\classes\mappers\]
###The error may involve
###The error occurred while executing a query
###Cause: : Could not get JDBC Connection; nested exception is : Cannot create PoolableConnectionFactory (Could not create connection to database server.)
at (:77)
at $(:446)
at .$(Unknown Source)
at (:166)
at (:63)
at (:43)
at .$(Unknown Source)
at .test1(:26)
at .invoke0(Native Method)
at (:62)
at (:43)
at (:497)
at $(:50)
at (:12)
at (:47)
at (:17)
at (:26)
at (:325)
at .(:78)
at .(:57)
at $(:290)
at $(:71)
at (:288)
at $000(:58)
at $(:268)
at (:363)
at ..(:86)
at (:38)
at (:459)
at (:678)
at (:382)
at (:192)
Caused by: :
###Error querying database. Cause: : Could not get JDBC Connection; nested exception is : Cannot create PoolableConnectionFactory (Could not create connection to database server.)
###The error may exist in file [D:\eclipse\workspaceForTeduStore\target\classes\mappers\]
###The error may involve
###The error occurred while executing a query
###Cause: : Could not get JDBC Connection; nested exception is : Cannot create PoolableConnectionFactory (Could not create connection to database server.)
at (:23)
at (:107)
at (:98)
at (:62)
at .invoke0(Native Method)
at (:62)
at (:43)
at (:497)
at $(:433)
... 30 more
Caused by: : Could not get JDBC Connection; nested exception is : Cannot create PoolableConnectionFactory (Could not create connection to database server.)
at (:80)
at (:82)
at (:68)
at (:271)
at (:69)
at (:56)
at (:259)
at (:132)
at (:105)
at (:81)
at (:104)
... 37 more
Caused by: : Cannot create PoolableConnectionFactory (Could not create connection to database server.)
at (:1549)
at (:1388)
at (:1044)
at (:111)
at (:77)
... 47 more
Caused by: .: Could not create connection to database server.
at .newInstance0(Native Method)
at (:62)
at (:45)
at (:422)
at (:404)
at (:387)
at (:919)
at (:898)
at (:887)
at (:862)
at (:2331)
at (:2084)
at .<init>(:795)
at .JDBC4Connection.<init>(:44)
at .newInstance0(Native Method)
at (:62)
at (:45)
at (:422)
at (:404)
at (:400)
at (:327)
at (:38)
at (:582)
at (:1556)
at (:1545)
... 51 more
Caused by:
at (:3004)
at (:1908)
at (:1837)
at (:1207)
at (:2254)
at (:2285)
... 65 more
解决办法:
最初,我使用的mysql驱动包版本是5.1.37,片段如下:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
这个驱动包版本过低,版本改用8.0.11,片段如下:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
即可解决问题。
另外需要提到的问题是,8.0.11版本的mysql驱动包的driverClass有所变化,由driverClass=""
变为:driverClass=""
综上所述,异常为连接mysql8.0时,mysql驱动包版本过低所致,修改mysql驱动包版本与数据库匹配即可。