/home/wpollock1/public_html/AJava/DBDump.java

// JDBC Table Dump program.  This application opens a database, selects all the
// records in a table, and using the meta-data returned prints a report (dump).
// By changing the SQL, this program can produce an arbitrary report on a join
// on a database.  The database and table must be supplied on the command line.
// Not all databases support meta-data, especially if the resultSet is empty.
// The metadata also contains info on column widths, which should be used to
// line up columns correctly.  (Omitted here for clarity.)
//
// There is no standard SQL to list the databases (the SQL standard
// uses the term schema) available on some server.  However there
// is a standard SQL query to list the schemas in a database server (the
// SQL standard uses the term catalog), but some DBMSes don't support
// schemas, or don't follow the standard (e.g., DB2 and Oracle).  Use:
//   SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA
//
// The standard SQL to list the tables in a DB/schema is:
//  SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
//     WHERE TABLE_SCHEMA = 'name'
// but not all DBMSes support it.  Oracle uses "SELECT * FROM TAB",
// and DB2 uses "SYSCAT" instead of "INFORMATION_SCHEMA".)
//
// Using INFORMATION_SCHEMA it is possible to describe (list the columns
// and their types and constraints) any table, but not all DBMSes
// support this.  For Oracle use "DESCRIBE tablename" and for DB2
// use "DESCRIBE TABLE tablename".
//
// To create an embedded Derby DB to use, run "ij" in a suitable directory
// and enter this information:
/*
   CONNECT 'jdbc:derby:AddressBook;create=true';

   CREATE TABLE addresses (
      id INT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1000),
      lname VARCHAR(40) NOT NULL,
      fname VARCHAR(40) NOT NULL,
      phone VARCHAR(14),
      notes varchar(256),
      primary key (ID) );

   INSERT INTO addresses (lname, fname, phone, notes)
      VALUES ('Pollock', 'Wayne', '253-7213', 'Professor'),
      ('Piffl', 'Hymie', NULL, 'Fake student name'),
      ('Jojo', 'Mojo', NULL, 'Super-villain');
   EXIT;
*/
// To run with this DB, use: java DBDump jdbc:derby:AddressBook addresses
//
// Written 2019 by Wayne Pollock, Tampa Florida USA.

import java.sql.*;

public class DBDump {

public static void main ( String [] args ) {
   if ( args.length != 2 ) {
      System.out.println(
         "**** Usage: java DBDump <URL> <table>" );
      return;
   }
   String URL = args[0];
   String table = args[1];
   String username = "";
   String password = "";  // Bad practice!
   String SQL_Query = "SELECT * FROM " + table;  // Security flaw!

   Connection con = null;
   Statement stmt = null;
   try {
       con = DriverManager.getConnection( URL, username, password );
       stmt = con.createStatement();
   } catch ( Exception e ) {
       System.err.println( "**** Cannot open connection to " + URL + "!" );
   }

   try {
       ResultSet results = stmt.executeQuery( SQL_Query );

       // Collect meta-data:
       ResultSetMetaData meta = results.getMetaData();
       int numColumns = meta.getColumnCount();

       // Display results:
       System.out.println( "\n\t\t\t--- " + table + " ---\n" );
       for ( int i = 1; i <= numColumns; ++i )
           System.out.printf( "%-12s", meta.getColumnLabel( i ) );
       System.out.println();

       while ( results.next() )     // Fetch next row, quit when no rows left.
       {   for ( int i = 1; i <= numColumns; ++i )
           {   String val = results.getString( i );
               if ( val == null )
                   val = "(null)";
               System.out.printf( "%-12s", val );
           }
           System.out.println();
       }
       con.close();
   }
   catch ( Exception e ) {
      e.printStackTrace();
   }
}  // End of Main
}  // End of DBDump