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.