I am using mysql-connector-java-8.0.15.jar in my code to connect to a remote database using com.mysql.cj.jdbc.Driver. When I deploy my code on the server the connection is refused. I tried firewall settings (is correct), directly connecting to mysql instance (works).
I debugged it and found that my code is able to connect successfully using mysql-connector-java-5.1.45.jar (older version) using jdbc : com.mysql.jdbc.Driver which is deprecated.
I am not able to use the updated library. How do I make the new library work?
Configuration of the Server :
Java:
openjdk 11.0.11 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.18.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.18.04, mixed mode, sharing)
Ubuntu 18.0.4
Edit:
Error Logs from the server while using mysql-connector-java-8.0.15.jar (the new lib) and com.mysql.cj.jdbc.Driver
Making Connection
com.mysql.cj.jdbc.exceptions.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 com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
This however works perfectly on my local system.
The method which I use to connect to the database
try {
    Class.forName("com.mysql.cj.jdbc.Driver");
    return DriverManager.getConnection("jdbc:mysql://xxx.xxx.xxx.xxx:3306/dbname", "username", "password");
} catch (ClassNotFoundException | SQLException e) {
    e.printStackTrace();
}
