gpt4 book ai didi

java - 用于连接 mysql 数据库的 SSLException

转载 作者:可可西里 更新时间:2023-11-01 08:38:46 25 4
gpt4 key购买 nike

我正在创建一个 web 项目..我已经为此配置了 jdbc 领域..我需要将 java servlet 连接到 mysql 数据库..

private static String JDBC_DRIVER = "com.mysql.jdbc.Driver", DB_URL = "jdbc:mysql://localhost/chat",
USER = "root", PASS = "public";
@OnMessage
public void onMessage(String msg, Session session) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String userId = getId(userName);
String sql = "SELECT * FROM user_relation WHERE user_id_2='" + userId + "' AND relation = 0;";
ResultSet rs = stmt.executeQuery(sql);
} catch (Exception e) {
e.printStackTrace();
}
}

当我通过服务器运行程序时出现以下错误:

** BEGIN NESTED EXCEPTION ** 

javax.net.ssl.SSLException
MESSAGE: closing inbound before receiving peer's close_notify

STACKTRACE:

javax.net.ssl.SSLException: closing inbound before receiving peer's close_notify
at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:129)
at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:117)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:308)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:264)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:255)
at java.base/sun.security.ssl.SSLSocketImpl.shutdownInput(SSLSocketImpl.java:645)
at java.base/sun.security.ssl.SSLSocketImpl.shutdownInput(SSLSocketImpl.java:624)
at com.mysql.cj.protocol.a.NativeProtocol.quit(NativeProtocol.java:1313)
at com.mysql.cj.NativeSession.quit(NativeSession.java:182)
at com.mysql.cj.jdbc.ConnectionImpl.realClose(ConnectionImpl.java:1743)
at com.mysql.cj.jdbc.ConnectionImpl.close(ConnectionImpl.java:717)
at org.apache.catalina.realm.JDBCRealm.close(JDBCRealm.java:447)
at org.apache.catalina.realm.JDBCRealm.getRoles(JDBCRealm.java:615)
at org.apache.catalina.realm.JDBCRealm.authenticate(JDBCRealm.java:404)
at org.apache.catalina.realm.JDBCRealm.authenticate(JDBCRealm.java:320)
at org.apache.catalina.realm.CombinedRealm.authenticate(CombinedRealm.java:188)
at org.apache.catalina.realm.LockOutRealm.authenticate(LockOutRealm.java:153)
at org.apache.catalina.authenticator.FormAuthenticator.doAuthenticate(FormAuthenticator.java:264)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:572)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:834)


** END NESTED EXCEPTION **

如何解决?我是否需要创建一个 ssl 证书..如果需要怎么办?

最佳答案

错误 - javax.net.ssl.SSLException: closing inbound before receiving peer's close_notify 主要是因为 SSL 默认设置为 true。

您只需要在代码中设置"?useSSL=false"

String url="jdbc:mysql://localhost:3306/samp1?useSSL=false";

sampl1 是数据库的名称。

关于java - 用于连接 mysql 数据库的 SSLException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52724001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com