Android - Connection to Postgresql

Asked

Viewed 403 times

0

I’m trying to connect the Android directly with PostgreSQL.
Include in the project package: postgresql-9.1-903.jdbc4.jar and I’m using PostgreSQL 9.1.

But I can’t successfully make the connection.

I modified the code and it returns me:

"Error: Driver!"

I’m using JDBC: postgresql-9.1-903.jdbc4.jar.


I modified the code and it’s wrong:

Connection: Database Error!

Follows the code:

// Conectar
public String conectarDB() {

    // Variáveis
    String driver = "org.postgresql.Driver";
    String dbURL = "jdbc:postgresql://localhost:5432/database";
    String user = "postgres";
    String pass = "123456";

    try{

        //Carrega o driver
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(dbURL, user, pass);
        return "Conexão: Ok!";

    } catch(ClassNotFoundException e) {
        e.printStackTrace();
        return "Conexão: Erro Driver!";

    } catch (SQLException e) {
        e.printStackTrace();
        return "Conexão: Erro Database!";
    }

}    
  • 1

    Is there an error? has log?

  • How do I view the log and send it to you?

  • Add this to (Catch exception e) { e.printStackTrace(); Return false;} and check the bottom tab Android Monitror

  • I’m not using Android Studio is too heavy for machine I’m using. Even without Postgresql there is no error even with } catch(Exception e) { e.printStackTrace(); Return false; }

1 answer

1

There is a need to load the Driver before connecting, second this documentation.

Try it this way:

 public boolean conectarDB() {

        try{
            //Carrega o driver
            Class.forName("org.postgresql.Driver");
            String dbURL = "jdbc:postgresql://localhost:5432/database";
            String user = "postgres";
            String pass = "123456";
            Connection conn = DriverManager.getConnection(dbURL, user, pass);

            if (conn != null) {
                return true;
            } else {
                return true;
            }

        } catch(Exception e){
            e.printStackTrace();
            return false;
        }

    }
  • Already include. Unfortunately the error continues. Thank you!

Browser other questions tagged

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