import java.sql.*; // for JDBC
import java.util.*; // for Scanner and its related exceptions
import java.io.*; // for more file-related stuff
/**
* grabs the current date from the Oracle student database
* (1st JDBC example)
*
* @author Ann Burroughs
* @author (modified by) Sharon Tuttle
* @version 3-11-13
*/
public class GetDate
{
/**
* tries to query for today's date from the
* HSU Oracle student database and print
* it to the screen
*
* @param args not used
*/
public static void main(String args[])
{
// turn off looking that the IPv6 stack to avoid
// a 2 minute wait for V6 timeout
System.setProperty("java.net.preferIPv4Stack", "true");
try
{
// load jdbc driver for Oracle
Class.forName("oracle.jdbc.driver.OracleDriver");
BufferedReader fromStream;
String username = "java";
String password = "";
// read this username's password from a local
// file pwd-file.txt (which we'll give
// permission 600!!!)
try
{
fromStream = new BufferedReader(
new FileReader("pwd-file.txt"));
password = fromStream.readLine();
fromStream.close();
}
catch (FileNotFoundException exc)
{
System.out.println("GetDate: could not " +
"open pwd-file.txt");
}
catch (IOException exc)
{
System.out.println("GetDate: IOError: " +
exc.getMessage());
}
// make a Connection object
// (make the connection to Oracle on
// on cedar, for this username and password)
System.out.println("about to make a Connection");
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@cedar:1521:student",
username, password);
// Connection class has a method createStatement,
// it returns a Statement object.
System.out.println("about to make a Statement object");
Statement stmt = con.createStatement();
// it is convenient, but not required,
// to store the Oracle statement desired
// in a String variable
String query = "select sysdate " +
"from dual"; // NO ; IN QUERY STR!!!!
// now -- Statement has an executeQuery method
// it expects a String containing the desired
// query
// it returns a ResultSet object
System.out.println("about to try executeQuery");
ResultSet rS = stmt.executeQuery(query);
// ResultSet method next
// tries to iterate to the "next" row
// in the ResultSet -- you DO have to
// have to call it for the 1st row
// (returns false if it can't)
rS.next();
// ResultSet has MANY get methods for getting
// values from the current row
// (some ARE by position, others ARE by
// column name), and you ALSO indicate
// by the choice of method what TYPE
// Java should try to treat that value as
// (1 is the FIRST projected result)
String todaysDate = rS.getString(1);
System.out.println(todaysDate);
// COURSE STYLE STANDARD -- CLOSE your statement
// and connection when you are done!!!
stmt.close();
con.close();
}
// any other exceptions? catch and output here:
catch (Exception exc)
{
System.out.println(exc);
}
} // end main
} // end GetDate class