2

I'm trying to connect to a database using the following code:

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

public class Model {

        Connection connection;

        Model() {

                try {
                        connection = DriverManager.getConnection("jdbc:postgresql:localhost:5432/deliverp", "kais", "0000");
                }
                catch (SQLException e) {
                        e.printStackTrace();
                }
        }

        static {

                try {
                        Class.forName("org.postgresql.Driver");
                }
                catch (ClassNotFoundException e) {
                        e.printStackTrace();
                }
        }

        public boolean addClient(Object data[]) {

                return true;
        }
}

But after running the Java program I receive the following stack trace:

Nov 17, 2017 8:58:24 AM org.postgresql.Driver connect
SEVERE: Connection error: 
org.postgresql.util.PSQLException: FATAL: database "localhost:5432/deliverp" does not exist
        at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2477)
        at org.postgresql.core.v3.QueryExecutorImpl.readStartupMessages(QueryExecutorImpl.java:2603)
        at org.postgresql.core.v3.QueryExecutorImpl.<init>(QueryExecutorImpl.java:125)
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:227)
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
        at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:194)
        at org.postgresql.Driver.makeConnection(Driver.java:450)
        at org.postgresql.Driver.connect(Driver.java:252)
        at java.sql.DriverManager.getConnection(DriverManager.java:664)
        at java.sql.DriverManager.getConnection(DriverManager.java:247)
        at Model.<init>(Model.java:16)
        at DelivERP.<init>(DelivERP.java:42)
        at DelivERP.main(DelivERP.java:104)

org.postgresql.util.PSQLException: FATAL: database "localhost:5432/deliverp" does not exist
        at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2477)
        at org.postgresql.core.v3.QueryExecutorImpl.readStartupMessages(QueryExecutorImpl.java:2603)
        at org.postgresql.core.v3.QueryExecutorImpl.<init>(QueryExecutorImpl.java:125)
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:227)
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
        at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:194)
        at org.postgresql.Driver.makeConnection(Driver.java:450)
        at org.postgresql.Driver.connect(Driver.java:252)
        at java.sql.DriverManager.getConnection(DriverManager.java:664)
        at java.sql.DriverManager.getConnection(DriverManager.java:247)
        at Model.<init>(Model.java:16)
        at DelivERP.<init>(DelivERP.java:42)
        at DelivERP.main(DelivERP.java:104)

Therefore I wanted to make sure that the database exists by running the test below:

kais@debian:~/Documents/DelivERP$ psql deliverp
psql (9.6.4)
Type "help" for help.

deliverp=> \d
             List of relations
 Schema |      Name      | Type  |  Owner   
--------+----------------+-------+----------
 public | BusinessEntity | table | postgres
 public | Client         | table | postgres
 public | Company        | table | postgres
 public | Order          | table | postgres
 public | Product        | table | postgres
(5 rows)

deliverp=> 

It sounds to me weird that the server didn't find the database although it exists ..

Any thoughts?

2
  • 1
    Your URL is wrong, it should be jdbc:postgresql://localhost:5432/deliverp (note the // before localhost). Commented Nov 17, 2017 at 15:36
  • @MarkRotteveel Yup, right. Commented Nov 17, 2017 at 16:00

1 Answer 1

4

You can try:

connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/deliverp", "kais", "0000");

or

connection = DriverManager.getConnection("jdbc:postgresql:deliverp", "kais", "0000");
Sign up to request clarification or add additional context in comments.

1 Comment

And it was a URL issue .. Thanks for pointing that out - problem solved :)

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.