import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class NewCustomer extends HttpServlet { 

  public static Connection conn;
  public static Statement stmt;

  public void doGet (HttpServletRequest request,
                     HttpServletResponse response)
        throws ServletException, IOException {
 
    doPost(request, response);
  }
  
  public void doPost (HttpServletRequest request,
                     HttpServletResponse response)
        throws ServletException, IOException {
 
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    out.println("<html><head><title>New Customer Sign-Up</title></head>");
    out.println("<body>");

    String cname[] = request.getParameterValues("cname");    
    String street[] = request.getParameterValues("street");    
    String city[] = request.getParameterValues("city");    
    String state[] = request.getParameterValues("state");    
    String zip[] = request.getParameterValues("zip");    
    String phone[] = request.getParameterValues("phone");    
    String email[] = request.getParameterValues("email");    
    String password[] = request.getParameterValues("password");    

    String newInsert = "insert into customers values(" +
                       " cust_seq.nextval ,'" + cname[0] + "', '" +
                       street[0] + "', '"  + city[0] + "', '" +
                       state[0] + "', '"  + zip[0] + "', '" +
                       phone[0] + "', '"  + email[0] + "', '" +
                       password[0] + "', null, null)";


//out.println(newInsert);

    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
    } catch(ClassNotFoundException e){
        out.println("Error loading the Driver:"+e.getMessage());
        return;
      }

    Connection conn = null;

    try {
      conn = DriverManager.getConnection (
         MyUtilities.CONNECTSTRING,MyUtilities.ID,MyUtilities.PASSWORD);
    } catch (SQLException e1) {
        out.println("Error connecting to Oracle:"+e1.getMessage());
        return;
      }
    if (conn == null) {
      out.println("Null Connection");
      return;
    }

    Statement stmt = null;
    try {
      stmt = conn.createStatement ();
    } catch (SQLException e) {
        out.println("createStatement " + e.getMessage());
        try {conn.close();} catch (SQLException e2) {};
        return;
      }

    ResultSet rset = null;

    try {
        stmt.executeUpdate(newInsert);
        out.println("Sign-Up sucessful <br>");
    } catch (SQLException e) {
        out.println("executeUpdate " + e.getMessage());
        return;
      }
                                                     
    try {
      rset = stmt.executeQuery("select cust_seq.currval from dual");
    } catch (SQLException e) {
        out.println("executeQuery " + e.getMessage());
        return;
      }
    try {
      rset.next();
      out.println(" Your Customer Account Number is " +
                           rset.getString(1));
      } catch (SQLException e) {
          System.out.println("Could not execute add Customer");
          while (e != null) {
            System.out.println("Message : " + e.getMessage());
            e = e.getNextException();
          }
          try {
          stmt.close();
          } catch (SQLException e1) {}
          return;
      }
     try {
       stmt.close();
       conn.close();
     } catch (SQLException e) {
         out.println("executeQuery " + e.getMessage());
         return;
       } 
     out.println("<P><P><P>Back to <A HREF=\""+
             MyUtilities.WSLOGIN+"\">Login</A> page.");
     out.close();
  }

   public String getServletInfo() {
    return "This Servlet processes New Customer Registration";
  }

}                                              
