public static void main (String[] args) {
String host = "jdbc:mysql://ec2-user@ec2-xx-xxx-xx-xxx.compute-1.amazonaws.com:3306/";
String uname = "root";
String db = "customers";
String password = "somepassword";
String driver = "com.mysql.jdbc.Driver";
int i = 0;
try {
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(host+db, uname, password);
Statement st = conn.createStatement();
i = st.executeUpdate("INSERT INTO users " +
"VALUES ('Simpson1', 'mail@example.com', 'Springfield')");
if(i==1)
System.out.println("Sucessfully Updated table");
conn.close();
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
我正在尝试连接到AWS DB,但是在运行上述程序时出现以下异常和错误:
Got an exception! 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.
我尝试连接的主机名是否有错误?
答案 0 :(得分:2)
根据数据库的连接字符串和URL,我看到您正在连接到EC2中安装的数据库。我假设您正尝试从外部互联网连接到实例。
请检查以下设置
确认是否存在连接/连接设置问题的最佳试金石是尝试在连接数据库实例的同一ec2实例中运行上述代码。
答案 1 :(得分:1)
似乎你必须向世界开放3306端口(顺便说一下这不是一个好主意,你可能想在生产中修改它)。 要做到这一点,你需要