pgjdbc/pgjdbc GitHub issues and pull requests (mirror)  
help / color / mirror / Atom feed
From: avinashyn (@avinashyn) <[email protected]>
To: pgjdbc/pgjdbc <[email protected]>
Subject: Re: [pgjdbc/pgjdbc] issue #3504: Wrong schemas fetched for a given catalog as input
Date: Mon, 03 Feb 2025 04:38:50 +0000
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>

Well Same with latest version as well.

Below is the sample code
-------------------------------------------------------------------
```
public class DBFetchPostgresSchemas {
   static final String DB_URL = "jdbc:postgresql://host:5434/postgresutf8";
   static final String USER = "user";
   static final String PASS = "password";
   
   
   public static void main(String[] args) {
	
	   try {
                getCatalogs();
		getSchemas("postgres");
		
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
   }
   
   public static void getCatalogs() throws SQLException 
   {
       Connection conn = DriverManager.getConnection(DB_URL, USER,PASS);
       ResultSet rs = null;
       try
       {
           List<String> catalogs = new ArrayList<String>();
           rs = conn.getMetaData().getCatalogs();
           while (rs.next())
           {
              String catalog = rs.getString("TABLE_CAT");
              System.out.println("catalog:"+catalog);
           }
       }
       catch (SQLException e)
       {
           e.printStackTrace();
       }
       finally
       {
    	   rs.close();
           conn.close();
       }

   }
   
  
   public static void getSchemas(String catalog) throws SQLException 
   {
       Connection conn = DriverManager.getConnection(DB_URL, USER,PASS);;
       ResultSet rs = null;
       try
       {
           List<String> schemas = new ArrayList<String>();
         
           if (catalog != null && catalog.length() > 0)
               conn.setCatalog(catalog);
           rs = conn.getMetaData().getSchemas();
           
           while (rs.next())
           {
               String schema = rs.getString("TABLE_SCHEM");
               System.out.println("schema:"+schema);
              
           }
       }
       catch (SQLException e)
       {
          e.printStackTrace();
       }
       finally
       {
           rs.close();
           conn.close();
       }
   }

   
   
}
```

===========================

Here **getCatalog()** --> gives all the catalogs present in Database. 

Version **42.6** gives only the catalog passed in JDBC url where as **42.7**  gives all the catalogs from database which is still fine but the problem arises with **getSchema()** -> It always gives schema of the one passed in JDBCUrl rather than the one passed in request.

In above example, "**postgres**" catalog is passed in request but it fetches the schema of "**postgresutf8**" catalog which is passed in db URL




view thread (25+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: github://pgjdbc/pgjdbc
  Cc: [email protected], [email protected]
  Subject: Re: [pgjdbc/pgjdbc] issue #3504: Wrong schemas fetched for a given catalog as input
  In-Reply-To: <<[email protected]>>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox