java怎样调用数据库中的表格,使用Java代码列出特定PostgreSQL数据库中的所有表

actually i have googled a bit and i need corresponding SELECT command to following PostgreSQL shell command :

\dt schemaname.*

i managed to get all databases with following code :

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

}

i tried following statement, but no luck :

statement.executeQuery("SELECT table_schema,table_name FROM "

+ rs.getString(1)

+ " ORDER BY table_schema,table_name");

this is the error i am getting :

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)

解决方案

To list all tables from database you have to read table pg_catalog.pg_tables

But unfortunately you have to be logged-in in database.

So in place where you wrote comments

//i need another while here to list tables

//inside the selected database

Before loop for tables you need to log-in in this database.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值