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"

你可能感兴趣的:(mysql)