简体   繁体   中英

SSLException for connecting mysql db

I am creating a webproject..I have configured the jdbc realm for this..I need to connect the java servlet to mysql database..

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();
    }
}

When I run the program through the server I get the following error :

** 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 **

How to solve this? Do I need to create a ssl certificate..if so how to do?

The error- javax.net.ssl.SSLException: closing inbound before receiving peer's close_notify is caused mainly because of SSL set to true by default.

You just need to set "?useSSL=false" in your code.

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

sampl1 is name of the database.

This was an issue with MySQL JDBC driver and Java 11. See here . This has been fixed in Connector/J version 8.0.16

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM