How to connect Virtual Android with Postgresql on Local Machine?

Asked

Viewed 970 times

1

Good morning,

I’m trying to make a connection to Android Virtual servant in the Eclipse ADT with JDBC do PostGreSQL 9.1. To my knowledge in Android it’s little I spent a lot of time trying to connect and I didn’t have any 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!");
    }

}  
  • Are you getting any of the bugs?

  • No... Just display: "Connection: Database Error!"

  • Then you know it’s a Sqlexception, try trading localhost for 127.0.0.1

  • If it is String driver = "org.postgresql.Driver"; = Error with JDBC If it is String driver = "with.postgresql.Driver"; = Error with Database

  • Need to configure something on Androidmanifest.xml? For Android Virtual recognizes something?

2 answers

3


I suffered a lot with this error but I was able to solve, it was very simple, Androidmanifest.xml Internet access was missing.

Just add the following line to Androidmanifest:

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

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

Also make sure to add the Postgresql library to the project (Android Studio):

1 - Right click on the project root;

2 - Open Module Settings;

3 - Aba Dependencies;

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

5 - Give OK and wait to sync.

  • Exceptional. I will test. Thank you!

2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.