为什么我的jbdc数据库功能无法读取前斜线

时间:2019-10-08 03:09:35

标签: java postgresql jdbc

我有一个从数据库中读取主键的函数,由于某种原因,我无法读取正斜杠。

host: jdbc:postgresql://databaseurl
    dbname: databasename
    dbuser: databaseuser
    dbpass: databasepass

这是我的输入,这是我输入的功能

public static String pkDatabase(String host, String dbname, String dbuser, String dbpass, String table)
            throws IOException, SQLException {

        String url = host + "//" + dbname;
        String user = dbuser;
        String password = dbpass;

        Connection conn = DriverManager.getConnection(url, user, password);

        DatabaseMetaData meta = conn.getMetaData();

        ResultSet rs1 = meta.getTables(null, null, table, new String[] { table });
        rs1 = meta.getPrimaryKeys(null, null, table);

        ArrayList<String> arr = new ArrayList<String>();
        while (rs1.next()) {
            arr.add(rs1.getString(4));
        }

        String listString = "";

        for (String s : arr) {
            listString += s;
        }

        return listString;

    }

这是我的输出

FATAL: database "/databasename" does not exist

我该如何解决?

1 个答案:

答案 0 :(得分:0)

您的问题在这一行

    String url = host + "//" + dbname;

它只能有一个正斜杠

    String url = host + "/" + dbname;