2

I have a MySqlConnection class for a distributed database in 3 cities. I want to access to 3 different IPs, but when the program tries to connect to a node, JDBC shows an exception of "Communications link failure".

The code is the next one:

public class MySqlConnection { 
    private String DB_URL;
    private String USER;
    private String PASSWORD;
    private Connection connection;    

    public Connection GetConnection(String node)
    {
        switch (node)
        {
            case "City1":
                DB_URL = "jdbc:mysql://192.168.100.8:3306/DATABASE_NAME";
                USER = "root";
                PASSWORD = "password_node1";
                break;
            case "City2":
                DB_URL = "jdbc:mysql://192.168.100.11:3306/DATABASE_NAME";
                USER = "root";
                PASSWORD = "password_node2";
                break;
            case "City3":
                DB_URL = "jdbc:mysql://192.168.100.13:3306/DATABASE_NAME";
                USER = "root";
                PASSWORD = "password_node3";
                break;
        }
        try
        {
            connection = (Connection) DriverManager.getConnection(DB_URL, USER, PASSWORD);
        }
        catch(SQLException ex)
        {
            JOptionPane.showMessageDialog(null, ex);
        }
        return connection;
    }
}

Note that I've 3 PCs with its mysql server, and all with different IPs. I'm using Ubuntu 17.04 in all PCs. Thank you instead.

  • Have you tried connecting to the single node? If that also doesn't work:You can check here. https://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai?rq=1 – Procrastinator Sep 08 '17 at 06:46
  • Yes, I tried. If I change the IP by 127.0.0.1 it works, but when I try a remote connection it fails. – Arturo Salas Sep 08 '17 at 06:50

0 Answers0