Ich kann keine entfernte Datenbank verbinden. Wenn ich meine eigene Datenbank auf localhost anschließe, verbindet sie sich. Was ist los mit dir?Verbindung abgelehnt: connect, kann keine Datenbank verbinden 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-Code:
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);
}
}
ich Datenbank auf PHPMyAdmin anmelden können, habe ich keine Root-Account, es meines Freundes Datenbank ist. Ich habe überprüft, ob Port 3306 hier offen ist: http://www.yougetsignal.com/tools/open-ports/ und es ist geschlossen. Wo kann ich es öffnen? In Router-Einstellungen in "Portweiterleitung"? Welche private IP und Typ (TCP oder UDP) sollte ich einstellen, um diesen Port zu öffnen?
Können Sie Ihren Code teilen? –
Sind die URL und die Anmeldeinformationen für die entfernte Datenbank korrekt angegeben? –
Ja, sie sind Ich änderte sie, weil ich meine Datenbankadresse nicht teilen möchte – Jkrr