import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;

// This servlet demonstrates how an entire form can be generated
// from a database. It would typically be called directly from
// another web page (possibly as a redirect).

public class ACMEForm extends HttpServlet 
                          implements SingleThreadModel {

  public void doGet(HttpServletRequest request,
                     HttpServletResponse response) 
              throws ServletException, IOException {

    Connection con;

    PrintWriter out = response.getWriter();
    response.setContentType("text/html"); 

    try {
      // Register the driver (or the bridge).
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      //Class.forName("com.mysql.jdbc.Driver");

      // Make the connection.
      con = DriverManager.getConnection("jdbc:odbc:ACME");
      // con = DriverManager.getConnection("jdbc:mysql://localhost/ACME");

      }

    // If there is a problem connecting, return an error message.
    catch(Exception ex) {
      out.println("<HTML><BODY><P>Error -- Database unreachable!</P></BODY></HTML>");
      return;
      }

    // Construct a query that gets all items and their prices
    // from the Stock database, in order to create the form.

    String queryString =
        "SELECT Stock.Item, Stock.Price " +
        "FROM Stock";

    try {

      // Create a new statement object.
      Statement s = con.createStatement();

      // Execute the query on it.
      ResultSet rs = s.executeQuery(queryString);

      // Create the header information and start the form
      out.println("<HTML><BODY><CENTER><H2>ACME On-line Purchases</H2>");
      out.println("<FORM METHOD=POST ACTION='http://localhost:8080/Purchase'>");
      out.println("Customer name: <INPUT TYPE=TEXT NAME='customerName'><BR><BR>");
      out.println("<TABLE BORDER=1><TR><TD>ITEM</TD><TD>PRICE</TD><TD>QUANTTIY</TD></TR>");

      // Cycle through the result set until no more records, and
      // build a simple html table to display the data, as well as
      // form elements to get quantity ordered.

      while (rs.next()) {

        // Extract the named fields from the current record
        String item = rs.getString("Item");
        double price = rs.getDouble("Price"); 

        // Write as a new table row
        out.println("<TR><TD>" + item + "</TD>");
        out.println("<TD>$" + price + "</TD>");

        // Note that for convenience we give the input element the
        // same name as the item.
        out.println("<TD><INPUT TYPE=TEXT SIZE=5 NAME='" + item + "'</TD></TR>");
        }

      // Close table and form, adding submit button
      out.println("</TD></TR></TABLE><BR><BR>");
      out.println("<INPUT TYPE=SUBMIT VALUE='Place Order'></FORM></BODY></HTML>");

      // Close everything
      rs.close();
      s.close();
      con.close();

      }
    // If the SQL string is not in the correct form, this will result
    // (it must be handled). The user should never be able to cause this.
    catch(SQLException ex) {
      out.println("<HTML><BODY><P>Error handling request!</P></BODY></HTML>");
      return;
      }

    }

  }

