elvintaghizade14
elvintaghizade14

Reputation: 167

Connection between java and heroku postgresql

I created an app in heroku to use remote db (postgres). I can easily select, insert, delete, update in intellij idea or datagrip. But i wanna make a connection via java code:

    public class ConnectionDB {
  public static void main(String[] args) throws URISyntaxException, SQLException {
    String dbURL = System.getenv("jdbc:postgresql://----,,,,?????");
    Connection conn = DriverManager.getConnection(dbURL);
  }
}

But when i run the above pieces of code, i get:

Exception in thread "main" java.sql.SQLException: The url cannot be null
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:660)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:251)
    at connection.ConnectionDB.main(ConnectionDB.java:11)

i tried DATABASE_URL also, but the same output as JDBC_DATABASE_URL. How to solve the problem?

Upvotes: 0

Views: 109

Answers (1)

Thomas Portwood
Thomas Portwood

Reputation: 1081

The line you use to define dbURL is attempting to access an environment variable that probably does not exist.

Please remove 'System.getenv' from the line to define the dbURL, or define an environment variable 'dbURL', then access it by using System.getenv('dbURL'):

String dbURL = "jdbc:postgresql://ec2-54-247-78-30.eu-west-1.compute.amazonaws.com:5432/d3du1hdp316o87"; 
Connection conn = DriverManager.getConnection(dbURL);

or, define the environment variable 'dbURL' and then use:

String dbURL = System.getenv('dbURL');
Connection conn = DriverManager.getConnection(dbURL);

Upvotes: 1

Related Questions