import java.sql.*;    // for JDBC
import java.util.*;   // for the Scanner class
import java.io.*;     // for the File class (I think)

/**
    to try to query the current date from the Oracle
    student database <br /><br />

    this assumes there is a file in the local directory
    named pwd-file.txt containing the password to the
    Oracle account with the username java

    (This is using a Java-8-friendly approach, suitable
    for use on nrs-projects...)

    @version 2021-11-15
    @author Sharon Tuttle
*/

public class GetDate
{
    /**
        tries to query for today's date from Oracle student
        database and print it to the screen

        @param args not used here
    */

    public static void main(String[] args)
    {
        String todaysDate; 

        // turn off looking at IPv6 stack to avoid a 2 minute
        //     wait for V6 timeout

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

        // some of the JDBC methods MAY throw exceptions...

        try
        {
            // load jdbc driver for Oracle

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

            // get password for java account from pwd-file.txt

            Scanner inFile = null;

            try
            {
                inFile = new Scanner(new File("pwd-file.txt"));
            }
            catch (FileNotFoundException exc)
            {
                System.err.println("GetDate: could not open "
                                   + "pwd-file.txt; Goodbye!");
                System.exit(1);
            }

            // but if I reach here, now my Scanner methods
            //     let me read from this file!

            String password = inFile.nextLine();
            inFile.close();

            // I know the account is java

            String username = "java";

            // make a Connection object!

            System.out.println("about to create Connection object");

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

            // make a Statement object!

            System.out.println("about to create Statement object");

            Statement stmt = con.createStatement();

            // for convenience/readability, I am storing the
            //     SQL command string in a variable

            // DO NOT PUT ; in QUERY STRING

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

            System.out.println("about to run Statement's executeQuery method");

            ResultSet rS = stmt.executeQuery(dateQuery);

            // NOTE: this PARTICULAR query ALWAYS returns JUST ONE ROW!
            //    (so I am not looping through THIS ResultSet!)
            // BUT I still need to call rS.next() to REACH thst first row!

            System.out.println("about to call rS.next()");

            rS.next();

            // there is ONE column in this tabular result,
            //     and I want to treat it as a string, and so:

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

            todaysDate = rS.getString(1);

            System.out.println(todaysDate);

            // CLASS STYLE STANDARD: CLOSE your Statement and
            //    ESPECIALLY your Connection when done!

            stmt.close();
            con.close();
        }
        catch (Exception exc)
        {
            System.out.println(exc);
        }
    }
}