我是一名 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.