Veeresh
Veeresh

Reputation: 81

Error connect postgreSQL from Java eclipse

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!!

Upvotes: 1

Views: 3291

Answers (2)

MikeB
MikeB

Reputation: 2442

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

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1499770

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.

Upvotes: 7

Related Questions