如何使用DriverManager为getConnection提供正确的“url”?

问题描述:

我想连接到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请求。这个解决方案运行良好,但哪一个是最好的使用方式?

我找到了解决方案。问题不在于代码。问题在于连接。我没有连接数据库的权限。有了数据库上的远程用户的权限,就可以了。