Computer Science 396S
Assignment 3
Due: October 4, 2004

Modify the program used for assignment 2 so that it uses the WebServer to get requests from an html page.  Create a page that will give the user the choice of either displaying the entire contents of the database or of finding a specific item in the database.   The html page will have two forms, one for each choice.  The forms will have different action attributes that refer to two separate programs.

The SQL statement that gets all the data from the database is Select * From Customers.  The SQL statement for finding a specific name is Select * from Customers Where Name = name.  The ResultSet will contain all the fields (columns) in the database.  If the data stored are strings, use rs.getString (column_name) to get the data in the fields.  However, if the data are of some other type, use the appropriate getXXX (…) method.  Some of these are getDate (…), getDouble (…), getInt (…), and getTime (…).

The following html page contains two forms, one for displaying the entire database and the second for finding a specific name in the database.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
    <head><title>Customer List Form</title></head>

<body>
    <h3>To see all the customers in the list, click the Display Button.</h3>
    <form name = "display" method = "get" action="http://localhost:8080/client_server.DisplayCustomers">
         <input type = "submit" value = "Display Customers" />
    </form>

    <h3>To find a specific customer, enter the name.</h3>
    <form name = "find" method = "get" action = "http://localhost:8080/client_server.FindCustomer">
         <p><input type = "text" name = "name" value = "" size = 30> Name </p>
         <p><input type="submit" value="Find Customer"></p>
    </form>
</body>
</html>

The programs, DisplayCustomers and FindCustomer, should be in the same folder as the WebServer program.  Sample code for the FindCustomer program follows.

package client_server;

/* FindCustomer locates a specific customer in the database. */
import java.sql.*;
import java.io.*;

public class FindCustomer extends WebRequestProcessor
{
     public void process (Request request, Response response)
    {
      try
      {
           // Get the requested name and the output writer.
           PrintWriter out = response.getWriter ();
           String name = request.getParameter ("name");

           // Get a driver and connect to the customers database.
           Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
           Connection con = DriverManager.getConnection ("jdbc:odbc:customers");
           // Create a statement and a query and get the ResultSet.
           Statement stmt = con.createStatement ();
           String query = "Select * From Customers Where Name = '" + name + "'";
           ResultSet rs = stmt.executeQuery (query);

           // Prepare the page with all the data in the form of an html table.
           Page.createHeader (out, "Customer List");
           // If the ResultSet is not empty, display a table with the data.
           if (rs.next ())
           {
                // Set up the heading for the table.
                out.println ("<table border='1' bordercolor='#000000' cellspacing='5'>");
                out.println ("<caption>Customer List</caption>");
                out.print ("<thead><tr>");
                out.print ("<th>ID</th><th>Name</th><th>Address</th>");
                out.println ("</tr></thead>");

                // Display the row of the database.
                out.print ("<tr>");
                out.print ("<td>" + rs.getString ("id") + "</td>");
                out.print ("<td>" + rs.getString ("name") + "</td>");
                out.print ("<td>" + rs.getString ("address") + "</td>");
                out.println ("</tr>");
                out.println ("</table");
           }
           else out.println ("<h3>The name " + name + " was not in the database</h3>");
           con.close ();
           Page.createFooter (out);
      } catch (ClassNotFoundException e){System.out.println ("Class Not Found exception.\n");}
        catch (SQLException e){System.out.println ("SQL Exception\n");}
    } // process
} // FindCustomer