有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JDBC中PostgreSQL的java“无效数据库地址”

我使用PostgreSQL创建数据库并将我的用户列表保存到其中,当我尝试通过java jdbc连接db时,我得到的错误是:

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

我使用Postgresql网站上的“JDBC41 Postgresql驱动程序,版本9.3-1102”。 这是我的代码:

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;
    }

}
}

谢谢


共 (1) 个答案

  1. # 1 楼答案

    作为错误

    "java.sql.SQLException: invalid database address:

    表示您的数据库名称不正确。如果安装了类似sql developer的程序,请检查数据库名称

    有效的数据库名称应该在"jdbc:postgresql://localhost:5432/users"后面指定/localhost:5432/

    读取JDBC using postgresql以使用jdbc连接到PostgreSQL数据库