0

Iam tryig to connect to a Postgres Database. Iam really new to that and have read a post in the forum. But I didn't manage it.

public void connect() {
    //Connection con = null;
    try {
        Class.forName("org.postgresql.Driver");

        Properties props = new Properties();
        props.setProperty("user", user);
        props.setProperty("password", password);
        props.setProperty("ssl","true");
        Connection conn = DriverManager.getConnection(url, props);
        //String url = "jdbc:postgresql://localhost/test?user=fred&password=secret&ssl=true";
        //Connection conn = DriverManager.getConnection(url);
        System.out.println("Erfolgreich verbunden!");
    }
    catch (Exception  e){
        e.printStackTrace();
        System.err.println(e.getClass().getName()+": "+e.getMessage());
        System.exit(0);
    }
}

EDIT: I updated my Code. The database is deployed to heroku. It throws the error:

java.sql.SQLException: No suitable driver found for jdbc:postgres://vuqmbekwlgohkw:******
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:189)
at com.company.Database.connect(Database.java:20) p
at com.company.Main.start(Main.java:16)
at com.company.Main.main(Main.java:25)
java.sql.SQLException: No suitable driver found for jdbc:postgres://vuqmbekwlgohkw:***************
14
  • InitialContext and look ups by JNDI name is for Application Servers (JEE technology) if you are not developing an JEE application you should simply follow the JDBC connection examples like here: jdbc.postgresql.org/documentation/80/connect.html Commented Jan 31, 2019 at 23:49
  • Thank you but this leads to the error: java.sql.SQLException: No suitable driver found for jdbc:postgres.
    – F4ll0ut
    Commented Feb 1, 2019 at 0:14
  • 1
    The code you have now posted does not throw this exception. Unclear what you're asking.
    – user207421
    Commented Feb 3, 2019 at 1:10
  • 1
    Only you think that NoInitialContextException and SQLException are the same thing, which they aren't.
    – user207421
    Commented Feb 3, 2019 at 1:18
  • 1
    I treat everybody the same here.
    – user207421
    Commented Feb 3, 2019 at 1:22

2 Answers 2

1

I believe that your problem is that your connection URL is malformed.

When DriverManager.getConnection throws SQLException, the message includes the exact url value passed to the function. In your case, that looks like jdbc:postgres://vuqmbekwlgohkw:******.

But obviously that is not the URL you are using. You have replaced part of the URL with asterisks. That suggests that you think the format of the URL is:

jdbc:postgres://username:password@host:port/dbname

which seems to be what Heroku provides in the DATABASE_URL environment variable. You are using asterisks to prevent us from seeing the password.

However, it looks like the PostgreSQL JDBC driver does not accept URLs in this format. When I tried, I also got the "No suitable driver" error. According to the documentation, the format of the URL is:

jdbc:postgres://host:port/database

Some parts are optional, but the driver does not appear to support putting the user name or password in the URL.

I was able to connect to an AWS PostgreSQL instance by using the URL format described in the documentation and using the connection properties to set the user name and password.

0
0

You said you are new so I'm gonna start with the painfully obvious, do you have the driver on your classpath?

it's a jar you add to your project that you can download from https://jdbc.postgresql.org/

Otherwise I have an example of the url that worked for me, except I was not exactly using jdbc, it was a spring-boot project:

jdbc:postgresql://ec2-174-99-88-88.compute-1.amazonaws.com:5432/asdfasdfsadf?sslmode=require

There is no port number in your commented URL and not all parameters might be supported, like Willis pointed out.

1
  • I think he has the driver on his classpath, because he reported that adding the Class.forName did nothing. If the driver wasn’t on the the classpath then trying to get a reference to it would have thrown an exception. Commented Feb 3, 2019 at 21:22

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.