How to connect Virtual Android with PostGreSQL on Local Machine?

1

Good morning,

I'm trying to create a connection of Android Virtual created in% with% with% with%. As my knowledge in Eclipse ADT I spent very little time trying to connect and did not have success.

public String conectarDB() throws ExecutionException {

    // Variáveis
    try {
        String driver = "com.postgresql.Driver";
        String url = "jdbc:postgresql://127.0.0.1:5432/database";
        String user = "postgres";
        String pass = "#Servicedesk#@!1";

     // Conectar
        try {
            Class.forName(driver);
            Connection con = null;
            con = (Connection) DriverManager.getConnection(url, user, pass);
            return ("Conexão: Funcionando!");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return ("Erro com Database!");
        } catch (SQLException e) {
            e.printStackTrace();
            return ("Erro com JDBC!");
        }

    } catch(Exception e) {
        e.printStackTrace();
        return ("Conexão: Erro no envio de Conexão!");
    }

}  
    
asked by anonymous 28.09.2016 / 15:16

2 answers

3

I suffered a lot with this error but I managed to solve it, it was very simple, the internet access permission was missing in AndroidManifest.xml

Just add the following line to AndroidManifest:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

After this you may encounter problems connecting to the bank if you have not previously configured access.

Also be sure to add the PostgreSQL library to the project (Android Studio):

1 - Right-click the project root;

2 - Open Module Settings;

3 - Aba Dependencies;

4 - Add a library to "+" and search for the "latest postgresql";

5 - Give OK and wait for synchronization.

    
01.12.2016 / 16:02
2

You can not make Android connections directly to a database. For this it is necessary to consume a web service.

On this site there is an example of creating and consuming a web service:

link

    
29.09.2016 / 01:05