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?

techspider
  • 3,370
  • 13
  • 37
  • 61
JamesB
  • 569
  • 1
  • 9
  • 31
  • Someone moderator can delete this question? – JamesB Aug 16 '16 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. – Prune Aug 16 '16 at 20:27
  • Fixed code formatting; made minor grammar upgrades. – Prune Aug 16 '16 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. – techspider Aug 18 '16 at 21:35

3 Answers3

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
codefinger
  • 10,088
  • 7
  • 39
  • 51
  • Also worth noting that Heroku provides a `JDBC_DATABASE_URL` for you if you are using the standard buildpack or deploy tools. https://devcenter.heroku.com/articles/connecting-to-relational-databases-on-heroku-with-java#using-the-jdbc_database_url – codefinger Aug 16 '16 at 17:52
  • 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? – JamesB Aug 16 '16 at 18:31
  • PostgreSQL itself supports both `postgres` and `postgresql`. But JDBC needs the exact string `postgresql` to locate the driver. – codefinger Aug 16 '16 at 18:33
  • "no app specified" means you are not in the correct directory. try adding `-a ` to the command but replace `` with your app-name – codefinger Aug 16 '16 at 18:34
  • also, what tutorial? – codefinger Aug 16 '16 at 18:34
  • From the link you pasted "Using the JDBC_DATABASE_URL", the steps. – JamesB Aug 16 '16 at 18:38
  • Did you try putting `postgresql` into your app? – codefinger Aug 16 '16 at 18:39
  • In my app? In dashboard the database that I've created appears as a app itself. I have that other that was my application where I upload my war file. – JamesB Aug 16 '16 at 18:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121092/discussion-between-jamesb-and-codefinger). – JamesB Aug 16 '16 at 18:51
  • the URL needs to be `jdbc:postgresql://...` – codefinger Aug 16 '16 at 20:16
  • In your post you write `jdbc:postgres://...`. note the `ql` – codefinger Aug 16 '16 at 21:12
  • Can I test it locally? Without uploading a war file every time I wanna test any change? – JamesB Aug 17 '16 at 00:26
  • Read the instructions in the previous link for connecting remotely https://devcenter.heroku.com/articles/connecting-to-relational-databases-on-heroku-with-java#connecting-to-a-database-remotely – codefinger Aug 17 '16 at 02:42
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")'?

Community
  • 1
  • 1
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • Was not only until the version 3 of JDBC? I read somewhere newer versions don't need it. Adding it the errors persists. – JamesB Aug 16 '16 at 17:42
  • 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 Aug 16 '16 at 17:44
  • @JamesB Never read about that. Make sure you properly included the required jar files into the classpath upon running. – Eng.Fouad Aug 16 '16 at 17:44
  • Yes I did. Jars are in the library. – JamesB Aug 16 '16 at 17:47
  • @JamesB If the database is hosted on Amazon AWS then It might require different driver than the normal one. – Eng.Fouad Aug 16 '16 at 17:49
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);  
}
JamesB
  • 569
  • 1
  • 9
  • 31