public static void main (String[] args) {
String host = "jdbc:mysql://[email protected]: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', '[email protected]', '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.
我要连接的主机名是否错误?
根据数据库的连接字符串和URL,我看到您正在连接到EC2中安装的数据库。我假设您正在尝试通过/通过外部互联网连接到实例。
请检查以下设置
确认是否存在连接/连接设置问题的最好的石蕊测试是尝试在连接数据库实例的同一ec2实例中运行上述代码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句