如果未从localhost连接,则通信链路出现故障

时间:2016-11-18 15:03:53

标签: java mysql database

我尝试将我的java应用程序连接到我的数据库服务器(MySQL)。但它不起作用。我已经在我的JavaPlugins上为我的Spigot服务器遇到了这个问题。但是因为Spigot插件直接在VPS上,所以它们可以通过localhost进行连接,但是如果我尝试通过我的域或IPv4连接,我就会失败。

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:988)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:341)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2251)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at kochbuch.MySQL.MySQLConnect(MySQL.java:27)
    at kochbuch.Main.start(Main.java:16)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300)
    ... 25 more`

这是我的MySQL.java Class

public class MySQL {

public static Connection MySQLConnection;

public void onEnable() {
    System.out.println("");
}

public void onDisable() {
    System.out.println("");
}

public static void MySQLConnect(String host, String port, String database, String username, String password) {
    if (!MySQL.MySQLIsConnected()) {
        try {
            MySQLConnection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

public void createTable(String tablename, String[] values) {
    MySQL.executeCommand("CREATE TABLE IF NOT EXISTS " + tablename + " (" + values + ")");
}

public static void MySQLDisconnect() {
    if (MySQL.MySQLIsConnected()) {
        try {
            MySQLConnection.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

public static void executeCommand(String command) {
    try {
        MySQLConnection.createStatement().executeUpdate(command);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

public static boolean MySQLIsConnected() {
    if (MySQLConnection != null) {
        return true;
    }
    return false;
}

public static ResultSet getResult(String qry) {
    if (MySQL.MySQLIsConnected()) {
        try {
            return MySQLConnection.createStatement().executeQuery(qry);
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return null;
    }
}

感谢帮助:) Lg Jaxophon

0 个答案:

没有答案