Show the Columns and Tables of a Schema
Updated: Feb 21, 2022
package com.northconcepts.datapipeline.foundations.examples.jdbc; import java.util.List; import java.util.stream.Collectors; import com.northconcepts.datapipeline.foundations.jdbc.JdbcConnection; import com.northconcepts.datapipeline.foundations.jdbc.JdbcExportedKey; import com.northconcepts.datapipeline.foundations.jdbc.JdbcImportedKey; import com.northconcepts.datapipeline.foundations.jdbc.JdbcTable; import com.northconcepts.datapipeline.foundations.jdbc.JdbcTableColumn; public class ShowTheColumnsAndTablesOfASchema { public static void main(String[] args) { JdbcConnection connection = new JdbcConnection() .setDriverClassName("org.postgresql.Driver") .setUrl("jdbc:postgresql://localhost:5432/customers") .setUsername("username") .setPlainTextPassword("password"); Listtables = connection.loadTables(null, null, "%", "TABLE").getTables(); for (JdbcTable table : tables) { System.out.println(table.getName() + " : " + table.getType()); System.out.println(" Columns"); for (JdbcTableColumn column : table.getColumns()) { System.out.println(" - " + column.getName() + " : " + column.getDatabaseTypeName() + "(" + column.getSize() + ")" + (column.isNullable()?"":" NOT NULL") + (column.isPrimaryKey()?" (PK " + column.getPrimaryKeySequence() + ")":"") + (column.isAutoIncrement()?" (AUTO)":"") + (column.isGenerated()?" (GEN)":"") + (column.isPseudo()?" (Pseudo)":"") + (column.isRowIdentifier()?" (RowID " + column.getRowIdentifierSequence() + ")":"")); } System.out.println(" ImportedKeys"); for (JdbcImportedKey key : table.getImportedKeys()) { System.out.println(" --> " + key.getForiegnKeyName() + " (" + table.getImportedKeyColumns(key.getForiegnKeyName()).stream().map(JdbcTableColumn::getName).collect(Collectors.joining(",")) + ")"); } System.out.println(" ExportedKeys"); for (JdbcExportedKey key : table.getExportedKeys()) { System.out.println(" --> " + key.getForiegnKeyName() + " (" + key.getForiegnKeyTableName() + "." + key.getForiegnKeyColumnName()+ ")"); } System.out.println("-------------------------------"); } System.out.println("Done."); } }