why my jbdc database funtion cant read foward slash

I have a function that reads a primary key from my database and I can't read foward slash for some reason.

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

This is my input, and this is my function for that input

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;

    }

and this is my output

FATAL: database "/databasename" does not exist

How can I fix this?

1 answer

  • answered 2019-10-08 04:01 Villat

    Your problem is at this line

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

    It should have only one forward slash

        String url = host + "/" + dbname;