0

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?

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.