我無法連接遠程數據庫。當我在本地主機上連接我自己的數據庫時,它會連接。怎麼了?連接被拒絕:連接,無法連接數據庫mysql
Exception in thread "main" java.lang.IllegalStateException: Cannot connect the database!
Caused by: 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.
Caused by: java.net.ConnectException: Connection refused: connect
Java代碼:
String url = "jdbc:mysql://ipadress:3306/database?autoReconnect=true&useSSL=false";
String username = "username";
String password = "password";
String query = "SELECT * FROM database";
System.out.println("Connecting database...");
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
// handle the error
}
try (Connection connection = DriverManager.getConnection(url, username, password)) {
System.out.println("Database connected!");
//Uruchamiamy zapytanie do bazy danych
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
}
connection.close();
} catch (SQLException e) {
throw new IllegalStateException("Cannot connect the database!", e);
}
}
我可以登錄到數據庫上的phpMyAdmin,我沒有root帳戶,這是我朋友的數據庫。我檢查了端口3306是否打開:http://www.yougetsignal.com/tools/open-ports/,它已關閉。我可以在哪裏打開它?在「端口轉發」中的路由器設置?我應該設置什麼專用IP和類型(TCP或UDP)來打開此端口?
可能你可以分享你的代碼嗎? –
正確提到的遠程數據庫的URL和憑證? –
是的,他們是 我改變了他們,因爲我不想分享我的數據庫地址 – Jkrr