如何修复javax.net.ssl.SSLException

时间:2019-10-18 16:45:57

标签: java mysql database-connection

我正在尝试使用Java中的MySQL Workbench连接到数据库。连接应该在两台计算机之间。没有数据库的主机具有以下字符串来建立连接:“ jdbc:mysql://192.168.43.26:3306 / DATABASEE?user = admin&password = toor” 192.168.43.26是服务器的实际IP;和DATABASEE数据库的名称。

public static int CheckLogin(String Username, String Psw) throws InterruptedException {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        // Create a variable for the connection string.
        String connectionUrl = "jdbc:mysql://192.168.43.26:3306/DATABASEE?user=admin&password=toor";

        try {Connection con = DriverManager.getConnection(connectionUrl); Statement stmt = con.createStatement();
            ResultSet sr = stmt.executeQuery("select * from Users;");
            while(sr.next()) {
                String S = sr.getString("Username");
                String S2 = sr.getString("Psw");
                if(S.equals(Username) && S2.contentEquals(Psw)) {
                    con.close();
                    return 0;
                }
            }
            con.close();
        }
        // Handle any errors that may have occurred.
        catch (SQLException e) {
            e.printStackTrace();
        }
        return 1;
    }

执行以下代码后,Eclipse给了我以下错误:

    Fri Oct 18 18:01:40 CEST 2019 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.
    Fri Oct 18 18:01:41 CEST 2019 WARN: Caught while disconnecting...

    EXCEPTION STACK TRACE:



    ** 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.jdbc.MysqlIO.quit(MysqlIO.java:2246)
            at com.mysql.jdbc.ConnectionImpl.realClose(ConnectionImpl.java:4236)
            at com.mysql.jdbc.ConnectionImpl.close(ConnectionImpl.java:1462)
            at Ciao.OpenSQL.CheckLogin(OpenSQL.java:132)
            at Ciao.MasterStart.actionPerformed(MasterStart.java:160)
            at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
            at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
            at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
            at 

java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
        at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
        at java.desktop/java.awt.Component.processMouseEvent(Component.java:6632)
        at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
        at java.desktop/java.awt.Component.processEvent(Component.java:6397)
        at java.desktop/java.awt.Container.processEvent(Container.java:2263)
        at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5008)
        at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
        at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
        at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
        at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
        at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
        at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
        at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
        at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
        at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
        at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
        at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
        at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
        at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)


** END NESTED EXCEPTION **

1 个答案:

答案 0 :(得分:0)

即使我使用where子句而不是获取整个表,我也遇到相同的错误。

在我的数据库URL中添加useSSL = false可以解决此问题:

Connection con = DriverManager.getConnetion("jdbc:mysql://127.0.0.1:3306/abc?useSSL=false","username","password");