Exemplo n.º 1
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