Example #1
0
  /*
   * addAuto method. parse the Automobile object and add them to three tables.
   */
  public void addAuto(Automobile auto) {
    String name = null;
    float price = 0;
    int autoid = 0;
    String sql = null;
    try {
      String autoName = auto.getMake() + " " + auto.getModel();
      price = auto.getBasePrice();
      sql =
          "insert into automobile (name, base_price) values('"
              + autoName
              + "',"
              + price
              + ")"; // MySQL command
      statement.executeUpdate(sql); // add to automobile table

      sql = "select id from automobile where name ='" + autoName + "';";
      ResultSet rs = statement.executeQuery(sql); // use ResultSet object to receive
      while (rs.next()) {
        autoid = rs.getInt("id"); // get auto_id
      }
      for (int i = 0; i < auto.getOptionSetNum(); i++) {
        name = auto.getOptionSetName(i);
        sql = "insert into optionset (name, auto_id) values('" + name + "'," + autoid + ")";
        statement.executeUpdate(sql); // add to optionset table
        int opsid = 0;
        sql = "select id from optionset where name ='" + name + "';";
        ResultSet rrs = statement.executeQuery(sql);
        while (rrs.next()) {
          opsid = rrs.getInt("id"); // get optionset id
        }
        for (int j = 0; j < auto.getOptionNum(i); j++) {
          name = auto.getOptionName(i, j);
          price = auto.getOptionPrice(i, j);
          sql =
              "insert into options (name, price, option_id) values('"
                  + name
                  + "',"
                  + price
                  + ","
                  + opsid
                  + ")";
          statement.executeUpdate(sql); // add to options table
        }
      }

    } catch (SQLException e) {
      e.printStackTrace();
    }
  }
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String modelName = request.getParameter("model");

    response.setContentType("text/html");
    PrintWriter writer = response.getWriter();
    HttpSession session = request.getSession();

    DefaultSocketClient client = new DefaultSocketClient("localhost", 8088);
    if (client.openConnection()) {
      Automobile a = client.getModelfromServer(modelName);

      writer.println("<!DOCTYPE HTML>");
      writer.println("<html>");
      writer.println("<head>");
      writer.println("<title>ConfigModel</title>");
      writer.println("</head>");
      writer.println("<body><div>");
      writer.println("<h1>Basic Car Choice</h1>");
      writer.println("<form action='Summary.jsp'>");
      writer.println("<table action='ConfigModel' border='1px'>");
      writer.println(
          "<tr><td align='middle'><b>Make/Model: </b></td><td>"
              + a.getMake()
              + " "
              + a.getModel()
              + "</td></tr>");
      ArrayList<String> opsetNames = a.getOpsetNames();
      for (int i = 0; i < opsetNames.size(); i++) {
        writer.println("<tr><td align='middle'><b>" + opsetNames.get(i) + ": " + "</b></td>");
        writer.println("<td><select name='" + opsetNames.get(i) + "'>");
        ArrayList<String> optionNames = a.getOptionNames(opsetNames.get(i));
        for (int j = 0; j < optionNames.size(); j++) {
          String optionName = optionNames.get(j);
          writer.println("<option value='" + optionName + "'>" + optionName + "</option>");
        }
        writer.println("</select></td>");
        writer.println("</tr>");
      }
      writer.println(
          "<tr><td colspan=2 align='right'><input type='submit' value='Done'/></td></tr>");
      writer.println("</table>");
      writer.println("</form>");
      writer.println("</div></body>");
      writer.println("</html>");

      session.setAttribute("auto", a);
    }
  }