4
votes

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

1 Answers

6
votes

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