我有一个MySqlConnection类,用于3个城市的分布式数据库。我想访问3个不同的IP,但是当程序尝试连接到节点时,JDBC会显示“通信链接失败”的异常。
代码是下一个:
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;
}
}
请注意,我有3台带有mysql服务器的电脑,并且所有电脑都有不同的IP。我在所有PC上都使用Ubuntu 17.04。谢谢你。