JDBC How to print all table names from a database?
By:Roy.LiuLast updated:2019-08-11
A JDBC example to connect to a PostgreSQL, and print out all the tables from the default database postgres
pom.xml
<dependencies> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.2.5</version> </dependency> </dependencies>
P.S Tested with Java 8 and postgresql jdbc driver 42.2.5
PrintAllTables.java
package com.mkyong.jdbc; import java.sql.*; public class PrintAllTables { public static void main(String[] argv) { System.out.println("PostgreSQL JDBC Connection Testing ~"); try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException e) { System.err.println("Unable to find the PostgreSQL JDBC Driver!"); e.printStackTrace(); return; // default database: postgres // JDK 7, auto close connection with try-with-resources try (Connection connection = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/postgres", "postgres", "password")) { DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet rs = metaData.getTables(null, null, "%", null)) { ResultSetMetaData rsMeta = rs.getMetaData(); int columnCount = rsMeta.getColumnCount(); while (rs.next()) { System.out.println("\n----------"); System.out.println(rs.getString("TABLE_NAME")); System.out.println("----------"); for (int i = 1; i <= columnCount; i++) { String columnName = rsMeta.getColumnName(i); System.out.format("%s:%s\n", columnName, rs.getString(i)); } catch (SQLException e) { System.err.println("Something went wrong!"); e.printStackTrace(); return;
Output
---------- pg_aggregate_fnoid_index ---------- table_cat:null table_schem:pg_catalog table_name:pg_aggregate_fnoid_index table_type:SYSTEM INDEX remarks:null ---------- pg_am_name_index ---------- table_cat:null table_schem:pg_catalog table_name:pg_am_name_index table_type:SYSTEM INDEX remarks:null ---------- pg_am_oid_index ---------- table_cat:null table_schem:pg_catalog table_name:pg_am_oid_index table_type:SYSTEM INDEX remarks:null ---------- pg_amop_fam_strat_index ---------- table_cat:null table_schem:pg_catalog table_name:pg_amop_fam_strat_index table_type:SYSTEM INDEX remarks:null ---------- pg_amop_oid_index ---------- table_cat:null table_schem:pg_catalog table_name:pg_amop_oid_index table_type:SYSTEM INDEX remarks:null ---------- pg_amop_opr_fam_index ---------- table_cat:null table_schem:pg_catalog table_name:pg_amop_opr_fam_index table_type:SYSTEM INDEX remarks:null //... ---------- pg_stat_progress_vacuum ---------- table_cat:null table_schem:pg_catalog table_name:pg_stat_progress_vacuum table_type:SYSTEM VIEW remarks:null ---------- pg_stat_replication ---------- table_cat:null table_schem:pg_catalog table_name:pg_stat_replication table_type:SYSTEM VIEW remarks:null ---------- pg_stat_ssl ---------- table_cat:null table_schem:pg_catalog table_name:pg_stat_ssl table_type:SYSTEM VIEW remarks:null //...
From:一号门
COMMENTS