//  guestbook6.java
//
// by Jeff Schmitt
//    Towson University
//    September, 1998
//
// based on guestbook3.java 
//    http://triton.towson.edu/~schmitt/java/jdbc/guestbook3.html
// and converted from CGI to servlet technology

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

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

import javax.servlet.*;
import javax.servlet.http.*;

/**
 * guestbook6 servlet. This servlet uses JDBC to access and append 
 * guestbook entries into the database.
 *
 * @version 	1.0, 09/30/98
 * @author 	Jeff Schmitt
 */

public class guestbook6 extends HttpServlet {
   /**
    * Handles a single guestbook request from the client.
    */
   public void doGet (HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {

      String name = req.getParameter("NAME");
      String aff1 = req.getParameter("AFF1");
      String aff2 = req.getParameter("AFF2");
      String email = req.getParameter("EMAIL");
      // the submit button for View Guestbook
      String view = req.getParameter("view"); 

      res.setContentType("text/html");

      ServletOutputStream out = res.getOutputStream();

      out.println("<HTML><HEAD><TITLE>Guestbook 6</TITLE></HEAD>");
      out.println("<BODY><H2>Guestbook6</H2>");
      out.println("<H3>Java Servlet using JDBC</H3>");

      // Load the GWE Mysql JDBC driver

      try {
         Class.forName ("gwe.sql.gweMysqlDriver");
      } catch (Exception e) {
         out.println("write to log: Error in program");
      } 

      out.println("<h3>Hello "+req.getRemoteHost()+"</h3>");

      // Read the MySQL userid and password from the datafile
      // .mysql-init in your home directory
      String account="**";
      String password="**";

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

      // Connect to the database

      // if the connection parameters are wrong you will get the
      // error message "No suitable driver" -- check the documentation
      // for the jdbc driver you are using.

      // For the GWE mysql implementation of JDBC, it
      // looks for connection URL's in the form of 
   
      //     jdbc:mysql://[host_addr]:[port]/[db_name]
      //     jdbc:mysql:db_name
     
      try {
         Connection conn = DriverManager.getConnection 
           ("jdbc:mysql://:3306/schmitt", account, password);

         Statement stmt = conn.createStatement ();

         ResultSet rset=null;
        
         if (view == null &&  (name!=null || aff1!=null || 
                               aff2!=null || email!=null) ) {
            // insert the form values into the guestbook
            try {
            // Insert the guestbook data into table guestbook6
               rset = stmt.executeQuery("insert into guestbook6 values ('"+
                  name+"', '"+aff1+"', '"+aff2+"','"+email+"')");
            } catch (SQLException e) {
            // the above insert raises the exception:
            // java.sql.SQLException: mysql exception: No columns returned.
            // -- this should be investigated further
            // for now we will ignore it
            // out.println("writetolog: Error in insert"+e+"<BR>");
            }
         }

         // Select all columns from the table guestbook6
         rset = stmt.executeQuery ("select * from guestbook6");

         // Iterate through the result and print the Guestbook
         out.println("<TABLE BORDER CELLSPACING=3 CELLPADDING=3>");
      
         while (rset.next ()) {
            out.println("<TR>");
            for (int i=1;i<=4;i++) {
               out.println("<TD>"+rset.getString (i));
            }
         }
      } catch (SQLException e) {
         out.println("writetolog: Error in select "+e+"<BR>");
      }
      out.println("</TABLE>");
      out.println("</BODY></HTML>");
   }
}
