import java.sql.*;
import java.util.*; 
import java.io.*;

/**
 * update the log_table on the java account
 * with a new string and date
 * 
 * assumes:
 * *   that the Oracle student database
 *     is on cedar.humboldt.edu
 *
 * *   that there is an account on that database
 *     with the username java
 *     whose password is stored in a (read-protected)
 *     file named pwd-file.txt in the same directory
 *     as this class' .class file
 * 
 * @author Ann Burroughs
 * @author modified by Sharon Tuttle
 * @version 2015-11-16
 **/

public class AddToLog
{
    /**
     * if the command-line argument is short enough,
     *    tries to add a log_table row with that
     *    argument and the current date (which includes
     *    the current time as well)
     *
     * @param args the string for log_table's 1st column
     */

    public static void main(String[] args)
    {
        // exactly 1 argument is required

        if (args.length != 1)
        {
            System.err.println("AddToLog: exactly ONE command "
                               + "argument is required!");
            System.exit(1);
        }

        // if get here -- is the argument short enough?

        if (args[0].length() > 5)
        {
            System.err.println("AddToLog: username must be < 5 "
                               + "characters long");
            System.exit(1);
        }

        // if get here, TRY to insert a row!

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

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

            // get the password from a local file

            BufferedReader fromStream;
            String username = "java";
            String password = "";

            try
            {
                fromStream = new BufferedReader(
                                 new FileReader("pwd-file.txt"));
                password = fromStream.readLine();
                fromStream.close();
            }

            catch (FileNotFoundException exc)
            {
                System.out.println("AddToLog: Could not open " +
                                   "pwd-file.txt");
                System.exit(1);
            }

            catch (IOException exc)
            {
                System.out.println("AddToLog: IOError: " +
                                   exc.getMessage());
            }              

            // connect to database on cedar

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

            // turn off JDBC's auto-commit default

            con.setAutoCommit(false);

            Statement insertStmt = con.createStatement();

            String insertString =   
                  "insert into log_table "
                + "values "
                + "('" + args[0] + "', sysdate)";

            // for debugging, and here for demo,
	        //    not a bad idea to check out your
		    //    resulting command string

            System.out.println("insert command is: \n" +
                               insertString + "\n");

            int numRowsInserted = 
                insertStmt.executeUpdate(insertString);

            if (numRowsInserted == 1)
            {
                System.out.println("YAY! 1 row inserted!");
 
                // I feel safe committing this change

                con.commit(); 
            }
            else
            {
                System.out.println("OH NO! no row inserted");
            }
 
            insertStmt.close();
            con.close();

        } // end of try-block
   
        // any exception thrown from my "outer" try
        //    will be caught and reported

        catch (Exception exc)
        {
            System.out.println("AddToLog: unexpected exception: " +
                               exc.getMessage());
        }

    } // end main method

} // end class AddToLog