使用java代码列出特定PostgreSQL数据库中的所有表

使用java代码列出特定PostgreSQL数据库中的所有表,java,sql,postgresql,Java,Sql,Postgresql,实际上,我在谷歌上搜索了一下,我需要相应的SELECT命令来执行以下PostgreSQL shell命令: \dt schemaname.* 我使用以下代码获取了所有数据库: Statement statement = (Statement) connection.createStatement(); ResultSet rs = statement .executeQuery("SELECT datnam

实际上,我在谷歌上搜索了一下,我需要相应的SELECT命令来执行以下PostgreSQL shell命令:

\dt schemaname.*
我使用以下代码获取了所有数据库:

            Statement statement = (Statement) connection.createStatement();
            ResultSet rs = statement
                    .executeQuery("SELECT datname FROM pg_database");
            while (rs.next()) {
                System.out.println("DB Name : " + rs.getString(1));
           //i need another while here to list tables 
           //inside the selected database
}
我尝试了以下陈述,但没有成功:

statement.executeQuery("SELECT table_schema,table_name FROM "
                                + rs.getString(1)
                                + " ORDER BY table_schema,table_name");
这就是我得到的错误:

org.postgresql.util.PSQLException: ERROR: relation "template1" does not exist
  Position: 37
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:374)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:254)
    at com.isiran.rayten.rg.db.bare.wrapper.PGWrap.main(PGWrap.java:64)

如果使用
psql-E
,它将响应在键入命令(如
\dt
)时运行的实际查询:

denis=# \dt public.*
********* QUERY **********
SELECT n.nspname as "Schema",
  c.relname as "Name",
  CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' END as "Type",
  pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','s','')
      AND n.nspname !~ '^pg_toast'
  AND n.nspname ~ '^(public)$'
ORDER BY 1,2;
**************************

然后,可以根据您的特定用例简化或修改这些查询。

要列出数据库中的所有表,您必须阅读table
pg\u catalog.pg\u tables
但不幸的是,您必须登录数据库。 所以在你写评论的地方

//i need another while here to list tables 
//inside the selected database

在循环表之前,您需要登录此数据库

使用
数据库元数据
对象查询信息,例如:


这将返回数据库中的所有表,您可能需要为
catalog
和/或
schemaPattern
指定值以获得更具体的结果。

连接字符串错误,您指向的是
template1
数据库。显然,您在第二个executeQuery()中使用了数据库名称作为表名我该怎么做?我知道我可以通过在shell中键入connectschema来实现这一点,但不确定如何实现这一点,因为它与代码中初始化object@connection@的方式相同,但使用不同的数据库名称。但是您需要每个数据库的用户名和密码,或者使用超级用户(例如postgres)连接到数据库。我已经用jdbc连接字符串连接到该数据库:-?是的,但您尝试列出其他数据库中的表。您无法从当前数据库中执行此操作。+1提供有用的注释,但正如Nicolai提到的,我必须首先连接到模式,我不确定如何在java中执行此操作,因为它不是一个查询,如果您遵循指向javadoc的链接,您可以看到,
schemaPattern
是第二个参数。它正在工作,但问题是它无法加载数据库名称,因此我无法传递名称,因为据我所知,它是为PostgreSQL实现的
DatabaseMetaData dbmd = connection.getMetaData();
try (ResultSet tables = dbmd.getTables(null, null, "%", new String[] { "TABLE" })) {
    while (tables.next()) {
        System.out.println(tables.getString("TABLE_NAME"));
    }
}