首页 新闻 搜索 专区 学院

java 链接mysql数据库的问题,请大佬看下解决不了了

0
悬赏园豆:100 [待解决问题]

package com.jdbc;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class Jdbc_link {

//链接数据库

public static Connection getConnection() throws SQLException,ClassNotFoundException{

Class.forName("com.mysql.jdbc.Driver");

String url="jdbc:mysql://localhost:3306/eshopdata?characterEncoding=utf8&useSSL=true";

String username="root";

String password="zy113028";

Connection conn=DriverManager.getConnection(url, username, password);

return conn;

}

//关闭数据库链接,释放资源

public static void release(Statement stmt,Connection conn){

if(stmt !=null){

try{

stmt.close();

}catch(SQLException e){

e.printStackTrace();

}

}

if(conn!=null){

try{

conn.close();

}catch(SQLException e){

e.printStackTrace();

}

}

}

public static void release(ResultSet re,Statement stmt,Connection conn){

if(re!=null){

try{

re.close();

}catch(SQLException e){

e.printStackTrace();

}

}

release(stmt,conn);

}

}

会提示链接出现错误

下面是报错

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 254 milliseconds ago. The last packet sent successfully to the server was 248 milliseconds ago.

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

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

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

at java.lang.reflect.Constructor.newInstance(Constructor.java:513)

at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)

at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:989)

at com.mysql.jdbc.ExportControlled.transformSocketToSSLSocket(ExportControlled.java:203)

at com.mysql.jdbc.MysqlIO.negotiateSSLConnection(MysqlIO.java:4901)

at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1659)

at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1226)

at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2191)

at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2222)

at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2017)

at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:779)

at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)

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

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

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

at java.lang.reflect.Constructor.newInstance(Constructor.java:513)

at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)

at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389)

at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)

at java.sql.DriverManager.getConnection(DriverManager.java:582)

at java.sql.DriverManager.getConnection(DriverManager.java:185)

at com.jdbc.Jdbc_link.getConnection(Jdbc_link.java:18)

at com.jdbc.Jdbc_main.main(Jdbc_main.java:14)

Caused by: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors

at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)

at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1611)

at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:187)

at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:181)

at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1035)

at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:124)

at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Handshaker.java:516)

at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.java:454)

at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:884)

at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1112)

at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1139)

at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1123)

at com.mysql.jdbc.ExportControlled.transformSocketToSSLSocket(ExportControlled.java:188)

... 19 more

Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors

at com.mysql.jdbc.ExportControlled$X509TrustManagerWrapper.checkServerTrusted(ExportControlled.java:304)

at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1027)

... 27 more

Caused by: java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors

at sun.security.provider.certpath.PKIXCertPathValidator.engineValidate(PKIXCertPathValidator.java:195)

at java.security.cert.CertPathValidator.validate(CertPathValidator.java:250)

at com.mysql.jdbc.ExportControlled$X509TrustManagerWrapper.checkServerTrusted(ExportControlled.java:297)

... 28 more

我用mysql图型界面是mysqlworkbench 8

问道途的主页 问道途 | 初学一级 | 园豆:102
提问于:2018-12-12 22:46
< >
分享
所有回答(2)
0

去掉连接字符串中的useSSL=true,另,建议先把提问的代码格式化做好。

幻天芒 | 园豆:36594 (高人七级) | 2018-12-12 22:54

改掉以后就出现这个错误,现在完全不知道怎么改
Wed Dec 12 23:11:25 CST 2018 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.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:868)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:864)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1746)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1226)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2191)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2222)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2017)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:779)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at com.jdbc.Jdbc_link.getConnection(Jdbc_link.java:18)
at com.jdbc.Jdbc_main.main(Jdbc_main.java:14)

支持(1) 反对(0) 问道途 | 园豆:102 (初学一级) | 2018-12-12 23:14

我开了tomcat

支持(0) 反对(0) 问道途 | 园豆:102 (初学一级) | 2018-12-12 23:18

@问道途: useSSL = false,异常中很明确,先这样设置。

支持(0) 反对(0) 幻天芒 | 园豆:36594 (高人七级) | 2018-12-12 23:27

@幻天芒: 设置那个之后会报身份无法验证的错误·
Unable to load authentication plugin 'caching_sha2_password

支持(0) 反对(0) 问道途 | 园豆:102 (初学一级) | 2018-12-12 23:37

@问道途: 请参考:https://stackoverflow.com/questions/50387952/how-to-resolve-unable-to-load-authentication-plugin-caching-sha2-password-issu

支持(0) 反对(0) 幻天芒 | 园豆:36594 (高人七级) | 2018-12-13 11:24
0

1 去掉useSSL=true或者useSSL=false
2 后面那个错误是因为mysql 8.0版本问题 你代码本身在mysql 5.7上面是正常的 解决办法参考
mysql8.0 Authentication plugin 'caching_sha2_password' cannot be loaded
authentication plugin caching_sha2

ycyzharry | 园豆:20831 (高人七级) | 2018-12-13 10:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册