0

I'm trying for the first time to JDBC to connect to a local database (Postgres) but I can't seem to actually made the connection. This is my code:

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

public class JDBCExample {

    public static void main(String[] argv) {

        System.out.println("-------- PostgreSQL "
                + "JDBC Connection Testing ------------");

        try {

            Class.forName("org.postgresql.Driver");

        } catch (ClassNotFoundException e) {

            System.out.println("Where is your PostgreSQL JDBC Driver? "
                    + "Include in your library path!");
            e.printStackTrace();
            return;

        }

        System.out.println("PostgreSQL JDBC Driver Registered!");

        Connection connection = null;

        try {

            connection = DriverManager.getConnection(
                    "jdbc:postgresql://localhost:5432/training", "iam47662285",
                    "MY56KZDZ");

        } catch (SQLException e) {

            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            return;

        }

        if (connection != null) {
            System.out.println("You made it, take control your database now!");
        } else {
            System.out.println("Failed to make connection!");
        }
    }

}

And this is the error that appears when i try to execute it:

-------- PostgreSQL JDBC Connection Testing ------------
PostgreSQL JDBC Driver Registered!
Connection Failed! Check output console
Nov 02, 2017 4:36:52 PM org.postgresql.Driver connect
SEVERE: Connection error: 
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "iam47662285"
    at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:438)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:222)
    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 UF2.Pruebas.BDR_BDOO.JDBCExample.main(JDBCExample.java:33)

org.postgresql.util.PSQLException: FATAL: password authentication failed for user "iam47662285"
    at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:438)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:222)
    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 UF2.Pruebas.BDR_BDOO.JDBCExample.main(JDBCExample.java:33)

I use Fedora, but i don't know if that's relevant. Anyone have any idea of why is this happening?

2
  • 3
    The message is clear: password authentication failed for user "iam47662285" for me Commented Nov 2, 2017 at 15:42
  • Yes, it seems that I needed to assign a password to the user Commented Nov 2, 2017 at 15:45

1 Answer 1

3

Your username/password is incorrect in the following code:

        connection = DriverManager.getConnection(
                "jdbc:postgresql://localhost:5432/training", "iam47662285",
                "MY56KZDZ");

Manually log in to psql and verify what the correct username/password is. If necessary ALTER USER "xxxx" WITH PASSWORD ''; to a new one that you'll remember.

Also hardcoding login details is a very bad idea.

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.