Android - Connecting with PostgreSQL

0

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

But I can not successfully connect.

I modified the code and it returns me:

  

"Error: Driver!"

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

I have modified the code and it is giving error:

  

Connection: Error Database!

Follow 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!";
    }

}    
    
asked by anonymous 19.09.2016 / 17:34

1 answer

1

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

Try this:

 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;
        }

    }
    
22.09.2016 / 20:52