1. 程式人生 > >java 配置連線到mysql8.0的一些坑

java 配置連線到mysql8.0的一些坑

         這幾天學了資料庫,想著配置一下,結果遇到幾個問題,以下是遇到的一些坑,供大家參考.

1.com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
	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:411)

原因是因為mysql-connector-java.jar包與mysql版本不匹配,我下的MySQL版本是8.0的而jar包是老師給的5.4的

解決方法:下載對應版本的jar包,8.0版本的jar包下載地址https://dev.mysql.com/downloads/connector/j/選plaform independent裡面的

2.Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)

解決方法:將驅動載入

jdbc.driverClassName=com.mysql.jdbc.Driver 修改為

jdbc.driverClassName=com.mysql.cj.jdbc.Driver

3.The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

將url設定serverTimezone屬性

"jdbc:mysql://localhost:3306/test01"修改為"jdbc:mysql://localhost:3306/test01"+
                    "?serverTimezone=GMT%2B8"