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

/**
 * query for the current date from the Oracle
 * student database on the HSU campus
 * (from nrs-projects.humboldt.edu)
 * 
 * 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-18
 **/

public class GetDate
{
    /**
     * tries to query for today's date from a database
     *     and prints the result to the screen
     *
     * @param args not used here
     */

    public static void main(String[] args)
    {
        // turn off Java's looking for the IPv6 stack
        //     to avoid a 2-minute wait for V6 timeout

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

        try
        {
            // load the JDBC driver for the Oracle database
            //     software

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

            // DETOUR! -- reading a string from a file!
            // (because hard coding passwords into code
            // is a BAD. IDEA.)

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

            try
            {
                // create an input stream to read from
                //    file pwd-file.txt in the same directory
		      //    as this class file

                fromStream = new BufferedReader(
                                 new FileReader("pwd-file.txt"));

                // BufferedReader has a method readLine
                //    which reads in the next line and
		      //    treats it like a String

                password = fromStream.readLine();
                
                // and CLOSE the stream when you are done!

                fromStream.close();
            }
            catch (FileNotFoundException exc)
            {
                System.out.println("GetDate: Could not open " +
                                   "pwd-file.txt");
                System.exit(1);
            }
            catch (IOException exc)
            {
                System.out.println("GetDate: IOError: " +
                                   exc.getMessage());
            }              

            // make a Connection object from nrs-projects
            //    to cedar

            System.out.println("about to try to create a " +
	                           "Connection object");
            Connection con = DriverManager.getConnection(
                "jdbc:oracle:thin:@cedar.humboldt.edu:1521:student",
                username, password);

            // Connection object has a method createStatement --
            //     we need this for running SQL statement

            System.out.println("about to try to create Statement "
                                + "Object");

            Statement dateQueryStmt = con.createStatement();

            // it is convenient (but not required) to store
	        //    the SQL statement's text in its own String

            String dateQueryString = "select sysdate "
                                     + "from dual";

            // Statement class has a method executeQuery
            //    that expects a String argument containing
	        //    a SQL select statement.
            // it returns a ResultSet object (which we
            //    can "step" through to get the query's
            //    results)
      
            System.out.println("about to try to call executeQuery");

            ResultSet rS = 
                dateQueryStmt.executeQuery(dateQueryString);

            // rS now "contains" the query's results;
            //     call its method named next to make
            //     the first row in the result set available

            rS.next();

            // ResultSet has numerous methods for now
            //    grabbing one of the values in the "current"
            //    row of the results 
            // getString called with an int argument 
            //    giving the column POSITION whose value
	        //    we want -- starting at 1 for position 1,
		    //    and then treat it like a String

            System.out.println("about to call getString");

            String todaysDate = rS.getString(1);        

            System.out.println("TODAY's DATE: " + todaysDate);

            // COURSE STYLE STANDARD: thou SHALT close
	        //    your statement and connection objects
		    //    when done with them!

            dateQueryStmt.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("GetDate: unexpected exception: " +
                               exc.getMessage());
        }

    } // end main method

} // end class GetDate