JDBC中com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver的区别

最近重装了系统,随后在官网下载了最新版本的MySQL8.0.13和相应的mysql-connector-java-8.0.13,运行程序时出现好几种错误。

之前的代码是这样的:

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///db1?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=root

在连接数据库时报错,提示

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.

大概意思就是之前的 `com.mysql.jdbc.Driver'已经过时了,现在得用`com.mysql.cj.jdbc.Driver'加载驱动,而且驱动程序已经自动加载好了。之前看到过在Mysql5之后的驱动包都会自动加载驱动。

现在将driverClassName的值改为com.mysql.cj.jdbc.Driver,运行程序,又提示如下错误

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.

 意思就是需要通过ServerTimeZone属性来设置一个时区,去网上查询之后,可以选择东8区的Hongkong或者Asia/Shanghai作为参数,修改后的代码如下

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql:///db1?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
username=root
password=root

猜你喜欢

转载自blog.csdn.net/GouGe_CSDN/article/details/86569929