0

I am trying the following code to make connection with my database online, hosted on ElephantSQL.

private static Connection getConnection() {

        try {
            Class.forName("org.postgresql.Driver");
        }
        catch (ClassNotFoundException e) {
            System.out.println("Jar not found "+e.getMessage());
        }

        //dbUrl is given this way
        String dbUrl = "jdbc:postgres://database:password@manny.db.elephantsql.com:5432/database";

        String username = "database";
        String password = "password";

        try {
            return DriverManager.getConnection(dbUrl,username,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

But I am getting the following error:

java.sql.SQLException: No suitable driver found for < url >

I tried all the things from the similar questions which I found here Question 1 and Question 2.

But nothing worked and I am stuck. I would appreciate any help.

Joakim Danielson
  • 29,280
  • 4
  • 14
  • 35
Panda
  • 373
  • 3
  • 13

2 Answers2

1

As documented in the manual the URL for Postgres must be structured like this:

jdbc:postgresql://host:port/database

The prefix jdbc:postgres needs to be jdbc:postgresql and the part database:password@manny.db.elephantsql.com:5432 in your URL is wrong. It's hard to tell what exactly the hostname is, but I guess you need to use:

jdbc:postgresql://manny.db.elephantsql.com:5432/database
a_horse_with_no_name
  • 440,273
  • 77
  • 685
  • 758
-1

It seems that your driver jar file is not in classpath.

One of the option is put driver jar file in lib directory of your project and add jar file in classpath ( or buildpath in eclipse )

you can download dirver file from given URL https://jdbc.postgresql.org/download.html

Viren
  • 1
  • 1