简体   繁体   中英

How to connect to my Heroku PostgreSQL database from Java

I have a Heroku PostgreSQL database to which I can't connect from Java. I have tried different methods but without success.

Below is my Java code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;



public class MainConfig {

    public static void main(String[] args) {

     try{
         dataSource();
         System.out.println("conected");
     }
    catch(Exception ex){
        ex.printStackTrace();
    }
    }

    public static Connection dataSource() throws URISyntaxException, SQLException {

        String ConnectionString ="jdbc:postgresql://l********n:cPd7VRjIhNVWOrZ4uWEeZWKYqV@ec2-184-73-251-115.compute-1.amazonaws.com:5432/dac36a93ohuvsd";
        String username = "****";
        String password = "***";

        return DriverManager.getConnection(ConnectionString, username, password);
    }

}

The error I get in console is:

> org.postgresql.util.PSQLException: The connection attempt failed.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:233)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:144)
    at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
    at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:21)
    at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:31)
    at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
    at org.postgresql.Driver.makeConnection(Driver.java:410)
    at org.postgresql.Driver.connect(Driver.java:280)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at MainConfig.dataSource(MainConfig.java:34)
    at MainConfig.main(MainConfig.java:14)
Caused by: java.net.UnknownHostException: lxlrnhejlzmyun:cPd7VRjIhNVWOrZ4uWEeZWKYqV@ec2-184-73-251-115.compute-1.amazonaws.com
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at org.postgresql.core.PGStream.<init>(PGStream.java:61)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:109)
    ... 12 more

I have added to my Build Path the Java driver postgresql-9.3-1102.jdbc41.jar

Another method I have tried for connecting is described bellow with the error message:

    import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;



public class MainConfig {

    public static void main(String[] args) {

     try{
         dataSource();
         System.out.println("conected");
     }
    catch(Exception ex){
        ex.printStackTrace();
    }
    }

    public static Connection dataSource() throws URISyntaxException, SQLException {
        URI dbUri = new URI(System.getenv("postgres://l**********n:cPd7VRjIhNVWOrZ4uWEeZWKYqV@ec2-184-73-251-115.compute-1.amazonaws.com:5432/dac36a93ohuvsd"));

        String username = dbUri.getUserInfo().split(":")[0];
        String password = dbUri.getUserInfo().split(":")[1];
        String ConnectionString = "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath();



        return DriverManager.getConnection(ConnectionString, username, password);
    }

}

Error

> java.lang.NullPointerException
    at java.net.URI$Parser.parse(Unknown Source)
    at java.net.URI.<init>(Unknown Source)
    at MainConfig.dataSource(MainConfig.java:23)
    at MainConfig.main(MainConfig.java:14)

What I am doing wrong?

Inorder to connect to heroku's PostgreSQL (with the required ssl) you must append the following to the url:

?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory

So the correct url as provided in the answer above is:

jdbc:postgresql://host:port/database?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory

Without these perimeters you will get the error described:

error: no pg_hba.conf entry for host 

格式应为: jdbc:postgresql://host:port/database ,而在您的代码中,请注意几个“:” jdbc:postgresql://l********n:cPd7VRjIhNVWOrZ4uWEeZWKYqV@ec2-184-73-251-115.compute-1.amazonaws.com:5432/dac36a93ohuvsd"

your url should look like - jdbc:postgresql://hostname:port/dbname?sslmode=require

copy hostname(Host) port and dbname(Database) as mentioned in heroku site

Dont forget to append ?sslmode=require in your url otherwise u won't get connected

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