jdbc连接mysql8.0.21_使用JDBC连接mysql8.0以上碰到的问题

在使用Java操作MySQL8.0.21时,遇到认证协议不支持、驱动过时及SSL连接警告等问题。解决方案包括升级到新版驱动mysql-connector-java-8.0.12,更改连接驱动为com.mysql.cj.jdbc.Driver,并在URL中添加?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true来避免SSL警告和确保正确编码。
摘要由CSDN通过智能技术生成

mysql8.0.X安装程序是比较新的版本,最近用JAVA操作mysql8.0.X,碰到几个问题,这里记录一下:

1、报如下错:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Client does not support authentication protocol requested by server; consider upgrading MySQL client

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

解决方法:需要用到新版本的驱动,比如:mysql-connector-java-8.0.12

下载地址: http://www.mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.12

2、报错如下:

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.

解决方法: Mysql和mysql-connector都为8.0以上的版本时,连接驱动就不要再用:com.mysql.jdbc.Driver了,要引用com.mysql.cj.jdbc.Driver

3、报如下错:

Mon Jun 24 15:40:04 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

Exception in thread "main" java.lang.NullPointerException

8.0.X以上的版本还需在数据库名后面加上这些东西 :

?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true

由原来的:

jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8

变为如下的:

jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值