私は Java の初心者で、JDBC を使用してリモートの mysql データベースに接続する単純なアプリケーションに取り組んでいます。ローカルでテストしましたが、問題なく動作しますが、リモート サーバーで動作させることができません。あまり役に立たないと思いますが、コードは次のとおりです。
Connection connection = null;
String dburl = "jdbc:mysql://314159265:3306/Db_Name";
String userName = "user";
String passWord = "password";
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(dburl, userName, passWord);
Statement st = connection.createStatement();
String query = "INSERT INTO Example (`TestColumn`) VALUES('hello')";
int rsI = st.executeUpdate(query);
System.out.println("Hi");
}catch (Exception e) {
System.out.println(e);
} finally {
if (connection != null) {
try {
connection.close();
System.out.println("Database connection terminated");
} catch (Exception e) { /* ignore close errors */ }
}
}
これを実行すると、次のメッセージが表示されます。
com.mysql.jdbc.exceptions.jdbc4.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.
ある種のサーバー構成の問題であるに違いないと確信しています。
Notes:
Username, password, IP, database name, etc. are just examples.