How to group database catalogs and schemas using JDBC?

206 Views Asked by At

I am aware that catalogs and schemas are not the same in all databases. In some databases what is known as schema is treated as catalog and in some others the opposite.

In some databases both catalogs and schemas information is available.

I am using the following code to obtain schemas

            result = databaseMetaData.getSchemas();
            schemaList = new ArrayList<String>();
            while (result.next()) {
                schemaList.add(result.getString("TABLE_SCHEM"));
            }

And for catalogs,

            result = databaseMetaData.getCatalogs();
            catalogs = new ArrayList<String>();
            while (result.next()) {
                catalogs.add(result.getString("TABLE_CAT"));
            }

In such cases I want to group the schemas which belong to a particular catalog. But I couldn't get the API. How to do it?

I have tried with the following too. Here the result set is empty.

ResultSet metaDataSchemas = metaData.getSchemas(null, null);
1

There are 1 best solutions below

2
On

you were so close try

  result = databaseMetaData.getSchemas();

        while (result.next()) {
         String catalog= result.getString(2);
        }