动态注册jdbc驱动程序,建立数据库连接读取数据库中的数据。mysql版本是5.7,mysql驱动包为mysql-connector-java8.0.16,ubuntu16.04。
问题描述:报错信息如下:
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835) at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:455) at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240) at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at com.Demo1.main(Demo1.java:22) Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) at com.mysql.cj.protocol.a.NativeProtocol.negotiateSSLConnection(NativeProtocol.java:361) at com.mysql.cj.protocol.a.NativeAuthenticationProvider.negotiateSSLConnection(NativeAuthenticationProvider.java:777) at com.mysql.cj.protocol.a.NativeAuthenticationProvider.proceedHandshakeWithPluggableAuthentication(NativeAuthenticationProvider.java:486) at com.mysql.cj.protocol.a.NativeAuthenticationProvider.connect(NativeAuthenticationProvider.java:202) at com.mysql.cj.protocol.a.NativeProtocol.connect(NativeProtocol.java:1452) at com.mysql.cj.NativeSession.connect(NativeSession.java:165) at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:955) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825) ... 6 more Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate) at sun.security.ssl.HandshakeContext. (HandshakeContext.java:171) at sun.security.ssl.ClientHandshakeContext. (ClientHandshakeContext.java:98) at sun.security.ssl.TransportContext.kickstart(TransportContext.java:220) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:428) at com.mysql.cj.protocol.ExportControlled.performTlsHandshake(ExportControlled.java:316) at com.mysql.cj.protocol.StandardSocketFactory.performTlsHandshake(StandardSocketFactory.java:188) at com.mysql.cj.protocol.a.NativeSocketConnection.performTlsHandshake(NativeSocketConnection.java:99) at com.mysql.cj.protocol.a.NativeProtocol.negotiateSSLConnection(NativeProtocol.java:352) ... 13 more
原因分析:
(1)可能是mysql服务没有开启
(2)连接数据库的url不正确
(1)开启mysql服务
service mysql start
(2)检查数据库连接是否配置正确。在
数据库名称后添加 ?useSSL=false&useUnicode=true&characterEncoding=utf-8&useLegacyDatetimeCode=false&serverTimezone=UTC
修改前:
String url="jdbc:mysql://localhost:3306/runoob?serverTimezone=UTC";
修改后:
String url="jdbc:mysql://localhost:3306/runoob?useSSL=false&useUnicode=true&characterEncoding=utf-8&useLegacyDatetimeCode=false&serverTimezone=UTC";
其中的参数含义如下:
useSSL:表示是否需要建立 SSL 连接的。这里将它显示关闭。 allowPublicKeyRetrieval:表示是否允许客户端从服务器获取公钥。 serverTimezone:设置CST时区。
至于为什么要添加这些参数,尚不清楚,因为看到别人在windows下使用mysql8.0也没有出现过这种错误,难道是因为我用的是mysql5.7?经过查阅,发现 MySQL驱动5.1有userLegacyDatetimeCode和userTimezone两个参数,默认为userLegacyDatetimeCod=true&userTimezone=false。而MySQL驱动8.0中没有userLegacyDatetimeCode和userTimezone两个参数,所以一定要配置serverTimezone为MySQL运行的时区. 连接建立时会将这个时区存储到连接中。
另外,还需要注意驱动包、mysql、java三者之间得对应版本,否则也是会出现各种错误的。
- mysql-connector-java与Mysql对应版本:
官方更推荐MySQL5.6以上使用connector/j 8.0 - mysql-connector-java与Mysql对应版本:
参考资料:
1、Java与MySQL时间戳传递/存储/协调问题–userLegacyDatetimeCode–userTimezone–serverTimezone
2、https://blog.csdn.net/shiqidide/article/details/7642531
3、mysql、驱动包、java对应版本
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)