/*
 * This can be used to check the JDBC connection to limani.cs.uchicago.edu.
 * Just run it and provide the connect information.  It will select
 * sample data from one of the sample tables in the oracle database.
 */

// You need to import the java.sql package to use JDBC
import java.sql.*;

// We import java.io to be able to read from the command line
import java.io.*;

class JdbcTest
{
  public static void main (String args [])
       throws SQLException, IOException
  {
    // Load the Oracle JDBC driver
	try {
   // EITHER style will obtain the oracle driver:
   //   DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
	Class.forName("oracle.jdbc.OracleDriver").newInstance();
	}
	catch ( Exception e)
	{ e.printStackTrace(); }

    // Connect to the database

    System.out.println ("Connecting...");
    // THIS connection is over the thin client driver
    Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@limani.cs.uchicago.edu:1521:cs51024","matei", "matei");

    System.out.println ("connected.");

    // Create a statement
    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO xxxx VALUES(?)");        
    java.util.Date d = new java.util.Date();

    pstmt.setDate( 1, new java.sql.Date(d.getTime()));   
    pstmt.executeUpdate( );  

    // Create a statement
    Statement stmt = conn.createStatement ();

    // Gather some rows
    ResultSet rset = stmt.executeQuery("select * from xxxx");

    System.out.println("Data:");
    //Walk through the result set and print out the first columns of data
    while (rset.next ())
      System.out.println (rset.getString (1));

    //close the resultSet
    rset.close();

    // Close the statement
    pstmt.close();

    // Close the connection
    conn.close();
  }

  // Utility function to read a line from standard input
  static String readEntry (String prompt)
  {
    try
    {
      StringBuffer buffer = new StringBuffer ();
      System.out.print (prompt);
      System.out.flush ();
      int c = System.in.read ();
      while (c != '\n' && c != -1)
      {
        buffer.append ((char)c);
        c = System.in.read ();
      }
      return buffer.toString ().trim ();
    }
    catch (IOException e)
    {
      return "";
    }
  }
}
