java.sql.SQLNonTransientConnectionException:Could not create connection to database server解决

1.启动报Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.错误

2.访问时报java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

可以是在配置mysql时,没有指定mysql的版本,采用默认的版本,默认的版本过高,本地版本低。因为在mysql5中,jdbc的驱动是com.mysql.jdbc.Driver,而mysql6以及以上是com.mysql.cj.jdbc.Driver。所以在配置时要配置mysql版本号,主要配置数据源的驱动。

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>  **这里指定匹配版本号**
        </dependency>
spring.datasource.url=jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
**配置的驱动要和Mysql的版本一致**
posted @ 2022-03-07 17:55  寒冷的雨呢  阅读(1111)  评论(0编辑  收藏  举报