private void GenerateAdminNoActionPerformed( java.awt.event.ActionEvent evt) { // GEN-FIRST:event_GenerateAdminNoActionPerformed int IntAdminNo = 0; String prodID; try { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/VunaFeeds", "root", ""); Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt.executeQuery("Select * from product where Status = 'Active'"); if (rs.last()) { String adminNo = rs.getString("ProductID"); IntAdminNo = Integer.valueOf(adminNo); System.out.println(IntAdminNo); IntAdminNo++; prodID = String.valueOf(IntAdminNo); ProductID.setText(prodID); } else { IntAdminNo = 1; prodID = String.valueOf(IntAdminNo); ProductID.setText(prodID); } ProductID.setEditable(false); } 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); } } // GEN-LAST:event_GenerateAdminNoActionPerformed
/** * This method is called from within the constructor to initialize the form. WARNING: Do NOT * modify this code. The content of this method is always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); ProductID = new javax.swing.JFormattedTextField(); productName = new javax.swing.JTextField(); admit = new javax.swing.JButton(); DisplayDate = new javax.swing.JLabel(); GenerateAdminNo = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); descript = new javax.swing.JTextPane(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("Product"); jPanel1.setBackground(new java.awt.Color(255, 255, 255)); jLabel1.setBackground(new java.awt.Color(110, 189, 235)); jLabel1.setFont(new java.awt.Font("Cambria", 1, 24)); jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel1.setText("Product"); jLabel1.setOpaque(true); jLabel3.setText("Product ID:"); jLabel4.setText("Product Name"); jLabel5.setText("Description:"); ProductID.setFormatterFactory( new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter())); admit.setBackground(new java.awt.Color(255, 255, 255)); admit.setText("Save"); admit.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(110, 189, 235), 2, true)); admit.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { admitActionPerformed(evt); } }); GenerateAdminNo.setBackground(new java.awt.Color(255, 255, 255)); GenerateAdminNo.setText("Generate"); GenerateAdminNo.setBorder( new javax.swing.border.LineBorder(new java.awt.Color(110, 189, 235), 2, true)); GenerateAdminNo.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { GenerateAdminNoActionPerformed(evt); } }); jScrollPane1.setViewportView(descript); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup( jPanel1Layout .createSequentialGroup() .addContainerGap() .addGroup( jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent( DisplayDate, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent( jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE) .addGroup( jPanel1Layout .createSequentialGroup() .addGroup( jPanel1Layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent( jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE) .addComponent( jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE) .addComponent( jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup( jPanel1Layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent( productName, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE) .addComponent( ProductID, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE) .addComponent( jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)) .addGap(30, 30, 30) .addGroup( jPanel1Layout .createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING) .addComponent( admit, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE) .addComponent( GenerateAdminNo, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addContainerGap())); jPanel1Layout.setVerticalGroup( jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup( jPanel1Layout .createSequentialGroup() .addComponent( jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent( DisplayDate, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE) .addGroup( jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent( ProductID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent( GenerateAdminNo, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup( jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent( jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent( productName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup( jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup( jPanel1Layout .createSequentialGroup() .addComponent(jLabel5) .addGap(83, 83, 83) .addComponent( admit, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent( jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(21, 21, 21))); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent( jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); layout.setVerticalGroup( layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent( jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); pack(); } // </editor-fold>//GEN-END:initComponents
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