1. 程式人生 > 其它 >java.sql.SQLNonTransientConnectionException:Could not create connection to database server解決

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的版本一致**
擇善人而交,擇善書而讀,擇善言而聽,擇善行而從。