我正在创建一个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证书吗?如果要怎么做?
答案 0 :(得分:1)
这是MySQL JDBC驱动程序和Java 11的问题。请参见here。此问题已在Connector / J版本8.0.16中修复
答案 1 :(得分:1)
错误- javax.net.ssl.SSLException:在接收对等方的close_notify 之前关闭入站,主要是因为默认情况下SSL设置为true。
您只需要在代码中设置"?useSSL=false"
。
String url="jdbc:mysql://localhost:3306/samp1?useSSL=false";
sampl1是数据库的名称。