1

I am trying to connect to postgreSQL from Java eclipse

Here is the snapshot of the database structure. Data base Structure

connection = DriverManager.getConnection(
                    "jdbc:postgresql://localhost:5432/postgres", "postgres",
                    "admin");

when i am trying to connect to postgres database it works absolutely fine. but that is the default database which i don't want to connect.

 connection = DriverManager.getConnection(
                        "jdbc:postgresql://localhost:5432/postgres", "han2012205",
                        "admin");

when i tried to access the han2012205 it is throwing this error.

org.postgresql.util.PSQLException: FATAL: password authentication failed for user "han201205"
    at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:398)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:173)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:136)
    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:393)
    at org.postgresql.Driver.connect(Driver.java:267)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at connectToDB.dbConnection.connectdatabase(dbConnection.java:26)
    at AdapterTest.main(AdapterTest.java:22)

Can some please help me with my problem

Hoping to hear back from some one soon!!

2 Answers 2

7

You've misunderstood the call you're making. You're using DriverManager.getConnection(String, String, String) - where the second parameter is the user, and the third parameter is the password. That's not the way to specify the database you want to connect to.

I suspect you want:

connection = DriverManager.getConnection(
                "jdbc:postgresql://localhost:5432/han201205", "postgres",
                "admin");

Note how the database name is the last part of the JDBC connection URL, not the second parameter. (You may need to just the username and password, of course...)

See the Postgres JDBC documentation for more details.

Sign up to request clarification or add additional context in comments.

Comments

2

Also, the database is called "han201205", your code was looking for "han2012205" with an extra "2"

1 Comment

the extra 2 i added here by mistake. i am sure that is not the problem. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.