2012-07-18 18 views
0

我想連接到Android應用程序中的MySQL數據庫,但無法這樣做。如何使用DriverManager爲getConnection提供正確的「url」?

我用於連接到MySQL數據庫的URL是:jdbc:mysql://xx.xx.x.xxx/dbname,其中xx.xx.x.xxx表示我的服務器的IP地址。數據庫dbname由此服務器上的phpMyAdmin管理。

但我總是收到相同的錯誤:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

我在我的清單中的Internet權限。

我正在使用的代碼:

private void callSQL() { 
    java.sql.Connection laConnection; 
    java.sql.Statement transmission; 
    ResultSet leResultat; 

    try { 
      Class.forName("com.mysql.jdbc.Driver").newInstance(); 
      laConnection= DriverManager.getConnection("jdbc:mysql://xx.xx.x.xxx/dbname", "user", "passwrd"); 

      transmission = laConnection.createStatement(); 
      leResultat= transmission.executeQuery("select * from Communes"); 

      while (leResultat.next()) { 
       System.out.println("Commune : "+ leResultat.getString("Nom")); 
      } 
    }catch(Exception e){ 
     System.out.println(e); 
    } 
} 

我已經能夠用另一種解決方案與連接到數據庫的PHP頁面的HTTP請求。這個解決方案運行良好,但哪一個是最好的使用方式?

回答

0

我找到了解決方案。問題不在於代碼。問題在於連接。我沒有連接數據庫的權限。有了數據庫上的遠程用戶的權限,就可以了。

相關問題