Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 29.17 KB | None | 0 0
  1. package mainpackage;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12. import javax.swing.JOptionPane;
  13. import javax.swing.table.DefaultTableModel;
  14.  
  15. /*
  16.  * To change this license header, choose License Headers in Project Properties.
  17.  * To change this template file, choose Tools | Templates
  18.  * and open the template in the editor.
  19.  */
  20. /**
  21.  *
  22.  * @author marvi
  23.  */
  24. public class StockIn extends javax.swing.JFrame {
  25.  
  26.     /**
  27.      * Creates new form StockIn
  28.      */
  29.     public StockIn() {
  30.         initComponents();
  31.         getConnection();
  32.         Show_Products_In_Jtable();
  33.     }
  34.  
  35.     /**
  36.      * This method is called from within the constructor to initialize the form.
  37.      * WARNING: Do NOT modify this code. The content of this method is always
  38.      * regenerated by the Form Editor.
  39.      */
  40.     @SuppressWarnings("unchecked")
  41.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  42.     private void initComponents() {
  43.  
  44.         jPanel1 = new javax.swing.JPanel();
  45.         jPanel2 = new javax.swing.JPanel();
  46.         txt_StockId = new javax.swing.JTextField();
  47.         txt_BrandName = new javax.swing.JTextField();
  48.         txt_GenericName = new javax.swing.JTextField();
  49.         txt_Manufacturer = new javax.swing.JTextField();
  50.         txt_Dosage = new javax.swing.JTextField();
  51.         jLabel1 = new javax.swing.JLabel();
  52.         jLabel2 = new javax.swing.JLabel();
  53.         jLabel3 = new javax.swing.JLabel();
  54.         jLabel4 = new javax.swing.JLabel();
  55.         jLabel5 = new javax.swing.JLabel();
  56.         jLabel6 = new javax.swing.JLabel();
  57.         jLabel7 = new javax.swing.JLabel();
  58.         txt_StockIn = new javax.swing.JTextField();
  59.         txt_StockDate = new javax.swing.JTextField();
  60.         jScrollPane1 = new javax.swing.JScrollPane();
  61.         tbl_Stocks = new javax.swing.JTable();
  62.         jPanel3 = new javax.swing.JPanel();
  63.         btn_Add = new javax.swing.JButton();
  64.         btn_Update = new javax.swing.JButton();
  65.         btn_Delete = new javax.swing.JButton();
  66.         btn_MainMenu = new javax.swing.JButton();
  67.  
  68.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  69.  
  70.         jPanel1.setBackground(new java.awt.Color(0, 255, 255));
  71.  
  72.         txt_StockId.addActionListener(new java.awt.event.ActionListener() {
  73.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  74.                 txt_StockIdActionPerformed(evt);
  75.             }
  76.         });
  77.  
  78.         txt_BrandName.addActionListener(new java.awt.event.ActionListener() {
  79.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  80.                 txt_BrandNameActionPerformed(evt);
  81.             }
  82.         });
  83.  
  84.         txt_GenericName.addActionListener(new java.awt.event.ActionListener() {
  85.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  86.                 txt_GenericNameActionPerformed(evt);
  87.             }
  88.         });
  89.  
  90.         txt_Manufacturer.addActionListener(new java.awt.event.ActionListener() {
  91.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  92.                 txt_ManufacturerActionPerformed(evt);
  93.             }
  94.         });
  95.  
  96.         txt_Dosage.addActionListener(new java.awt.event.ActionListener() {
  97.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  98.                 txt_DosageActionPerformed(evt);
  99.             }
  100.         });
  101.  
  102.         jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
  103.         jLabel1.setText("Generic Name");
  104.  
  105.         jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
  106.         jLabel2.setText("ID");
  107.  
  108.         jLabel3.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
  109.         jLabel3.setText("Brand Name");
  110.  
  111.         jLabel4.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
  112.         jLabel4.setText("Manufacturer");
  113.  
  114.         jLabel5.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
  115.         jLabel5.setText("Dosage");
  116.  
  117.         jLabel6.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
  118.         jLabel6.setText("Stock Date");
  119.  
  120.         jLabel7.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
  121.         jLabel7.setText("Stock In");
  122.  
  123.         txt_StockIn.addActionListener(new java.awt.event.ActionListener() {
  124.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  125.                 txt_StockInActionPerformed(evt);
  126.             }
  127.         });
  128.  
  129.         txt_StockDate.addActionListener(new java.awt.event.ActionListener() {
  130.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  131.                 txt_StockDateActionPerformed(evt);
  132.             }
  133.         });
  134.  
  135.         javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
  136.         jPanel2.setLayout(jPanel2Layout);
  137.         jPanel2Layout.setHorizontalGroup(
  138.             jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  139.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
  140.                 .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  141.                     .addGroup(jPanel2Layout.createSequentialGroup()
  142.                         .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  143.                             .addGroup(jPanel2Layout.createSequentialGroup()
  144.                                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  145.                                 .addComponent(jLabel2))
  146.                             .addGroup(jPanel2Layout.createSequentialGroup()
  147.                                 .addGap(18, 18, 18)
  148.                                 .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  149.                                     .addComponent(jLabel4)
  150.                                     .addComponent(jLabel5)
  151.                                     .addComponent(jLabel7)
  152.                                     .addComponent(jLabel3)
  153.                                     .addComponent(jLabel6))
  154.                                 .addGap(0, 0, Short.MAX_VALUE)))
  155.                         .addGap(18, 18, 18))
  156.                     .addGroup(jPanel2Layout.createSequentialGroup()
  157.                         .addContainerGap()
  158.                         .addComponent(jLabel1)
  159.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
  160.                 .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  161.                     .addComponent(txt_Dosage, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
  162.                     .addComponent(txt_Manufacturer, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
  163.                     .addComponent(txt_GenericName, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
  164.                     .addComponent(txt_BrandName, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
  165.                     .addComponent(txt_StockId, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
  166.                     .addComponent(txt_StockIn, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
  167.                     .addComponent(txt_StockDate, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
  168.                 .addGap(22, 22, 22))
  169.         );
  170.         jPanel2Layout.setVerticalGroup(
  171.             jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  172.             .addGroup(jPanel2Layout.createSequentialGroup()
  173.                 .addGap(24, 24, 24)
  174.                 .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  175.                     .addGroup(jPanel2Layout.createSequentialGroup()
  176.                         .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  177.                             .addComponent(txt_StockId, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
  178.                             .addComponent(jLabel2))
  179.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  180.                         .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  181.                             .addComponent(txt_BrandName, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
  182.                             .addComponent(jLabel3))
  183.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  184.                         .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  185.                             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
  186.                                 .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  187.                                     .addComponent(txt_GenericName, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
  188.                                     .addComponent(jLabel1))
  189.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  190.                                 .addComponent(txt_Manufacturer, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
  191.                             .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING))
  192.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  193.                         .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  194.                             .addComponent(txt_Dosage, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
  195.                             .addComponent(jLabel5))
  196.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  197.                         .addComponent(jLabel7))
  198.                     .addComponent(txt_StockIn, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
  199.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  200.                 .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  201.                     .addComponent(jLabel6)
  202.                     .addComponent(txt_StockDate, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
  203.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  204.         );
  205.  
  206.         tbl_Stocks.setModel(new javax.swing.table.DefaultTableModel(
  207.             new Object [][] {
  208.  
  209.             },
  210.             new String [] {
  211.                 "Stock ID", "Brand Name", "Generic Name", "Manufacturer", "Dosage", "Stock In", "Stock Date"
  212.             }
  213.         ));
  214.         jScrollPane1.setViewportView(tbl_Stocks);
  215.  
  216.         btn_Add.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
  217.         btn_Add.setText("ADD");
  218.         btn_Add.addActionListener(new java.awt.event.ActionListener() {
  219.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  220.                 btn_AddActionPerformed(evt);
  221.             }
  222.         });
  223.  
  224.         btn_Update.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
  225.         btn_Update.setText("UPDATE");
  226.         btn_Update.addActionListener(new java.awt.event.ActionListener() {
  227.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  228.                 btn_UpdateActionPerformed(evt);
  229.             }
  230.         });
  231.  
  232.         btn_Delete.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
  233.         btn_Delete.setText("DELETE");
  234.         btn_Delete.addActionListener(new java.awt.event.ActionListener() {
  235.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  236.                 btn_DeleteActionPerformed(evt);
  237.             }
  238.         });
  239.  
  240.         btn_MainMenu.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
  241.         btn_MainMenu.setText("Main Menu");
  242.         btn_MainMenu.addActionListener(new java.awt.event.ActionListener() {
  243.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  244.                 btn_MainMenuActionPerformed(evt);
  245.             }
  246.         });
  247.  
  248.         javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
  249.         jPanel3.setLayout(jPanel3Layout);
  250.         jPanel3Layout.setHorizontalGroup(
  251.             jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  252.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
  253.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  254.                 .addComponent(btn_Add)
  255.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  256.                 .addComponent(btn_Update)
  257.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  258.                 .addComponent(btn_Delete)
  259.                 .addGap(22, 22, 22))
  260.             .addGroup(jPanel3Layout.createSequentialGroup()
  261.                 .addGap(91, 91, 91)
  262.                 .addComponent(btn_MainMenu)
  263.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  264.         );
  265.         jPanel3Layout.setVerticalGroup(
  266.             jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  267.             .addGroup(jPanel3Layout.createSequentialGroup()
  268.                 .addGap(26, 26, 26)
  269.                 .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  270.                     .addComponent(btn_Add)
  271.                     .addComponent(btn_Update)
  272.                     .addComponent(btn_Delete))
  273.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  274.                 .addComponent(btn_MainMenu)
  275.                 .addContainerGap(16, Short.MAX_VALUE))
  276.         );
  277.  
  278.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  279.         jPanel1.setLayout(jPanel1Layout);
  280.         jPanel1Layout.setHorizontalGroup(
  281.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  282.             .addGroup(jPanel1Layout.createSequentialGroup()
  283.                 .addContainerGap()
  284.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  285.                     .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  286.                     .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  287.                 .addGap(18, 18, 18)
  288.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 678, Short.MAX_VALUE)
  289.                 .addContainerGap())
  290.         );
  291.         jPanel1Layout.setVerticalGroup(
  292.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  293.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
  294.                 .addContainerGap()
  295.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  296.                     .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 504, Short.MAX_VALUE)
  297.                     .addGroup(jPanel1Layout.createSequentialGroup()
  298.                         .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  299.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  300.                         .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
  301.                 .addContainerGap())
  302.         );
  303.  
  304.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  305.         getContentPane().setLayout(layout);
  306.         layout.setHorizontalGroup(
  307.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  308.             .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  309.         );
  310.         layout.setVerticalGroup(
  311.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  312.             .addGroup(layout.createSequentialGroup()
  313.                 .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  314.                 .addGap(0, 0, Short.MAX_VALUE))
  315.         );
  316.  
  317.         pack();
  318.     }// </editor-fold>                        
  319. ///////////////////////////////  
  320.  
  321.     public java.sql.Connection getConnection() {
  322.         java.sql.Connection con = null;
  323.  
  324.         try {
  325.             con = (java.sql.Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/medicaldatabase", "root", "password");
  326.  
  327.             return con; //return to con
  328.         } catch (SQLException ex) {
  329.             Logger.getLogger(
  330.                     StockIn.class.getName()).log(Level.SEVERE, null, ex);
  331.  
  332.             return null;
  333.         }
  334.     }
  335.  
  336.     public boolean checkInput() {
  337.         if (txt_BrandName == null
  338.                 || txt_GenericName == null
  339.                 || txt_Manufacturer == null
  340.                 || txt_Dosage == null
  341.                 || txt_StockIn == null
  342.                 || txt_StockDate == null) {
  343.             return false;
  344.         } else {
  345.             try {
  346.  
  347.                 return true;
  348.             } catch (Exception ex) {
  349.                 return false;
  350.             }
  351.         }
  352.     }
  353.  
  354.     /////////////////////////////////
  355.     private void txt_StockIdActionPerformed(java.awt.event.ActionEvent evt) {                                            
  356.         // TODO add your handling code here:
  357.     }                                          
  358.  
  359.     private void txt_BrandNameActionPerformed(java.awt.event.ActionEvent evt) {                                              
  360.         // TODO add your handling code here:
  361.     }                                            
  362.  
  363.     private void txt_GenericNameActionPerformed(java.awt.event.ActionEvent evt) {                                                
  364.         // TODO add your handling code here:
  365.     }                                              
  366.  
  367.     private void txt_ManufacturerActionPerformed(java.awt.event.ActionEvent evt) {                                                
  368.         // TODO add your handling code here:
  369.     }                                                
  370.  
  371.     private void txt_DosageActionPerformed(java.awt.event.ActionEvent evt) {                                          
  372.         // TODO add your handling code here:
  373.     }                                          
  374.  
  375.     private void txt_StockInActionPerformed(java.awt.event.ActionEvent evt) {                                            
  376.         // TODO add your handling code here:
  377.     }                                          
  378.  
  379.     private void txt_StockDateActionPerformed(java.awt.event.ActionEvent evt) {                                              
  380.         // TODO add your handling code here:
  381.     }                                            
  382.  
  383.     private void btn_UpdateActionPerformed(java.awt.event.ActionEvent evt) {                                          
  384.         // TODO add your handling code here:
  385.  
  386.         if (checkInput() && txt_StockId.getText() != null) {
  387.             String UpdateQuery = null;
  388.             PreparedStatement ps = null;
  389.             Connection con = getConnection();
  390.             try {
  391.  
  392.                 UpdateQuery = "UPDATE stocktable SET BrandName = ?,GenericName = ?,Manufacturer = ?,Dosage = ?,StockIn = ?,StockDate = ?" + "WHERE Stock_ID = ?";
  393.                 ps = con.prepareStatement(UpdateQuery);
  394.                
  395.                 ps.setString(1, txt_BrandName.getText());
  396.                 ps.setString(2, txt_GenericName.getText());
  397.                 ps.setString(3, txt_Manufacturer.getText());
  398.                 ps.setInt(4, Integer.parseInt(txt_Dosage.getText()));
  399.                 ps.setInt(5, Integer.parseInt(txt_StockIn.getText()));
  400.                 ps.setString(6, txt_StockDate.getText());
  401.  
  402.                 ps.executeUpdate();
  403.                 Show_Products_In_Jtable();
  404.                 JOptionPane.showMessageDialog(null, "Data is Updated");
  405.             } catch (SQLException ex) {
  406.                 Logger.getLogger(StockIn.class.getName()).log(Level.SEVERE, null, ex);
  407.             }
  408.         } else {
  409.             JOptionPane.showMessageDialog(null, "One or More FIelds are Empty or Wrong");
  410.         }
  411.  
  412.     }                                          
  413.  
  414.     private void btn_DeleteActionPerformed(java.awt.event.ActionEvent evt) {                                          
  415.         // TODO add your handling code here:
  416.         if (!txt_StockId.getText().equals("")) {
  417.             try {
  418.                 Connection con = getConnection();
  419.                 PreparedStatement ps = con.prepareStatement("Delete FROM stocktable WHERE Stock_ID = ?");
  420.  
  421.                 int id = Integer.parseInt(txt_StockId.getText());
  422.                 ps.setInt(1, id);
  423.                 ps.executeUpdate();
  424.                 Show_Products_In_Jtable();
  425.                 JOptionPane.showMessageDialog(null, "Product is Deleted");
  426.  
  427.             } catch (SQLException ex) {
  428.                 Logger.getLogger(StockIn.class.getName()).log(Level.SEVERE, null, ex);
  429.             }
  430.         } else {
  431.             JOptionPane.showMessageDialog(null, "Product is NOT Deleted");
  432.         }
  433.     }                                          
  434.  
  435.     private void btn_MainMenuActionPerformed(java.awt.event.ActionEvent evt) {                                            
  436.         // TODO add your handling code here:
  437.         Buttons menu = new Buttons();
  438.         menu.mtdMainMenu();
  439.         this.dispose();
  440.     }                                            
  441.  
  442.     private void btn_AddActionPerformed(java.awt.event.ActionEvent evt) {                                        
  443.         // TODO add your handling code here:
  444.  
  445.         if (checkInput() != false) {
  446.             try {
  447.                 Connection con = getConnection();
  448.                 PreparedStatement ps = con.prepareStatement("INSERT INTO stocktable(BrandName,GenericName,Manufacturer,Dosage,StockIn,StockDate)" + "values(?,?,?,?,?,?)");
  449.  
  450.                 ps.setString(1, txt_BrandName.getText());
  451.                 ps.setString(2, txt_GenericName.getText());
  452.                 ps.setString(3, txt_Manufacturer.getText());
  453.                 ps.setInt(4, Integer.parseInt(txt_Dosage.getText()));
  454.                 ps.setInt(5, Integer.parseInt(txt_StockIn.getText()));
  455.                 ps.setString(6, txt_StockDate.getText());
  456.  
  457.                 ps.executeUpdate();
  458.                 Show_Products_In_Jtable();
  459.                 JOptionPane.showMessageDialog(null, "Data Added");
  460.  
  461.             } catch (Exception ex) {
  462.                 JOptionPane.showMessageDialog(null, ex.getMessage());
  463.             }
  464.         } else {
  465.             JOptionPane.showMessageDialog(null, "One or More Field Are Undefined");
  466.         }
  467.         //for checking purpose only
  468.         System.out.println("Brand Name => " + txt_BrandName.getText());
  469.         System.out.println("Generic => " + txt_GenericName.getText());
  470.         System.out.println("Manufacturer => " + txt_Manufacturer.getText());
  471.         System.out.println("Dosage => " + txt_Dosage.getText());
  472.         System.out.println("StockIn =>" + txt_StockIn.getText());
  473.         System.out.println("StockDate =>" + txt_StockDate.getText());
  474.  
  475.  
  476.     }                                      
  477.  
  478.     /////////////////////////////////////
  479.     //Display Data in Jtable (1)
  480.     public ArrayList<Stocks> getStocksList() {
  481.         ArrayList<Stocks> stockList = new ArrayList<Stocks>();
  482.         Connection con = getConnection();
  483.         String query = "SELECT Stock_ID,Brandname,GenericName,Manufacturer,Dosage,StockIn,StockDate FROM stocktable";
  484.  
  485.         Statement st;
  486.         ResultSet rs;
  487.  
  488.         try {
  489.             st = con.createStatement();
  490.             rs = st.executeQuery(query);
  491.             Stocks stocks;
  492.  
  493.             /////
  494.             while (rs.next()) {
  495.                 stocks = new Stocks(rs.getInt("Stock_Id"), rs.getString("BrandName"), rs.getString("GenericName"), rs.getString("Manufacturer"), rs.getInt("Dosage"), rs.getInt("StockIn"), rs.getString("StockDate"));
  496.                 stockList.add(stocks);
  497.  
  498.             }
  499.         } catch (SQLException ex) {
  500.             Logger.getLogger(StockIn.class.getName()).log(Level.SEVERE, null, ex);
  501.         }
  502.  
  503.         return stockList;
  504.  
  505.         //populate Jtable
  506.     }
  507.  
  508.     public void Show_Products_In_Jtable() {
  509.  
  510.         ArrayList<Stocks> list = getStocksList();
  511.         DefaultTableModel model = (DefaultTableModel) tbl_Stocks.getModel();
  512.         model.setRowCount(0);
  513.         Object[] row = new Object[6];
  514.         for (int i = 0; i < list.size(); i++) {
  515.             row[0] = list.get(i).getStockId();
  516.             row[1] = list.get(i).getBrandName();
  517.             row[2] = list.get(i).getGenericName();
  518.             row[3] = list.get(i).getManufacturer();
  519.             row[4] = list.get(i).getDosage();
  520.             row[5] = list.get(i).getStockIn();
  521.             row[6] = list.get(i).getStockDate();
  522.  
  523.             model.addRow(row);
  524.  
  525.         }
  526.     }
  527.  
  528.     public void ShowItem(int index) {
  529.         txt_StockId.setText(Integer.toString(getStocksList().get(index).getStockId()));
  530.         txt_BrandName.setText(getStocksList().get(index).getBrandName());
  531.         txt_GenericName.setText(getStocksList().get(index).getGenericName());
  532.         txt_Manufacturer.setText(getStocksList().get(index).getManufacturer());
  533.         txt_Dosage.setText(Integer.toString(getStocksList().get(index).getDosage()));
  534.         txt_StockIn.setText(Integer.toString(getStocksList().get(index).getStockIn()));
  535.         txt_StockDate.setText(getStocksList().get(index).getStockDate());
  536.  
  537.     }
  538.  
  539.     //////////////////////////////////////
  540.     /**
  541.      * @param args the command line arguments
  542.      */
  543.     public static void main(String args[]) {
  544.         /* Set the Nimbus look and feel */
  545.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  546.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  547.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  548.          */
  549.         try {
  550.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  551.                 if ("Nimbus".equals(info.getName())) {
  552.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  553.                     break;
  554.                 }
  555.             }
  556.         } catch (ClassNotFoundException ex) {
  557.             java.util.logging.Logger.getLogger(StockIn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  558.         } catch (InstantiationException ex) {
  559.             java.util.logging.Logger.getLogger(StockIn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  560.         } catch (IllegalAccessException ex) {
  561.             java.util.logging.Logger.getLogger(StockIn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  562.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  563.             java.util.logging.Logger.getLogger(StockIn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  564.         }
  565.         //</editor-fold>
  566.  
  567.         /* Create and display the form */
  568.         java.awt.EventQueue.invokeLater(new Runnable() {
  569.             public void run() {
  570.                 new StockIn().setVisible(true);
  571.             }
  572.         });
  573.     }
  574.  
  575.     // Variables declaration - do not modify                    
  576.     private javax.swing.JButton btn_Add;
  577.     private javax.swing.JButton btn_Delete;
  578.     private javax.swing.JButton btn_MainMenu;
  579.     private javax.swing.JButton btn_Update;
  580.     private javax.swing.JLabel jLabel1;
  581.     private javax.swing.JLabel jLabel2;
  582.     private javax.swing.JLabel jLabel3;
  583.     private javax.swing.JLabel jLabel4;
  584.     private javax.swing.JLabel jLabel5;
  585.     private javax.swing.JLabel jLabel6;
  586.     private javax.swing.JLabel jLabel7;
  587.     private javax.swing.JPanel jPanel1;
  588.     private javax.swing.JPanel jPanel2;
  589.     private javax.swing.JPanel jPanel3;
  590.     private javax.swing.JScrollPane jScrollPane1;
  591.     private javax.swing.JTable tbl_Stocks;
  592.     private javax.swing.JTextField txt_BrandName;
  593.     private javax.swing.JTextField txt_Dosage;
  594.     private javax.swing.JTextField txt_GenericName;
  595.     private javax.swing.JTextField txt_Manufacturer;
  596.     private javax.swing.JTextField txt_StockDate;
  597.     private javax.swing.JTextField txt_StockId;
  598.     private javax.swing.JTextField txt_StockIn;
  599.     // End of variables declaration                  
  600. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement