0

To start off I want to say sorry because this question got asked a billion times but I am still not sure what my mistake is.

So the problem that I have is the following:
I try to connect to my MySQL Server with host, port and such but I get the "Connection refused"-exception. It usually means that the problem is about the connection/login and means that it can't connect to the MySQL Server. I am using the exact same data when I log into that server by phpMyAdmin. Below is the error and my source code, I hope someone of you can tell me what my mistake is.

Thanks in advance.

Exception:

The last packet sent successfully to the server was 0 milliseconds ago. The 
driver has not received any packets from the server.
    at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:172)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at teabot.database.DatabaseConnection.<init>(DatabaseConnection.java:26)
    at teabot.database.DatabaseHelper.setupDB(DatabaseHelper.java:16)
    at teabot.main.Main.init(Main.java:39)
    at teabot.main.Main.main(Main.java:69)
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: 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(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:103)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:149)
    at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:165)
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:92)
    at com.mysql.cj.NativeSession.connect(NativeSession.java:152)
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:982)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:852)
    ... 9 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:173)
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:66)

Source code:

protected DatabaseConnection(String host, int port, String databaseName, String username, String password) {                
    boolean error = false;
    try {
        String url = "jdbc:mysql://" + host + ":" + port + "/" + databaseName;
        this.connection = DriverManager.getConnection(url, username, password);
    } catch (SQLException e) {
        e.printStackTrace();
        Helper.log(2, e.getMessage());
        error = true;
    }
    if (!error) {
        Helper.log(0, "Database is ready.");
    }
}

Edit: My problem was that my MySQL Server wasn't listening on that port since when I tried it out with phpMyAdmin I forgot that it was running on the same machine.

  • If you can connect with PHPMyAdmin, but can't with JDBC, then that could be an indication that PHPMyAdmin is using UNIX domain sockets to connect, and that TCP/IP (networking) is disabled or using a different port. See the linked duplicate. – Mark Rotteveel Jun 29 '18 at 06:40

2 Answers2

0

Not so much an answer, but a way you can diagnose the issue:

I generally attempt to use a tool like telnet or putty to connect to the MySQL database ont he relevant port. If you can't connect, you know the problem is not your code, but your network connection. That's what the stack track looks like to me - a network connectivity issue, rather than anything else. Perhaps firewall blocking the port?

Then I'll try using the MySQL connection tool with the username and password.

It might be some other parameters missing from the conection string.

Phil Riley
  • 116
  • 1
  • Tried that out, my MySQL server isn't listening on that port, that's the cause of the problem. –  Jun 28 '18 at 15:01
0

The strack track shows that Caused by: java.net.ConnectException: Connection refused: connect which means that mysql server is not live at the url you passed or if server is live, it does not listening on the port you passed as parameter

Chirag
  • 555
  • 1
  • 5
  • 20