0

I tried to connect my java application to my database server (MySQL). But it doesn't work. I already had this problem at my JavaPlugins for my Spigot server. But because the Spigot plugins are on the VPS directly they can connect over localhost and this works but if I try to connect trough my domain or IPv4 I have this failure.

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`

and this is my 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;
    }
}

Thanks for Helping :) Lg Jaxophon

jrbedard
  • 3,662
  • 5
  • 30
  • 34
  • Maybe you should allow remote access. Look at this thread: http://stackoverflow.com/questions/14779104/how-to-allow-remote-connection-to-mysql – Seblor Nov 18 '16 at 16:01
  • I did! No changes :( – Jaxophon Nov 18 '16 at 18:13
  • Even after restarting the service ? Weird... – Seblor Nov 18 '16 at 18:14
  • Yes i restarted the mysql service – Jaxophon Nov 18 '16 at 18:16
  • Just to be sure, you already tried what's written on this thread ? http://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai – Seblor Nov 18 '16 at 18:16
  • 1. Hostname in JDBC URL is not recognized by local DNS server. How can i test this 2. Verify if mysqld is started without the --skip-networking option. Whats that? – Jaxophon Nov 18 '16 at 18:20
  • For the DNS thing, you can ignore it since you still have the problem when using the IPv4. For the second part, I have this (from a french website) : "Check if MySQL is configured to accept remote access by reading `etc/my.cnf`. it should not contain lines like: `skip-networking` or`bind-address`." – Seblor Nov 18 '16 at 18:25
  • Nothing thound :) and the user I use has grant permissions :) Something else? – Jaxophon Nov 18 '16 at 18:36
  • No firewall / proxy / anto-virus ? – Seblor Nov 18 '16 at 18:43

0 Answers0