4

I use PostgreSQL to create my database and save my users list to it, when i try to connect db by java jdbc, i get error that say:

"java.sql.SQLException: invalid database address: jdbc:postgresql://localhost:5432/users".

i use "JDBC41 Postgresql Driver, Version 9.3-1102" from PostgreSQL website. and this is my code:

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

public class javaconnect {

private static Connection c = null;

public static Connection connectDb() {

    try {
        Class.forName("org.postgresql.Driver");
        c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/users", "postgres", "12345");
        return c;
    } catch (ClassNotFoundException | SQLException e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
        return null;
    }

}
}

Thanks.

1 Answer 1

6

As the error,

"java.sql.SQLException: invalid database address:

says your database name is incorrect . check for the database name if you have something like sql developer installed.

Valid database name should be specified here "jdbc:postgresql://localhost:5432/users" after the /localhost:5432/

Read JDBC using postgresql to connect to PostgreSQL database using jdbc

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

1 Comment

Thank you, i was used table name instead db name :D

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.