2

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?

4
  • Someone moderator can delete this question? Commented Aug 16, 2016 at 19:00
  • Can you not find the spot to delete the question yourself? Posting a comment is not the way to get a moderator's attention. Rather, post a request for help on the "meta" site. Commented Aug 16, 2016 at 20:27
  • Fixed code formatting; made minor grammar upgrades. Commented Aug 16, 2016 at 20:28
  • Using Block-quote for errors will make it readable in question. Please try to avoid a scroll bar in your question always and make everything fit in the screen - either code or error. Don't post sensitive information like id, user name or passwords in questions. Commented Aug 18, 2016 at 21:35

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

13 Comments

Also worth noting that Heroku provides a JDBC_DATABASE_URL for you if you are using the standard buildpack or deploy tools. devcenter.heroku.com/articles/…
I was using the default URL (postgres://...) and adding jdbc: in front of it... omg I tried to view the JDBC_DATABASE_URL but I could not do the steps of the tutorial: I opened console, and logged, after that inserted $heroku run echo \$JDBC_DATABASE_URL but it returns !Error no app specified, how to do it?
PostgreSQL itself supports both postgres and postgresql. But JDBC needs the exact string postgresql to locate the driver.
"no app specified" means you are not in the correct directory. try adding -a <app> to the command but replace <app> with your app-name
also, what tutorial?
|
0

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")'?

5 Comments

Was not only until the version 3 of JDBC? I read somewhere newer versions don't need it. Adding it the errors persists.
public Connection getConnection() { try { try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } return DriverManager.getConnection("jdbc:postgres://osnvehqhufnxzr:TS3Qt37c_HHbGRNKw3yk7g88fp@ec2-54-225-93-34.compute-1.amazonaws.com:5432/d39mfq0odt56bv", "postgres", "root"); } catch (SQLException e) { throw new RuntimeException(e); }
@JamesB Never read about that. Make sure you properly included the required jar files into the classpath upon running.
Yes I did. Jars are in the library.
@JamesB If the database is hosted on Amazon AWS then It might require different driver than the normal one.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.