public void addProductToDatabase(Product p) throws SQLException {
    int number = p.getNumber();
    String name = p.getName();
    BigDecimal price = p.getUnitPrice();
    int quantity = p.getQuantity();
    String category = p.getCategory();
    String car = p.getCar();
    String image = p.getImage();
    Statement stmt = con.createStatement();

    String sql =
        "INSERT INTO APP.PRODUCTS "
            + "VALUES ("
            + number
            + ",'"
            + name
            + "',"
            + quantity
            + ",'"
            + category
            + "','"
            + image
            + "','"
            + car
            + "',"
            + price
            + ")";
    stmt.executeUpdate(sql);
  }
  public void insertSale(Customer c) throws SQLException {
    int id = c.getId();
    String name = c.getName();
    String address = c.getAddress();
    String phone = c.getPhoneNumber();
    String cardType = c.getCardType();
    String cardNumber = c.getCardNumber();
    String cardExp = c.getCardExp();
    String secCode = c.getSecCode();
    String cartDetails = "";
    for (Product p : c.getCart().getProductList())
      cartDetails += p.getName() + "@" + p.getUnitPrice() + "x" + p.getQuantity() + " ";
    BigDecimal cartTotal = c.getCart().getTotal().setScale(2);

    Statement stmt = con.createStatement();

    String sql =
        "INSERT INTO App.SALES VALUES("
            + id
            + ",'"
            + name
            + "','"
            + address
            + "','"
            + phone
            + "','"
            + cardType
            + "','"
            + cardNumber
            + "','"
            + cardExp
            + "','"
            + secCode
            + "','"
            + cartDetails
            + "',"
            + cartTotal
            + ",'"
            + getCurrentTimeStamp()
            + "')";
    stmt.executeUpdate(sql);
  }
  public void removeProductFromDatabase(Product p) throws SQLException {
    int number = p.getNumber();
    String name = p.getName();
    BigDecimal price = p.getUnitPrice();
    int quantity = p.getQuantity();
    String category = p.getCategory();
    String car = p.getCar();
    String image = p.getImage();
    Statement stmt = con.createStatement();

    String sql = "DELETE FROM App.Products WHERE number=" + number;
    stmt.executeUpdate(sql);
  }
 // ---TODO: This feels like a kludge.  Find a better way to handle it.
 public void respondToEditForm(HttpServletRequest request, HttpSession session) {
   String pid = request.getParameter("productid");
   if (pid != null) {
     try {
       int product_id = Integer.parseInt(pid);
       Product prod = Product.loadProduct(new Integer(product_id));
       if (prod == null) {
         Util.noteError(
             session, "Internal error: No product with ID " + product_id + " was found.");
       } else {
         setProduct(prod);
       }
     } catch (NumberFormatException e) {
       Util.noteError(session, "Internal error: Illegal productid: " + pid);
     }
   }
   super.respondToEditForm(request, session);
   Debug.println("Version.respondToEditForm: Setting 'record' to " + getProduct());
   session.setAttribute("record", getProduct());
 }
 private void initDefaults() {
   setProduct(Product.getPrototype());
 }
 public AbstractList generateCollection(HttpSession session) {
   Product prod = (Product) session.getAttribute("record");
   String query = "select * from tbl_version where product = " + prod.getId();
   return DatabaseRecord.loadRecords(query, Version.class);
 }
 /** @see DatabaseRecord#initializeNewRecord */
 public void initializeNewRecord(HttpServletRequest request, HttpSession session) {
   int product_id = Util.parseInt(Util.getRequiredField(request, session, "productid"), 0);
   if (product_id != 0) {
     setProduct(Product.loadProduct(new Integer(product_id)));
   }
 }
Esempio n. 8
0
  private void admitActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_admitActionPerformed

    // String  = purchase.getText();
    String name = productName.getText();
    String id = ProductID.getText();
    String desc = descript.getText();

    if (desc.isEmpty()) {
      desc = "N/A";
    }

    if (name.length() == 0 || id.length() == 0 || desc.length() == 0) {
      JOptionPane.showMessageDialog(null, "Please fill all fields");
    } else {
      try {

        String editS = HomePage.toEdit;

        Class.forName("com.mysql.jdbc.Driver");
        Connection con =
            DriverManager.getConnection("jdbc:mysql://localhost:3306/VunaFeeds", "root", "");

        if (editS.equalsIgnoreCase("Null")) {
          PreparedStatement psmt =
              con.prepareStatement(
                  "INSERT into product(productID,productName,description) values (?,?,?)");
          psmt.setString(1, id);
          psmt.setString(2, name);
          psmt.setString(3, desc);

          PreparedStatement psmt1 =
              con.prepareStatement(
                  "INSERT into new_Stock(productID,Date,units,direction,status,current) values (?,?,?,?,?,?)");
          psmt1.setString(1, id);
          psmt1.setString(2, dateForDB);
          psmt1.setString(3, "0");
          psmt1.setString(4, "In");
          psmt1.setString(5, "Current");
          psmt1.setString(6, "0");

          psmt.executeUpdate();
          psmt1.executeUpdate();

        } else if (editS.equalsIgnoreCase("EditProduct")) {
          PreparedStatement psmt =
              con.prepareStatement(
                  "UPDATE product set productname = '"
                      + name
                      + "' , description  = '"
                      + desc
                      + "' where ProductID = "
                      + id
                      + "");
          psmt.executeUpdate();
        }

        JOptionPane.showMessageDialog(null, "Product Saved");
        this.dispose();
        HomePage.toEdit = " ";
        Product win = new Product();
        win.setVisible(true);

      } catch (SQLException e) {

        System.out.println("2 Error : " + e);
        JOptionPane.showMessageDialog(null, "Oops!! An error occured. \n " + e);
      } catch (Exception ex) {
        System.out.println("Error 1:" + ex);
        JOptionPane.showMessageDialog(null, "Oops!! An error occured. \n" + ex);
      }
    }
    // TODO add your handling code here:
  } // GEN-LAST:event_admitActionPerformed