import java.sql.*;      // for JDBC
import java.io.*;       // for file i/o

/**
 * expects the name of a table as a command line argument,
 *    and it shows the names of the columns in that table,
 *    (printed to system output, 1 column per line)
 *
 * assumes that the Oracle student database is on 
 *     cedar.humboldt.edu, and that there is an account
 *     on that database with username java and password  
 *     that you know and have stored in a file pwd-file.txt 
 *     in the same directory as this class' .class file 
 *
 * @author Ann Burroughs
 * @author (modified by) Sharon Tuttle
 * @version 3-13-13
 */

public class SpewTableColumns
{
    /**
     * expects the name of a table as a command-line
     *    argument, and shows the names of the columns in that 
     *    table, (printed to system output, 1 column per line)
     *
     * @param args expected to contain the desired table
     *    name as the first element in this array
     */

    public static void main(String args[])
    {
        // turn off looking at IPv6 stack to avoid 2 minute 
        //    wait for V6 timeout

        System.setProperty("java.net.preferIPv4Stack", "true");

        if (args.length < 1)
	{   
            System.out.println("Need a table name as a first " +
                "argument!");
            System.exit(0);
        }

        try
	{
            String whichTable = args[0];

	    // load driver

	    Class.forName("oracle.jdbc.driver.OracleDriver");

	    String username = "java";
	    String password = getPassword();

	    // create Connection object con

	    Connection con = DriverManager.getConnection(
                "jdbc:oracle:thin:@cedar:1521:student",
		username, password);

            // set up a query to get the table's columns

            Statement stmt = con.createStatement();

            // YES THIS IS PRONE TO SQL INJECTION!!!
            // I WOULD NOT DO THIS ON THE APPLICATION TIER!

            String query = "select * " +
                           "from " + whichTable;

            ResultSet rS = stmt.executeQuery(query);

            // grab the metadata for this ResultSet

            ResultSetMetaData md = rS.getMetaData();

            // let's get the number of columns in this
	    //     result
            
            int columnCt = md.getColumnCount();

            // ...and now loop for this many columns,
	    // getting each one's name (label)

            System.out.println();

            for (int i=1; i<= columnCt; i++)
            {
                System.out.println(md.getColumnLabel(i));
            }

            System.out.println();

            stmt.close();
            con.close();

	} // end of try block

        // any exception thrown within the try-block above will be
        //    caught and reported

        catch (Exception e)
	{
	    System.out.println(e);
	}

    }  // end main

    /**
     * reads the java account password from the file pwd-file.txt,
     *   assumed to be in the same directory as this class
     *
     *   @return     the password read
     */

    private static String getPassword()
    {
	BufferedReader fromStream;  
	String password = "";
        try
	{
	    fromStream = new BufferedReader(
	        new FileReader("pwd-file.txt"));
	    password = fromStream.readLine();
	    fromStream.close();
	}
        catch (FileNotFoundException exc)
	{
	    System.out.println(
                "Could not open: "
                + "pwd-file.txt");
	}
        catch (IOException exc)
	{
	    System.out.println(    
               "IOError: " +
               exc.getMessage());
	}
        return password;
    }
     
}  // end SpewTableColumns class