/* GuestJDBCo.java
 *
 * By Jeff Schmitt
 *    January, 1998
 *
 * This CGI program uses JDBC to  access an ORACLE database COSC on 
 * triton.towson.edu
 *
 * It uses the JDBC THIN driver.  
 */

// You need to import the java.sql package to use JDBC
import java.sql.*; /* Java 1.1 */
import CGIParameters;
import java.util.*;
import java.io.*;

public class GuestJDBCo {
   CGIParameters params;

   //  get a value from the Form and environment data
   public String getKey(String key) {
      try {
         if (params.containsKey(key)) {
            return params.get(key);
         } else {
            return "";
         }
      } catch (NoSuchFieldException e) {
         System.out.println("No such parameter -> "+e.getMessage());
         return "";
      }
   }

   public GuestJDBCo(String args[])
      throws SQLException, ClassNotFoundException {
      System.out.println("Content-type: text/html\n\n");
      System.out.println("<HTML><HEAD><TITLE>JDBC Guestbook</TITLE></HEAD>");
      System.out.println("<BODY><H2>JDBC Oracle Guestbook</H2>");

      // Load the Oracle JDBC driver  JDK 1.1
      // Class.forName ("oracle.jdbc.driver.OracleDriver");
      // Load the Oracle JDBC driver (JDK 1.0.2)
      // When you import jdbc.sql the driver is in the package
      // oracle.jdbc.dnlddriver
      // Class.forName ("oracle.jdbc.driver.OracleDriver");
      Class.forName ("oracle.jdbc.driver.OracleDriver");
      try {
         params = new CGIParameters(true);
      } catch (Exception e) {
         System.out.println("Error in CGIParameters: "+e.getMessage());
         System.exit(1);
      }

      System.out.println("<h3>Hello "+getKey("REMOTE_HOST")+"</h3>");
      System.out.println("Guest Oracle JDBC<P>");

      // Read the Oracle userid and password from the datafile
      // /usr/people/schmitt/.oracle-init
      String account="**";
      String password="**";

      try {
         BufferedReader in = new BufferedReader(new InputStreamReader(
              new FileInputStream("/usr/faculty/schmitt/.oracle-init")));
         account=in.readLine();
         password=in.readLine();
      } catch (Exception e) {
         System.out.println("GuestJDBC: cannot read .oracle-init");
         System.exit(1);
      }

      // Connect to the database
      // You can use either the fully specified SQL*net syntax or a shortcut
      // syntax as <host>:<port>:<sid>.
      // This example uses the database name DEMO  
      Connection conn = DriverManager.getConnection 
          ("jdbc:oracle:thin:@triton.towson.edu:1521:cosc", account,password);

      // Create a Statement
      Statement stmt = conn.createStatement ();
      ResultSet rset=null;

      // Insert the guestbook data into ORACLE table guestbook3
      try {
         rset = stmt.executeQuery("insert into guestbook6 values ('"+
              getKey("NAME")+"', '"+getKey("AFF1")+"', '"+
              getKey("AFF2")+"', '"+getKey("EMAIL")+"')");
      } catch (SQLException e) {
         System.out.println("GuestJDBC: Error in insert "+e+"<BR>");
      }

      // Select all columns from the table guestbook6
      try {
         rset = stmt.executeQuery ("select * from guestbook6");
      } catch (SQLException e) {
         System.out.println("GuestJDBC: Error in select "+e+"<BR>");
      }

      // Iterate through the result and print the Guestbook
      System.out.println("<TABLE BORDER CELLSPACING=3 CELLPADDING=3>");
      
      while (rset.next ()) {
         System.out.println("<TR>");
         for (int i=1;i<=4;i++) {
            System.out.println("<TD>"+rset.getString (i));
         }
      }
      System.out.println("</TABLE>");
      System.out.println("</BODY></HTML>");
   }

   public static void main(String args[]) {
      try {
         GuestJDBCo smain = new GuestJDBCo(args);
      } catch (Exception e) {
         System.out.println("GuestJDBCo: Error in program");
         System.exit(1);
      }
   }
}
