简体   繁体   中英

PostgreSQL JDBC Driver not working for Heroku DB Connection

I have a class that tries to connect to a Heroku database:

public class ConnectionFactory {

    public Connection getConnection() {
        System.out.println("Conectando ao banco");
        try {           
            return DriverManager.getConnection("connectionstring", "username", "password");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

What it returns is:

java.lang.RuntimeException: java.sql.SQLException: No suitable driver found for jdbc:postgres://osnvehqhufnxzr:TS3Qt37c_HHbGRNKw3yk7g88fp@ec2-54-225-93-34.compute-1.amazonaws.com:5432/d39mfq0odt56bv

I already tried postgresql-9.3-1103.jdbc3.jar and postgresql-9.4.1209.jre6.jar in the Build Path of the project. What I am doing wrong?

Use postgresql in your JDBC URL and not postgres .

Also, you need to change your DB password (because you posted it publicly) by running this command:

$ heroku pg:credentials DATABASE --reset

You need to load the driver class first by the class loader:

Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection(...);

Also see: What is the purpose of 'Class.forName("MY_JDBC_DRIVER")'?

The solution was simple, System.getenv("JDBC_DATABASE_URL") returns it in server and it does the correct configuration of login and password.

public Connection getConnection() throws URISyntaxException, SQLException 
{   
String urlDB = System.getenv("JDBC_DATABASE_URL");          
return DriverManager.getConnection(urlDB);  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM