/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package shop;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.Query;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import net.proteanit.sql.DbUtils;

/**
 *
 * @author Sabit
 */
public class Medicines extends javax.swing.JFrame {

    /**
     * Creates new form Medicines
     */
    public Medicines() {
        setTitle("Medicines");
        initComponents();
        updateTable();
        medicineIdFLd.setEnabled(false);
        updateBtn.setEnabled(false);
        DelBtn.setEnabled(false);
        idLbl.setVisible(false);
        currentStockLbl.setVisible(false);
        currentStockFld.setVisible(false);
        medicineIdFLd.setVisible(false);
        resetBtn.setVisible(false);
        LoginForm lf = new LoginForm();

    }

    String sql = "INSERT INTO medicines(medicine_name,medicine_type,company_name,stock,price_per_unit,current_stock)" + " VALUES (?,?,?,?,?,?)";

    public void InsertMedicine() {

        Medicines medicine = new Medicines();
        Connection con;
        PreparedStatement ps;
        try {
            con = DriverManager.getConnection("jdbc:mysql://localhost/medical_store", "root", "root");
            ps = con.prepareStatement(sql);
            ps.setString(1, medicineNameFLd.getText());
            ps.setString(2, typeFld.getText());
            ps.setString(3, companyNameFLd.getText());
            ps.setString(4, stockFLd.getText());
            ps.setString(5, pricePerUnitFld.getText());
            ps.setString(6, stockFLd.getText());

            int save = ps.executeUpdate();
            System.out.println("Int:" + save);

            System.out.println("executeQuery");

            if (save > 0) {
                System.out.println("Inserted");
                JOptionPane.showMessageDialog(null, "saved");
                dispose();
                medicine.updateTable();
                medicine.setVisible(true);
                dispose();

            } else {
                System.out.println("Not Inserted");
                JOptionPane.showMessageDialog(null, "Not saved");

            }

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Insert Valid Data");
            dispose();
            medicine.setVisible(true);
        }

    }

    public void updateTable() {
        Connection con;

        PreparedStatement ps;
        try {
            con = DriverManager.getConnection("jdbc:mysql://localhost/medical_store", "root", "root");
            ps = con.prepareStatement("SELECT * FROM medicines");

            ResultSet rs = ps.executeQuery();
            medicineTable.setModel(DbUtils.resultSetToTableModel(rs));

        } catch (SQLException ex) {

            Logger.getLogger(LoginForm.class.getName()).log(Level.SEVERE, null, ex);

        }
    }

    /**
     * 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() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        medicineTable = new javax.swing.JTable();
        backBtn = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        medicineNameFLd = new javax.swing.JTextField();
        companyNameFLd = new javax.swing.JTextField();
        typeFld = new javax.swing.JTextField();
        stockFLd = new javax.swing.JTextField();
        insertMedicineBtn = new javax.swing.JButton();
        updateBtn = new javax.swing.JButton();
        DelBtn = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        pricePerUnitFld = new javax.swing.JTextField();
        idLbl = new javax.swing.JLabel();
        medicineIdFLd = new javax.swing.JTextField();
        currentStockFld = new javax.swing.JTextField();
        currentStockLbl = new javax.swing.JLabel();
        resetBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setText("                                                                  Medicines");

        medicineTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Name", "Type", "Price", "Stock", "Current Stock"
            }
        ));
        medicineTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                medicineTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(medicineTable);

        backBtn.setText("Back");
        backBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backBtnActionPerformed(evt);
            }
        });

        jLabel3.setText("Company:");

        jLabel2.setText("Name:");

        jLabel4.setText("Type:");

        jLabel5.setText("Stock:");

        insertMedicineBtn.setText("Insert");
        insertMedicineBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insertMedicineBtnActionPerformed(evt);
            }
        });

        updateBtn.setText("Update ");
        updateBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateBtnActionPerformed(evt);
            }
        });

        DelBtn.setText("Delete");
        DelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DelBtnActionPerformed(evt);
            }
        });

        jLabel6.setText("Price:");

        idLbl.setText("Id:");

        currentStockLbl.setText("CurrentStock");

        resetBtn.setText("Reset");
        resetBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetBtnActionPerformed(evt);
            }
        });

        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)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel4)
                                    .addComponent(idLbl))
                                .addGap(18, 18, 18))
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(7, 7, 7)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(companyNameFLd, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(typeFld, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(stockFLd, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(medicineIdFLd, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(medicineNameFLd, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(currentStockLbl, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(24, 24, 24)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(pricePerUnitFld, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(currentStockFld, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(resetBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(insertMedicineBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(updateBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(DelBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(0, 32, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(idLbl)
                    .addComponent(medicineIdFLd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(medicineNameFLd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(companyNameFLd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(typeFld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(stockFLd, 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.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(pricePerUnitFld, 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.BASELINE)
                    .addComponent(currentStockFld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(currentStockLbl))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 78, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(insertMedicineBtn)
                    .addComponent(updateBtn)
                    .addComponent(DelBtn)
                    .addComponent(resetBtn))
                .addGap(32, 32, 32))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 886, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(backBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1)
                .addGap(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 409, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 73, Short.MAX_VALUE)
                .addComponent(backBtn)
                .addGap(21, 21, 21))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void backBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backBtnActionPerformed
        // TODO add your handling code here:
        MainPage mainpage = new MainPage();
        dispose();
        mainpage.wcLbl.setText("Welcome: " +GlobalConstant.name);
        mainpage.setVisible(true);

    }//GEN-LAST:event_backBtnActionPerformed

    private void medicineTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_medicineTableMouseClicked

        int i = medicineTable.getSelectedRow();

        DefaultTableModel model = (DefaultTableModel) medicineTable.getModel();
        medicineIdFLd.setText(model.getValueAt(i, 0).toString());
        medicineNameFLd.setText(model.getValueAt(i, 1).toString());
        typeFld.setText(model.getValueAt(i, 2).toString());
        companyNameFLd.setText(model.getValueAt(i, 3).toString());
        stockFLd.setText(model.getValueAt(i, 5).toString());
        pricePerUnitFld.setText(model.getValueAt(i, 4).toString());
        currentStockFld.setText(model.getValueAt(i, 6).toString());

        updateBtn.setEnabled(true);
        DelBtn.setEnabled(true);
        insertMedicineBtn.setVisible(false);
        resetBtn.setVisible(true);

        mainstock = Integer.parseInt(stockFLd.getText().toString());
        System.out.println("Stock:" + mainstock);
        currStock = Integer.parseInt(currentStockFld.getText().toString());
        System.out.println("CurrentStock:" + currentStockFld);

    }//GEN-LAST:event_medicineTableMouseClicked

    private void DelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DelBtnActionPerformed

        deleteMedicine();
    }//GEN-LAST:event_DelBtnActionPerformed

    private void updateBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateBtnActionPerformed

        updatemedicine();

    }//GEN-LAST:event_updateBtnActionPerformed

    private void insertMedicineBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insertMedicineBtnActionPerformed

        InsertMedicine();

    }//GEN-LAST:event_insertMedicineBtnActionPerformed

    private void resetBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetBtnActionPerformed

        stockFLd.setText("0");
        currentStockFld.setText("0");
        int a, b;
        a = Integer.parseInt(stockFLd.getText().toString());
        b = Integer.parseInt(stockFLd.getText().toString());

        Medicines medicine = new Medicines();

        Connection con;
        PreparedStatement ps;
        String sql = "UPDATE medicines SET medicine_name='" + medicineNameFLd.getText() + "',current_stock='" + a + "',medicine_type='" + typeFld.getText() + "',company_name='" + companyNameFLd.getText() + "',stock='" + b + "',price_per_unit='" + pricePerUnitFld.getText() + "' WHERE id='" + medicineIdFLd.getText() + "'";
        try {

            con = DriverManager.getConnection("jdbc:mysql://localhost/medical_store", "root", "root");
            ps = con.prepareStatement(sql);

            int save = ps.executeUpdate();

            System.out.println("executeQuery");

            if (save > 0) {
                System.out.println("Inserted");
                JOptionPane.showMessageDialog(null, "saved");
                dispose();
                medicine.updateTable();
                medicine.setVisible(true);
                dispose();

            } else {
                System.out.println("Not Inserted");
                JOptionPane.showMessageDialog(null, "Not saved");

            }

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Insert Valid Data");
            dispose();
            medicine.setVisible(true);
        }
    }//GEN-LAST:event_resetBtnActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Medicines.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Medicines.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Medicines.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Medicines.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Medicines().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton DelBtn;
    private javax.swing.JButton backBtn;
    private javax.swing.JTextField companyNameFLd;
    private javax.swing.JTextField currentStockFld;
    private javax.swing.JLabel currentStockLbl;
    private javax.swing.JLabel idLbl;
    private javax.swing.JButton insertMedicineBtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    public javax.swing.JTextField medicineIdFLd;
    private javax.swing.JTextField medicineNameFLd;
    public javax.swing.JTable medicineTable;
    private javax.swing.JTextField pricePerUnitFld;
    private javax.swing.JButton resetBtn;
    private javax.swing.JTextField stockFLd;
    private javax.swing.JTextField typeFld;
    private javax.swing.JButton updateBtn;
    // End of variables declaration//GEN-END:variables
    int mainstock, currStock, temp, add, ad, addcs;

    
    public void updatemedicine() {
        Medicines medicine = new Medicines();

        Connection con;
        PreparedStatement ps;
        ad = Integer.parseInt(stockFLd.getText().toString());
        temp = mainstock - currStock;
        addcs = currStock + ad;
        System.out.println("tempCurrettStock:" + temp);
        add = mainstock + ad;
        System.out.println("add:" + add);

        String sql = "UPDATE medicines SET medicine_name='" + medicineNameFLd.getText() + "',current_stock='" + addcs + "',medicine_type='" + typeFld.getText() + "',company_name='" + companyNameFLd.getText() + "',stock='" + add + "',price_per_unit='" + pricePerUnitFld.getText() + "' WHERE id='" + medicineIdFLd.getText() + "'";
        try {

            con = DriverManager.getConnection("jdbc:mysql://localhost/medical_store", "root", "");
            ps = con.prepareStatement(sql);

            int save = ps.executeUpdate();

            System.out.println("executeQuery");

            if (save > 0) {
                System.out.println("Inserted");
                JOptionPane.showMessageDialog(null, "saved");
                dispose();
                medicine.updateTable();
                medicine.setVisible(true);
                dispose();

            } else {
                System.out.println("Not Inserted");
                JOptionPane.showMessageDialog(null, "Not saved");

            }

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Insert Valid Data");
            dispose();
            medicine.setVisible(true);
        }

    }

    public void deleteMedicine() {
        Medicines medicine = new Medicines();


        String sql = "DELETE FROM  medicines  WHERE id='" + medicineIdFLd.getText() + "'";

        Connection con;
        PreparedStatement ps;
        try {

            con = DriverManager.getConnection("jdbc:mysql://localhost/medical_store", "root", "root");
            ps = con.prepareStatement(sql);

            int save = ps.executeUpdate();

            System.out.println("executeQuery");

            if (save > 0) {
                System.out.println("Deleted");
                JOptionPane.showMessageDialog(null, "Deleted");
                dispose();
                medicine.updateTable();
                medicine.setVisible(true);
                dispose();

            } else {
                System.out.println("Not Deleted");
                JOptionPane.showMessageDialog(null, "Not Deleted");

            }

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Deleted Valid Data");
            dispose();
            medicine.setVisible(true);
        }

    }

}
