By GokiSoft.com| 17:23 29/10/2021|
Java Advanced

[Assignment] >> Quản lý bán hàng + java swing BT1147

Viết chương trình sau :

- Triển khai lớp đối tường sau

1. Danh mục sản phầm -> gồm các thuộc tính sau : mã danh mục, tên danh mục

- tạo hàm tạo và các bộ getter/setter cho chương trình

- Viết hàm nhập dữ liệu và hiển thị dữ liệu cho danh mục

2. Sản phẩm -> gồm các thuộc tính : mã sản phẩm, mã danh mục, tên sp, giá, ngày nhập, ngày bán, hạn sử dụng, mô tả sản phẩm.

- tạo hàm tạo và các bộ getter/setter cho chương trình

- Viết hàm nhập dữ liệu và hiển thị dữ liệu cho danh mục

Sử dụng swing thiết kế chương trình sau, hoặc java FX & Cơ sở dữ liệu

1. Form => Thêm, sửa, xoá danh mục sản phẩm và hiển thị danh mục sản phẩm

2. Form => Thêm, sửa, xoá sản phẩm và hiển thị danh mục sản phẩm, tạo các bộ lọc filter để lọc theo

2.1. Danh mục sản phẩm

2.2 Hạn sử dụng (còn hạn hay đã hết hạn) (lấy giờ hiện tại của hệ thống để kiểm tra với HSD của sản phẩm)

2.3 Báo các mặt hàng sắp hết hạn sử dụng  (lấy giờ hiện tại của hệ thống và kiểm tra vs HSD, nếu giờ hiện tại <= HSD và giờ hiện tại >= HSD - 7 ngày thì in  kết quả ra)

2.4 Thêm nhiều tính năng nữa cang tốt, tự mở rộng, ...

2.5 danh sách các sản phẩm đã bán

2.6 danh sách sản phầm còn tồn kho

2.7 Tìm kiểm sản phẩm

3. Thống kê

3.1. Báo cáo sản phẩm bán trong ngày

3.2 Bao cáo sản phẩm bán trong tuần

3.3 Báo cáo sản phẩm bán trong tháng

4. Viết đăng ký, đăng nhập tài khoản khi vào sử dụng hệ thống

Liên kết rút gọn:

https://gokisoft.com/1147

Bình luận

avatar
Phạm Ngọc Minh [T1907A]
2020-04-12 09:35:58



create table product (
   id int primary key auto_increment,
   proName varchar(100),
   price float default 0,
   importDate datetime,
   sellDate datetime,
   HSD datetime
)




package Assignment;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class productModify {
    
public static ArrayList<Product> showAll() {
        ArrayList<Product> list = new ArrayList<>();
        Connection conn = null;
        Statement statement = null;
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost/productAss?serverTimezone=UTC","root","");
            statement = conn.createStatement();
            String sql = "Select * from product" ;
            ResultSet resultset = statement.executeQuery(sql);
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            if (statement!= null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        

        return list;
    }

    public static void update(Product pro) {
        Connection conn = null;
        PreparedStatement statement = null;

        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost/productAss?serverTimezone=UTC","root","");

            String sql = "insert into product(proName,importDate,sellDate,HSD,price) values(?,Convert(?,Datetime),Convert(?,Datetime),Convert(?,Datetime),?)";
            statement = conn.prepareCall(sql);
            statement.setString(1, pro.getProductName());
            statement.setString(2, pro.getNgayNhap());
            statement.setString(3, pro.getNgayBan());
            statement.setString(4, pro.getHSD());
            statement.setFloat(5, pro.getPrice());
            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    public static void delete(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {
            //lay tat ca danh sach sinh vien
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/productAss?serverTimezone=UTC", "root", "");

            String sql = "delete from product where ProID = ?";
            statement = connection.prepareCall(sql);
            
            statement.setInt(1, id);
            
            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    public static void Edit(Product pro) {
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/productAss?serverTimezone=UTC", "root", "");

            String sql = "update product set proName=?,importDate=?,sellDate=?,HSD=?,price=? where ProID = ?";
            statement = connection.prepareCall(sql);
            statement.setString(1,pro.getProductName() );
            statement.setString(2,pro.getNgayNhap() );
            statement.setString(3,pro.getNgayBan() );
            statement.setString(4, pro.getHSD());
            statement.setFloat(5,pro.getPrice() );
            statement.setInt(6, pro.getProductId());
            
            
            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    public static ArrayList<Product> findByproName(String proName) {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/productAss?serverTimezone=UTC", "root", "");

            String sql = "select * from product where proName like ?";
            statement = connection.prepareCall(sql);
            statement.setString(1, "%"+proName+"%");
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
    public static ArrayList<Product> tonkho() {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/productAss?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (sellDate = '' or sellDate = ' ' or sellDate = null )";
            statement = connection.prepareCall(sql);
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
    public static ArrayList<Product> trongngay() {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/productAss?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (CURDATE() = sellDate)";
            statement = connection.prepareCall(sql);
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
    public static ArrayList<Product> trongtuan() {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/productAss?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (YEARWEEK(importDate) = YEARWEEK(sellDate))";
            statement = connection.prepareCall(sql);
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
    public static ArrayList<Product> trongthang() {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/productAss?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (MONTH(importDate) = MONTH(sellDate))";
            statement = connection.prepareCall(sql);
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
    
    
    public static ArrayList<Product> conhan() {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/productAss?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (CURDATE() < HSD )";
            statement = connection.prepareCall(sql);
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
    public static ArrayList<Product> hethan() {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/productAss?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (CURDATE() > HSD)";
            statement = connection.prepareCall(sql);
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
    public static ArrayList<Product> daban() {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/productAss?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (selldate > 0)";
            statement = connection.prepareCall(sql);
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
}




package Assignment;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public final class productFrame extends javax.swing.JFrame {

    ArrayList<Product> list = new ArrayList<>();

    DefaultTableModel tableModel;
    productModify productModify;

   
    public productFrame() {
        initComponents();
        tableModel = (DefaultTableModel) tblProduct.getModel();
        showall();
    }

    public void showall() {
        list = productModify.showAll();
        tableModel.setRowCount(0);
        for (Product product : list) {
            tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                product.getProductName(),
                product.getNgayNhap(),
                product.getNgayBan(),
                product.getHSD(),
                product.getPrice()});
        }

    }

    /**
     * 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">                          
    private void initComponents() {

        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jCheckBoxMenuItem1 = new javax.swing.JCheckBoxMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txttensp = new javax.swing.JTextField();
        txtNgaynhap = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtngayban = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtHSD = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtprice = new javax.swing.JTextField();
        txtgia = new javax.swing.JLabel();
        btnupdate = new javax.swing.JButton();
        btnedit = new javax.swing.JButton();
        btndelete = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblProduct = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        mnFilter = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenuItem9 = new javax.swing.JMenuItem();
        jMenuItem10 = new javax.swing.JMenuItem();
        jMenuItem11 = new javax.swing.JMenuItem();

        jMenuItem1.setText("jMenuItem1");

        jMenuItem2.setText("jMenuItem2");

        jCheckBoxMenuItem1.setSelected(true);
        jCheckBoxMenuItem1.setText("jCheckBoxMenuItem1");

        jMenuItem3.setText("jMenuItem3");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Tên sản Phẩm : ");

        txttensp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txttenspActionPerformed(evt);
            }
        });

        txtNgaynhap.setText("YYYY-MM-DD");

        jLabel2.setText("Ngày nhập  : ");

        txtngayban.setText("YYYY-MM-DD");

        jLabel3.setText("Ngày bán : ");

        txtHSD.setText("YYYY-MM-DD");

        jLabel4.setText("HSD : ");

        txtgia.setText("Price : ");

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

        btnedit.setText("Edit");
        btnedit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btneditActionPerformed(evt);
            }
        });

        btndelete.setText("Delete");
        btndelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btndeleteActionPerformed(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(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtHSD, javax.swing.GroupLayout.PREFERRED_SIZE, 401, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtngayban, javax.swing.GroupLayout.PREFERRED_SIZE, 401, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtNgaynhap, javax.swing.GroupLayout.PREFERRED_SIZE, 401, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(38, 38, 38)
                                .addComponent(txttensp, javax.swing.GroupLayout.PREFERRED_SIZE, 401, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(txtgia)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(btnupdate)
                                .addGap(18, 18, 18)
                                .addComponent(btnedit)
                                .addGap(18, 18, 18)
                                .addComponent(btndelete)
                                .addGap(234, 234, 234))
                            .addComponent(txtprice, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 401, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txttensp, 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(jLabel2)
                    .addComponent(txtNgaynhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtngayban, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtHSD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtgia)
                    .addComponent(txtprice, 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(btnupdate)
                    .addComponent(btnedit)
                    .addComponent(btndelete))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

            },
            new String [] {
                "ProductId", "ProductName", "inportDate", "sellDate", "HSD", "Price"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblProduct);

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        mnFilter.setText("Filter");
        mnFilter.setActionCommand("Filter");

        jMenuItem4.setText("Search Product");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        mnFilter.add(jMenuItem4);

        jMenuItem5.setText("Product Inventory List");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        mnFilter.add(jMenuItem5);

        jMenuItem6.setText("List Of Products Sold");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        mnFilter.add(jMenuItem6);

        jMenuItem7.setText("Products Sold During The Week");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        mnFilter.add(jMenuItem7);

        jMenuItem8.setText("Products Sold During The Month");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        mnFilter.add(jMenuItem8);

        jMenuItem9.setText("Products Sold During The Day");
        jMenuItem9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem9ActionPerformed(evt);
            }
        });
        mnFilter.add(jMenuItem9);

        jMenuItem10.setText("Still Valid");
        jMenuItem10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem10ActionPerformed(evt);
            }
        });
        mnFilter.add(jMenuItem10);

        jMenuItem11.setText("Expired");
        jMenuItem11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem11ActionPerformed(evt);
            }
        });
        mnFilter.add(jMenuItem11);

        jMenuBar1.add(mnFilter);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .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, 657, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(32, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void txttenspActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
    }                                        

    private void btnupdateActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:

        String proName = txttensp.getText();
        String HSD = txtHSD.getText();
        String importDate = txtNgaynhap.getText();
        String sellDate = txtngayban.getText();
        float price = Float.parseFloat(txtprice.getText());
        Product pr = new Product();
        pr.setProductName(proName);
        pr.setHSD(HSD);
        pr.setNgayBan(sellDate);
        pr.setNgayNhap(importDate);
        pr.setPrice(price);

        productModify.update(pr);

        System.out.println(pr.toString());

        showall();
    }                                         

    private void btndeleteActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
       int selectedIndex = tblProduct.getSelectedRow();
        if(selectedIndex >= 0) {
            Product pro = list.get(selectedIndex);
            
            int option = JOptionPane.showConfirmDialog(this, "Do you want to delete this item?");
            System.out.println("option : " + option);
            
            if(option == 0) {
                productModify.delete(pro.getProductId());
                
                showall();
            }
        }
        
    }                                         

    private void btneditActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        String proName = txttensp.getText();
        String HSD = txtHSD.getText();
        String importDate = txtNgaynhap.getText();
        String sellDate = txtngayban.getText();
        float price = Float.parseFloat(txtprice.getText());
        Product pr = new Product(proName,importDate,sellDate,HSD,price);
        int selectedIndex = tblProduct.getSelectedRow();
        if(selectedIndex >= 0){
            Product pro = list.get(selectedIndex);
            pr.setProductId(pro.getProductId());
        }
        productModify.Edit(pr);
        showall();
    }                                       

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        String input = JOptionPane.showInputDialog(this, "Enter full name to search");
        if(input != null && input.length() > 0) {
            list = productModify.findByproName(input);
            
            tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getNgayNhap(),
                    product.getNgayBan(),
                    product.getHSD(),
                    product.getPrice()});
            });
        } else {
            showall();
        }
    }                                          

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        list = productModify.tonkho();
        tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getNgayNhap(),
                    product.getNgayBan(),
                    product.getHSD(),
                    product.getPrice()});
            });
    }                                          

    private void jMenuItem9ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        list = productModify.trongngay();
        tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getNgayNhap(),
                    product.getNgayBan(),
                    product.getHSD(),
                    product.getPrice()});
            });
    }                                          

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        list = productModify.trongtuan();
        tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getNgayNhap(),
                    product.getNgayBan(),
                    product.getHSD(),
                    product.getPrice()});
            });
    }                                          

    private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        list = productModify.trongthang();
        tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getNgayNhap(),
                    product.getNgayBan(),
                    product.getHSD(),
                    product.getPrice()});
            });
    }                                          

    private void jMenuItem11ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        list = productModify.hethan();
        tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getNgayNhap(),
                    product.getNgayBan(),
                    product.getHSD(),
                    product.getPrice()});
            });
    }                                           

    private void jMenuItem10ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        list = productModify.conhan();
        tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getNgayNhap(),
                    product.getNgayBan(),
                    product.getHSD(),
                    product.getPrice()});
            });
    }                                           

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        list = productModify.daban();
        tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getNgayNhap(),
                    product.getNgayBan(),
                    product.getHSD(),
                    product.getPrice()});
            });
    }                                          

    
    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(productFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(productFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(productFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(productFrame.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 productFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btndelete;
    private javax.swing.JButton btnedit;
    private javax.swing.JButton btnupdate;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem11;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenu mnFilter;
    private javax.swing.JTable tblProduct;
    private javax.swing.JTextField txtHSD;
    private javax.swing.JTextField txtNgaynhap;
    private javax.swing.JLabel txtgia;
    private javax.swing.JTextField txtngayban;
    private javax.swing.JTextField txtprice;
    private javax.swing.JTextField txttensp;
    // End of variables declaration                   
}




package Assignment;

public class Product {
    
String  productName ,ngayNhap , ngayBan , HSD ;
    
    float price;
    int productId;

    public Product() {
    }

    public Product( String productName, String ngayNhap, String ngayBan, String HSD, float price) {
        
        this.productName = productName;
        this.ngayNhap = ngayNhap;
        this.ngayBan = ngayBan;
        this.HSD = HSD;
        this.price = price;
    }

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getNgayNhap() {
        return ngayNhap;
    }

    public void setNgayNhap(String ngayNhap) {
        this.ngayNhap = ngayNhap;
    }

    public String getNgayBan() {
        return ngayBan;
    }

    public void setNgayBan(String ngayBan) {
        this.ngayBan = ngayBan;
    }

    public String getHSD() {
        return HSD;
    }

    public void setHSD(String HSD) {
        this.HSD = HSD;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Product{" + "productName=" + productName + ", ngayNhap=" + ngayNhap + ", ngayBan=" + ngayBan + ", HSD=" + HSD + ", price=" + price + ", productId=" + productId + '}';
    }
    
}


avatar
Trần Anh Quân [T1907A]
2020-04-11 19:46:46



/*
 * 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 assignment;

/**
 *
 * @author Anh Quan
 */
public class Product {
    
String  productName ,ngayNhap , ngayBan , HSD ;
    
    float price;
    int productId;

    public Product() {
    }

    public Product( String productName, String ngayNhap, String ngayBan, String HSD, float price) {
        
        this.productName = productName;
        this.ngayNhap = ngayNhap;
        this.ngayBan = ngayBan;
        this.HSD = HSD;
        this.price = price;
    }

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getNgayNhap() {
        return ngayNhap;
    }

    public void setNgayNhap(String ngayNhap) {
        this.ngayNhap = ngayNhap;
    }

    public String getNgayBan() {
        return ngayBan;
    }

    public void setNgayBan(String ngayBan) {
        this.ngayBan = ngayBan;
    }

    public String getHSD() {
        return HSD;
    }

    public void setHSD(String HSD) {
        this.HSD = HSD;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Product{" + "productName=" + productName + ", ngayNhap=" + ngayNhap + ", ngayBan=" + ngayBan + ", HSD=" + HSD + ", price=" + price + ", productId=" + productId + '}';
    }
    
}



/*
 * 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 assignment;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author Anh Quan
 */
    
public final class productFrame extends javax.swing.JFrame {

    ArrayList<Product> list = new ArrayList<>();

    DefaultTableModel tableModel;
    productModify productModify;

    /**
     * Creates new form productFrame
     */
    public productFrame() {
        initComponents();
        tableModel = (DefaultTableModel) tblProduct.getModel();
        showall();
    }

    public void showall() {
        list = productModify.showAll();
        tableModel.setRowCount(0);
        for (Product product : list) {
            tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                product.getProductName(),
                product.getNgayNhap(),
                product.getNgayBan(),
                product.getHSD(),
                product.getPrice()});
        }

    }

    /**
     * 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">                          
    private void initComponents() {

        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jCheckBoxMenuItem1 = new javax.swing.JCheckBoxMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txttensp = new javax.swing.JTextField();
        txtNgaynhap = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtngayban = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtHSD = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtprice = new javax.swing.JTextField();
        txtgia = new javax.swing.JLabel();
        btnupdate = new javax.swing.JButton();
        btnedit = new javax.swing.JButton();
        btndelete = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblProduct = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        mnFilter = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenuItem9 = new javax.swing.JMenuItem();
        jMenuItem10 = new javax.swing.JMenuItem();
        jMenuItem11 = new javax.swing.JMenuItem();

        jMenuItem1.setText("jMenuItem1");

        jMenuItem2.setText("jMenuItem2");

        jCheckBoxMenuItem1.setSelected(true);
        jCheckBoxMenuItem1.setText("jCheckBoxMenuItem1");

        jMenuItem3.setText("jMenuItem3");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Tên sản Phẩm : ");

        txttensp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txttenspActionPerformed(evt);
            }
        });

        txtNgaynhap.setText("YYYY-MM-DD");

        jLabel2.setText("Ngày nhập  : ");

        txtngayban.setText("YYYY-MM-DD");

        jLabel3.setText("Ngày bán : ");

        txtHSD.setText("YYYY-MM-DD");

        jLabel4.setText("HSD : ");

        txtgia.setText("Price : ");

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

        btnedit.setText("Edit");
        btnedit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btneditActionPerformed(evt);
            }
        });

        btndelete.setText("Delete");
        btndelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btndeleteActionPerformed(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(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtHSD, javax.swing.GroupLayout.PREFERRED_SIZE, 401, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtngayban, javax.swing.GroupLayout.PREFERRED_SIZE, 401, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtNgaynhap, javax.swing.GroupLayout.PREFERRED_SIZE, 401, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(38, 38, 38)
                                .addComponent(txttensp, javax.swing.GroupLayout.PREFERRED_SIZE, 401, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(txtgia)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(btnupdate)
                                .addGap(18, 18, 18)
                                .addComponent(btnedit)
                                .addGap(18, 18, 18)
                                .addComponent(btndelete)
                                .addGap(234, 234, 234))
                            .addComponent(txtprice, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 401, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txttensp, 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(jLabel2)
                    .addComponent(txtNgaynhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtngayban, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtHSD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtgia)
                    .addComponent(txtprice, 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(btnupdate)
                    .addComponent(btnedit)
                    .addComponent(btndelete))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

            },
            new String [] {
                "ProductId", "ProductName", "inportDate", "sellDate", "HSD", "Price"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblProduct);

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        mnFilter.setText("Filter");
        mnFilter.setActionCommand("Filter");

        jMenuItem4.setText("Search Product");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        mnFilter.add(jMenuItem4);

        jMenuItem5.setText("Product Inventory List");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        mnFilter.add(jMenuItem5);

        jMenuItem6.setText("List Of Products Sold");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        mnFilter.add(jMenuItem6);

        jMenuItem7.setText("Products Sold During The Week");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        mnFilter.add(jMenuItem7);

        jMenuItem8.setText("Products Sold During The Month");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        mnFilter.add(jMenuItem8);

        jMenuItem9.setText("Products Sold During The Day");
        jMenuItem9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem9ActionPerformed(evt);
            }
        });
        mnFilter.add(jMenuItem9);

        jMenuItem10.setText("Still Valid");
        jMenuItem10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem10ActionPerformed(evt);
            }
        });
        mnFilter.add(jMenuItem10);

        jMenuItem11.setText("Expired");
        jMenuItem11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem11ActionPerformed(evt);
            }
        });
        mnFilter.add(jMenuItem11);

        jMenuBar1.add(mnFilter);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .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, 657, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(32, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void txttenspActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
    }                                        

    private void btnupdateActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:

        String proName = txttensp.getText();
        String HSD = txtHSD.getText();
        String importDate = txtNgaynhap.getText();
        String sellDate = txtngayban.getText();
        float price = Float.parseFloat(txtprice.getText());
        Product pr = new Product();
        pr.setProductName(proName);
        pr.setHSD(HSD);
        pr.setNgayBan(sellDate);
        pr.setNgayNhap(importDate);
        pr.setPrice(price);

        productModify.update(pr);

        System.out.println(pr.toString());

        showall();
    }                                         

    private void btndeleteActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
       int selectedIndex = tblProduct.getSelectedRow();
        if(selectedIndex >= 0) {
            Product pro = list.get(selectedIndex);
            
            int option = JOptionPane.showConfirmDialog(this, "Do you want to delete this item?");
            System.out.println("option : " + option);
            
            if(option == 0) {
                productModify.delete(pro.getProductId());
                
                showall();
            }
        }
        
    }                                         

    private void btneditActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        String proName = txttensp.getText();
        String HSD = txtHSD.getText();
        String importDate = txtNgaynhap.getText();
        String sellDate = txtngayban.getText();
        float price = Float.parseFloat(txtprice.getText());
        Product pr = new Product(proName,importDate,sellDate,HSD,price);
        int selectedIndex = tblProduct.getSelectedRow();
        if(selectedIndex >= 0){
            Product pro = list.get(selectedIndex);
            pr.setProductId(pro.getProductId());
        }
        productModify.Edit(pr);
        showall();
    }                                       

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        String input = JOptionPane.showInputDialog(this, "Enter full name to search");
        if(input != null && input.length() > 0) {
            list = productModify.findByproName(input);
            
            tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getNgayNhap(),
                    product.getNgayBan(),
                    product.getHSD(),
                    product.getPrice()});
            });
        } else {
            showall();
        }
    }                                          

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        list = productModify.tonkho();
        tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getNgayNhap(),
                    product.getNgayBan(),
                    product.getHSD(),
                    product.getPrice()});
            });
    }                                          

    private void jMenuItem9ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        list = productModify.trongngay();
        tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getNgayNhap(),
                    product.getNgayBan(),
                    product.getHSD(),
                    product.getPrice()});
            });
    }                                          

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        list = productModify.trongtuan();
        tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getNgayNhap(),
                    product.getNgayBan(),
                    product.getHSD(),
                    product.getPrice()});
            });
    }                                          

    private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        list = productModify.trongthang();
        tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getNgayNhap(),
                    product.getNgayBan(),
                    product.getHSD(),
                    product.getPrice()});
            });
    }                                          

    private void jMenuItem11ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        list = productModify.hethan();
        tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getNgayNhap(),
                    product.getNgayBan(),
                    product.getHSD(),
                    product.getPrice()});
            });
    }                                           

    private void jMenuItem10ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        list = productModify.conhan();
        tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getNgayNhap(),
                    product.getNgayBan(),
                    product.getHSD(),
                    product.getPrice()});
            });
    }                                           

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        list = productModify.daban();
        tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getNgayNhap(),
                    product.getNgayBan(),
                    product.getHSD(),
                    product.getPrice()});
            });
    }                                          

    /**
     * @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(productFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(productFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(productFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(productFrame.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 productFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btndelete;
    private javax.swing.JButton btnedit;
    private javax.swing.JButton btnupdate;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem11;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenu mnFilter;
    private javax.swing.JTable tblProduct;
    private javax.swing.JTextField txtHSD;
    private javax.swing.JTextField txtNgaynhap;
    private javax.swing.JLabel txtgia;
    private javax.swing.JTextField txtngayban;
    private javax.swing.JTextField txtprice;
    private javax.swing.JTextField txttensp;
    // End of variables declaration                   
}



/*
 * 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 assignment;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Anh Quan
 */
public class productModify {
    
public static ArrayList<Product> showAll() {
        ArrayList<Product> list = new ArrayList<>();
        Connection conn = null;
        Statement statement = null;
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost/productAss?serverTimezone=UTC","root","");
            statement = conn.createStatement();
            String sql = "Select * from product" ;
            ResultSet resultset = statement.executeQuery(sql);
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            if (statement!= null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        

        return list;
    }

    public static void update(Product pro) {
        Connection conn = null;
        PreparedStatement statement = null;

        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost/productAss?serverTimezone=UTC","root","");

            String sql = "insert into product(proName,importDate,sellDate,HSD,price) values(?,Convert(?,Datetime),Convert(?,Datetime),Convert(?,Datetime),?)";
            statement = conn.prepareCall(sql);
            statement.setString(1, pro.getProductName());
            statement.setString(2, pro.getNgayNhap());
            statement.setString(3, pro.getNgayBan());
            statement.setString(4, pro.getHSD());
            statement.setFloat(5, pro.getPrice());
            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    public static void delete(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {
            //lay tat ca danh sach sinh vien
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/productAss?serverTimezone=UTC", "root", "");

            String sql = "delete from product where ProID = ?";
            statement = connection.prepareCall(sql);
            
            statement.setInt(1, id);
            
            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    public static void Edit(Product pro) {
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/productAss?serverTimezone=UTC", "root", "");

            String sql = "update product set proName=?,importDate=?,sellDate=?,HSD=?,price=? where ProID = ?";
            statement = connection.prepareCall(sql);
            statement.setString(1,pro.getProductName() );
            statement.setString(2,pro.getNgayNhap() );
            statement.setString(3,pro.getNgayBan() );
            statement.setString(4, pro.getHSD());
            statement.setFloat(5,pro.getPrice() );
            statement.setInt(6, pro.getProductId());
            
            
            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    public static ArrayList<Product> findByproName(String proName) {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/productAss?serverTimezone=UTC", "root", "");

            String sql = "select * from product where proName like ?";
            statement = connection.prepareCall(sql);
            statement.setString(1, "%"+proName+"%");
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
    public static ArrayList<Product> tonkho() {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/productAss?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (sellDate = '' or sellDate = ' ' or sellDate = null )";
            statement = connection.prepareCall(sql);
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
    public static ArrayList<Product> trongngay() {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/productAss?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (CURDATE() = sellDate)";
            statement = connection.prepareCall(sql);
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
    public static ArrayList<Product> trongtuan() {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/productAss?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (YEARWEEK(importDate) = YEARWEEK(sellDate))";
            statement = connection.prepareCall(sql);
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
    public static ArrayList<Product> trongthang() {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/productAss?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (MONTH(importDate) = MONTH(sellDate))";
            statement = connection.prepareCall(sql);
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
    
    
    public static ArrayList<Product> conhan() {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/productAss?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (CURDATE() < HSD )";
            statement = connection.prepareCall(sql);
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
    public static ArrayList<Product> hethan() {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/productAss?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (CURDATE() > HSD)";
            statement = connection.prepareCall(sql);
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
    public static ArrayList<Product> daban() {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/productAss?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (selldate > 0)";
            statement = connection.prepareCall(sql);
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
}



create table product (
   id int primary key auto_increment,
   proName varchar(100),
   price float default 0,
   importDate datetime,
   sellDate datetime,
   HSD datetime
)


avatar
NguyenHuuThanh [T1907A]
2020-04-10 04:48:40



/*
 * 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 newprojectjavaswing;

/**
 *
 * @author abc
 */
public class SanPham {
    String id , maDanhMuc;
    String namesp ;
    Float price;
    String inputdate,outputdate,hsd,description;
    
    public SanPham()
    {
        
    }

    public SanPham(String id, String maDanhMuc, String namesp, Float price, String inputdate, String outputdate, String hsd, String description) {
        this.id = id;
        this.maDanhMuc = maDanhMuc;
        this.namesp = namesp;
        this.price = price;
        this.inputdate = inputdate;
        this.outputdate = outputdate;
        this.hsd = hsd;
        this.description = description;
    }

    
    
    

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getMaDanhMuc() {
        return maDanhMuc;
    }

    public void setMaDanhMuc(String maDanhMuc) {
        this.maDanhMuc = maDanhMuc;
    }

    public String getNamesp() {
        return namesp;
    }

    public void setNamesp(String namesp) {
        this.namesp = namesp;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public String getInputdate() {
        return inputdate;
    }

    public void setInputdate(String inputdate) {
        this.inputdate = inputdate;
    }

    public String getOutputdate() {
        return outputdate;
    }

    public void setOutputdate(String outputdate) {
        this.outputdate = outputdate;
    }

    public String getHsd() {
        return hsd;
    }

    public void setHsd(String hsd) {
        this.hsd = hsd;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
    

    
}
/*
 * 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 newprojectjavaswing;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author abc
 */
public class FormSanPham extends javax.swing.JFrame {

    DefaultTableModel tableModel;
    
    List<SanPham> sanphamlist;
    /**
     * Creates new form FormSanPham
     */
    public FormSanPham() {
        initComponents();
         tableModel = (DefaultTableModel) jTable2.getModel();
         sanphamlist = new ArrayList<>();
    }

    /**
     * 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">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtIDSP = new javax.swing.JTextField();
        txtIDDanhmuc = new javax.swing.JTextField();
        txtNameSP = new javax.swing.JTextField();
        txtPrice = new javax.swing.JTextField();
        txtInputDate = new javax.swing.JTextField();
        txtOutputDate = new javax.swing.JTextField();
        txtExpiryDate = new javax.swing.JTextField();
        txtDescription = new javax.swing.JTextField();
        btnSave = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        btnfindproduct = new javax.swing.JButton();
        btnSoldProduct = new javax.swing.JButton();
        btnRemainproduct = new javax.swing.JButton();
        txtExpiredProduct = new javax.swing.JButton();
        btnGoingexpiredproduct = new javax.swing.JButton();
        btnAllProduct = new javax.swing.JButton();
        btnProductDM = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("IDSP");

        jLabel2.setText("ID Danh mục");

        jLabel3.setText("Name SP");

        jLabel4.setText("Price");

        jLabel5.setText("InputDate");

        jLabel6.setText("OutputDate");

        jLabel7.setText("ExpiryDate");

        jLabel8.setText("Description");

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

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

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

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

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addGap(101, 101, 101)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtDescription, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                            .addComponent(txtExpiryDate)
                            .addComponent(txtOutputDate)
                            .addComponent(txtInputDate)
                            .addComponent(txtPrice)
                            .addComponent(txtNameSP)
                            .addComponent(txtIDDanhmuc)
                            .addComponent(txtIDSP)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addComponent(btnSave)
                        .addGap(18, 18, 18)
                        .addComponent(btnDelete)
                        .addGap(18, 18, 18)
                        .addComponent(btnUpdate)
                        .addGap(29, 29, 29)
                        .addComponent(btnReset)))
                .addContainerGap(43, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(txtIDSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtIDDanhmuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtNameSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtInputDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtOutputDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtExpiryDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txtDescription, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSave)
                    .addComponent(btnDelete)
                    .addComponent(btnUpdate)
                    .addComponent(btnReset))
                .addContainerGap(45, Short.MAX_VALUE))
        );

        btnfindproduct.setText("Find product ");
        btnfindproduct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnfindproductActionPerformed(evt);
            }
        });

        btnSoldProduct.setText("Sold product");
        btnSoldProduct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSoldProductActionPerformed(evt);
            }
        });

        btnRemainproduct.setText("Remain product");

        txtExpiredProduct.setText("Expired Product");

        btnGoingexpiredproduct.setText("Goingtoexpired Product");

        btnAllProduct.setText("AllProduct");

        btnProductDM.setText("ProductDM");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(96, 96, 96)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnfindproduct)
                    .addComponent(btnSoldProduct)
                    .addComponent(btnRemainproduct)
                    .addComponent(txtExpiredProduct)
                    .addComponent(btnGoingexpiredproduct)
                    .addComponent(btnAllProduct)
                    .addComponent(btnProductDM))
                .addGap(162, 162, 162)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(70, 70, 70)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(82, 82, 82)
                        .addComponent(btnfindproduct)
                        .addGap(26, 26, 26)
                        .addComponent(btnSoldProduct)
                        .addGap(30, 30, 30)
                        .addComponent(btnRemainproduct)
                        .addGap(39, 39, 39)
                        .addComponent(txtExpiredProduct)
                        .addGap(32, 32, 32)
                        .addComponent(btnGoingexpiredproduct)
                        .addGap(38, 38, 38)
                        .addComponent(btnAllProduct)
                        .addGap(41, 41, 41)
                        .addComponent(btnProductDM)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

            },
            new String [] {
                "IDSP", "IDDM", "NameSP", "Price", "InputDate", "OutputDate", "ExpiryDate", "Description"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(421, 421, 421)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(1698, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(173, 173, 173)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 464, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 12, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        // Save
        String idsp = txtIDSP.getText();
        String idDM = txtIDDanhmuc.getText();
        String namesp = txtNameSP.getText();
        Float price = Float.parseFloat(txtPrice.getText());
        String inputDate = txtInputDate.getText();
        String outputDate = txtOutputDate.getText();
        String expiryDate = txtExpiryDate.getText();
        String description = txtDescription.getText();
        
        SanPham sp = new SanPham(idsp,idDM,namesp,price,inputDate,outputDate,expiryDate,description);
        
        showSanpham();
        
    }                                       

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        // Update
        String idsp = txtIDSP.getText();
        String idDM = txtIDDanhmuc.getText();
        String namesp = txtNameSP.getText();
        Float price = Float.parseFloat(txtPrice.getText());
        String inputDate = txtInputDate.getText();
        String outputDate = txtOutputDate.getText();
        String expiryDate = txtExpiryDate.getText();
        String description = txtDescription.getText();
        
        SanPham sp = new SanPham(idsp,idDM,namesp,price,inputDate,outputDate,expiryDate,description);
        
        Sanphammodify.insert(sp);
        
        txtIDSP.setText("");
        txtIDDanhmuc.setText("");
        txtNameSP.setText("");
        txtPrice.setText("");
        txtInputDate.setText("");
        txtOutputDate.setText("");
        txtExpiryDate.setText("");
        txtDescription.setText("");
    }                                         

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        txtIDSP.setText("");
        txtIDDanhmuc.setText("");
        txtNameSP.setText("");
        txtPrice.setText("");
        txtInputDate.setText("");
        txtOutputDate.setText("");
        txtExpiryDate.setText("");
        txtDescription.setText("");
    }                                        

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        // Delete
        
        int selectedIndex = jTable2.getSelectedRow();
        if( selectedIndex >= 0 )
        {
            SanPham sp = sanphamlist.get(selectedIndex);
            
            
            int option = JOptionPane.showConfirmDialog(this, "Do you want to delete this item :");
            System.out.println("option : " + option);
            
            if ( option == 0 )
            {
                Sanphammodify.delete(sp.getId());
                
                
                showSanpham();
            }
            
            
        }
    }                                         

    private void btnfindproductActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
        // find product
         String input = JOptionPane.showInputDialog(this, "Enter id");
        if (input.length() > 0)
        {
            sanphamlist = Sanphammodify.findById(input);
            
            sanphamlist.forEach((SanPham) -> {
            tableModel.addRow(new Object[] {tableModel.getRowCount()+ 1 , SanPham.getId(),
                SanPham.getMaDanhMuc(), SanPham.getNamesp() , SanPham.getInputdate() , SanPham.getOutputdate() , SanPham.getHsd() , SanPham.getDescription()});
        });
        }
    }                                              

    private void btnSoldProductActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
        
        
    }                                              

    /**
     * @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(FormSanPham.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormSanPham.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormSanPham.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormSanPham.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 FormSanPham().setVisible(true);
            }
        });
    }
    
    private void showSanpham()
    {
        sanphamlist = Sanphammodify.findAll();
        
        tableModel.setRowCount(0);
        
        sanphamlist.forEach((SanPham) -> {
            tableModel.addRow(new Object[] {tableModel.getRowCount()+ 1 , SanPham.getId(),
                SanPham.getMaDanhMuc(), SanPham.getNamesp(), SanPham.getPrice() , SanPham.getInputdate() , SanPham.getOutputdate() , SanPham.getHsd() , SanPham.getDescription()});
            
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btnAllProduct;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnGoingexpiredproduct;
    private javax.swing.JButton btnProductDM;
    private javax.swing.JButton btnRemainproduct;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnSoldProduct;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JButton btnfindproduct;
    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.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField txtDescription;
    private javax.swing.JButton txtExpiredProduct;
    private javax.swing.JTextField txtExpiryDate;
    private javax.swing.JTextField txtIDDanhmuc;
    private javax.swing.JTextField txtIDSP;
    private javax.swing.JTextField txtInputDate;
    private javax.swing.JTextField txtNameSP;
    private javax.swing.JTextField txtOutputDate;
    private javax.swing.JTextField txtPrice;
    // End of variables declaration                   
}

/*
 * 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 javaswingjava05;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author abc
 */
public class Studentmodify {
   public static List<Student> findAll() {
        List<Student> studentlist = new ArrayList<>();

        Connection connection = null;

        // an instruction that explains what should happen
        java.sql.Statement statement = null;
        try {
            // Ket noi toi database : jdbc : mysql://localhost:3306/ ten ; tai khoan + mat khau
            //muc dich lay danh sach tat ca sinh vien :
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student04?serverTimezone=UTC", "root", "");

            //Query
            String sql = "select * from student";
            // Creates a Statement object for sending SQL statements to the database
            statement = connection.createStatement();
            // A table of data representing a database result set, which is usually generated by executing a statement that queries the database.
            ResultSet resultSet = statement.executeQuery(sql);
            // reselutSet.next() == moves the pointer of the current (ResultSet) object to the next row, from the current position
            while (resultSet.next()) {
                
                // Create a student getting from resultSet ( database ) 
                Student std = new Student(
                        resultSet.getInt("id"),
                        resultSet.getString("fullname"),
                        resultSet.getString("gender"),
                        resultSet.getString("rollno"),
                        resultSet.getString("email"),
                        resultSet.getString("address"));

                studentlist.add(std);
            }

        } catch (SQLException ex) {
            Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // neu connection da chay , dong connection.
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        return studentlist;
    }
   
   public static void update(Student std) {
        Connection connection = null;

        // for parametirized query
        PreparedStatement statement = null;
        try {
            // Ket noi toi database : jdbc : mysql://localhost:3306/ ten ; tai khoan + mat khau
            //muc dich lay danh sach tat ca sinh vien :
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student04?serverTimezone=UTC", "root", "");

            //Query
            String sql = "update student set fullname = ?, gender = ? , rollno = ? , email = ? , address = ? where id = ?";
            
            //
            statement = connection.prepareCall(sql);
            
            statement.setString(1, std.getFullname());
            statement.setString(2, std.getGender());
            statement.setString(3, std.getRollno());
            statement.setString(4, std.getEmail());
            statement.setString(5, std.getAddress());
            statement.setInt(6, std.getId());
            
            statement.execute();
            

        } catch (SQLException ex) {
            Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // neu connection da chay , dong connection.
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
     
   
   public static List<Student> findByRollno( String rollno ) {
        List<Student> studentlist = new ArrayList<>();

        Connection connection = null;

        // an instruction that explains what should happen
        PreparedStatement statement = null;
        try {
            // Ket noi toi database : jdbc : mysql://localhost:3306/ ten ; tai khoan + mat khau
            //muc dich lay danh sach tat ca sinh vien :
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student04?serverTimezone=UTC", "root", "");

            //Query
            String sql = "select * from student WHERE rollno = ?";
            // Creates a Statement object for sending SQL statements to the database
            statement = connection.prepareCall(sql);
            
            statement.setString(1, rollno);
            
            
            // A table of data representing a database result set, which is usually generated by executing a statement that queries the database.
            ResultSet resultSet = statement.executeQuery(sql);
            // reselutSet.next() == moves the pointer of the current (ResultSet) object to the next row, from the current position
            while (resultSet.next()) {
                
                // Create a student getting from resultSet ( database ) 
                Student std = new Student(resultSet.getInt("id"),
                        resultSet.getString("fullname"),
                        resultSet.getString("gender"),
                        resultSet.getString("rollno"),
                        resultSet.getString("email"),
                        resultSet.getString("address"));
                        

                studentlist.add(std);
            }

        } catch (SQLException ex) {
            Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // neu connection da chay , dong connection.
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        return studentlist;
    }
   
   public static void insert(Student std) {
        Connection connection = null;

        // for parametirized query
        PreparedStatement statement = null;
        try {
            // Ket noi toi database : jdbc : mysql://localhost:3306/ ten ; tai khoan + mat khau
            //muc dich lay danh sach tat ca sinh vien :
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student04?serverTimezone=UTC", "root", "");

            //Query
            String sql = "insert into student(fullname , gender , rollno , email , address) values(?,?,?,?,?)";
            //
            statement = connection.prepareCall(sql);

            statement.setString(1, std.getFullname());
            statement.setString(2, std.getGender());
            statement.setString(3, std.getRollno());
            statement.setString(4, std.getEmail());
            statement.setString(5, std.getAddress());

            statement.execute();

        } catch (SQLException ex) {
            Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // neu connection da chay , dong connection.
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}
/*
 * 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 newprojectjavaswing;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author abc
 */
public class Sanphammodify {
    public static List<SanPham> findAll()
    {
        List<SanPham> sanphamlist = new ArrayList<>();
        Connection connection = null;
        
        Statement statement = null;
        
        
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sanpham?serverTimezone=UTC","root","");
            
            String sql = "select * from sanpham";
            
            statement = connection.createStatement();
            
            ResultSet resultset = statement.executeQuery(sql);
            
            while(resultset.next())
            {
                SanPham sp = new SanPham(resultset.getString("id"),resultset.getString("idDM"),resultset.getString("nameSP"),resultset.getFloat("price"),resultset.getString("inputDate"),resultset.getString("outputDate"),resultset.getString("expiryDate"),resultset.getString("description"));
                
                
                sanphamlist.add(sp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            if(connection != null)
            {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(statement != null)
            {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        return sanphamlist;
    }
    
    public static void insert(SanPham sp) {
        Connection connection = null;

        // for parametirized query
        PreparedStatement statement = null;
        try {
            // Ket noi toi database : jdbc : mysql://localhost:3306/ ten ; tai khoan + mat khau
            //muc dich lay danh sach tat ca sinh vien :
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sanpham?serverTimezone=UTC", "root", "");

            //Query
            String sql = "insert into sanpham(id , idDM , nameSP , price ,inputDate ,outputDate, expiryDate ,description) values(?,?,?,?,?)";
            //
            statement = connection.prepareCall(sql);

            statement.setString(1, sp.getId());
            statement.setString(2, sp.getMaDanhMuc());
            statement.setString(3, sp.getNamesp());
            statement.setFloat(4, sp.getPrice());
            statement.setString(5, sp.getInputdate());
            statement.setString(6, sp.getOutputdate());
            statement.setString(7, sp.getHsd());
            statement.setString(8, sp.getDescription());

            statement.execute();

        } catch (SQLException ex) {
            Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // neu connection da chay , dong connection.
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
     public static void update(SanPham sp) {
        Connection connection = null;

        // for parametirized query
        PreparedStatement statement = null;
        try {
            // Ket noi toi database : jdbc : mysql://localhost:3306/ ten ; tai khoan + mat khau
            //muc dich lay danh sach tat ca sinh vien :
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sanpham?serverTimezone=UTC", "root", "");

            //Query
            String sql = "update sanpham set idDM = ? , namesp = ? , price = ? ,inputDate = ? , outputDate = ? ,expiryDate = ? , description = ? where id = ?";
            
            //
            statement = connection.prepareCall(sql);
            
            statement.setString(1, sp.getMaDanhMuc());
            statement.setString(2, sp.getNamesp());
            statement.setFloat(3, sp.getPrice());
            statement.setString(4, sp.getInputdate());
            statement.setString(5, sp.getOutputdate());
            statement.setString(6, sp.getHsd());
            statement.setString(7, sp.getDescription());
            statement.setString(8, sp.getId());
            
            
            statement.execute();
            

        } catch (SQLException ex) {
            Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // neu connection da chay , dong connection.
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
     
     public static void delete(String id) {
        Connection connection = null;

        // for parametirized query
        PreparedStatement statement = null;
        try {
            // Ket noi toi database : jdbc : mysql://localhost:3306/ ten ; tai khoan + mat khau
            //muc dich lay danh sach tat ca sinh vien :
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sanpham?serverTimezone=UTC", "root", "");

            //Query
            String sql = "delete from sanpham where id = ?";
            
            //
            statement = connection.prepareCall(sql);
            
            statement.setString(1, id);
            
            statement.execute();
            

        } catch (SQLException ex) {
            Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // neu connection da chay , dong connection.
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
     
     public static List<SanPham> findById( String id ) {
        List<SanPham> sanphamlist = new ArrayList<>();

        Connection connection = null;

        // an instruction that explains what should happen
        PreparedStatement statement = null;
        try {
            // Ket noi toi database : jdbc : mysql://localhost:3306/ ten ; tai khoan + mat khau
            //muc dich lay danh sach tat ca sinh vien :
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sanpham?serverTimezone=UTC", "root", "");

            //Query
            String sql = "select * from sanpham WHERE id = ?";
            // Creates a Statement object for sending SQL statements to the database
            statement = connection.prepareCall(sql);
            
            statement.setString(1, id);
            
            
            // A table of data representing a database result set, which is usually generated by executing a statement that queries the database.
            ResultSet resultSet = statement.executeQuery(sql);
            // reselutSet.next() == moves the pointer of the current (ResultSet) object to the next row, from the current position
            while (resultSet.next()) {
                
                // Create a student getting from resultSet ( database ) 
                SanPham sp = new SanPham(resultSet.getString("id"),
                        resultSet.getString("idDM"),
                        resultSet.getString("namesp"),
                        resultSet.getFloat("price"),
                        resultSet.getString("inputDate"),
                        resultSet.getString("outputDate"),
                        resultSet.getString("expiryDate"),
                        resultSet.getString("description"));
                        

                sanphamlist.add(sp);
            }

        } catch (SQLException ex) {
            Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // neu connection da chay , dong connection.
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        return sanphamlist;
    }
}
/*
 * 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 newprojectjavaswing;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author abc
 */
public class FormDanhMucmodify {
    public static void insert(Danhmucsanpham sp)
    {
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/danhmuc?serverTimezone=UTC", "root" ,"");
            
            String sql = "insert into danhmuc(idDanhMuc,nameDanhMuc) values (?,?)";
            
            statement = connection.prepareCall(sql);
            
            statement.setString(1,sp.getIdDM());
            statement.setString(2,sp.getNameDM());
            
            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(FormDanhMuc.class.getName()).log(Level.SEVERE, null, ex);
        } finally
        {
            if (statement != null)
            {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(FormDanhMuc.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(connection != null)
            {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(FormDanhMuc.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
    }
    
    public static  List<Danhmucsanpham> findAll() {
        List<Danhmucsanpham> listdm = new ArrayList<>();

        Connection connection = null;

        // an instruction that explains what should happen
        Statement statement = null;
        try {
            // Ket noi toi database : jdbc : mysql://localhost:3306/ ten ; tai khoan + mat khau
            //muc dich lay danh sach tat ca sinh vien :
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/danhmuc?serverTimezone=UTC", "root", "");

            //Query
            String sql = "select * from danhmuc";
            // Creates a Statement object for sending SQL statements to the database
            statement = connection.createStatement();
            // A table of data representing a database result set, which is usually generated by executing a statement that queries the database.
            ResultSet resultSet = statement.executeQuery(sql);
            // reselutSet.next() == moves the pointer of the current (ResultSet) object to the next row, from the current position
            while (resultSet.next()) {
                
                // Create a student getting from resultSet ( database ) 
                Danhmucsanpham sp = new Danhmucsanpham(resultSet.getInt("rollNo"),resultSet.getString("idDanhMuc"), resultSet.getString("nameDanhMuc"));

                listdm.add(sp);
            }

        } catch (SQLException ex) {
            Logger.getLogger(FormDanhMucmodify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(FormDanhMucmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // neu connection da chay , dong connection.
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(FormDanhMucmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        return listdm;
    }
    
    public static void delete(int rollNo) {
        Connection connection = null;

        // for parametirized query
        PreparedStatement statement = null;
        try {
            // Ket noi toi database : jdbc : mysql://localhost:3306/ ten ; tai khoan + mat khau
            //muc dich lay danh sach tat ca sinh vien :
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/danhmuc?serverTimezone=UTC", "root", "");

            //Query
            String sql = "delete from danhmuc where rollNo = ?";
            
            //
            statement = connection.prepareCall(sql);
            
            statement.setInt(1, rollNo);
            
            statement.execute();
            

        } catch (SQLException ex) {
            Logger.getLogger(FormDanhMucmodify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(FormDanhMucmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // neu connection da chay , dong connection.
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(FormDanhMucmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
     public static void update(Danhmucsanpham sp) {
        Connection connection = null;

        // for parametirized query
        PreparedStatement statement = null;
        try {
            // Ket noi toi database : jdbc : mysql://localhost:3306/ ten ; tai khoan + mat khau
            //muc dich lay danh sach tat ca sinh vien :
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/danhmuc?serverTimezone=UTC", "root", "");

            //Query
            String sql = "update danhmuc set idDanhmuc = ? ,nameDanhmuc = ? where rollNo = ?";
            
            //
            statement = connection.prepareCall(sql);
            
            statement.setString(1, sp.getIdDM());
            statement.setString(2, sp.getNameDM());
            statement.setInt(3, sp.getRollNo());
            
            statement.execute();
            

        } catch (SQLException ex) {
            Logger.getLogger(FormDanhMucmodify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(FormDanhMucmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // neu connection da chay , dong connection.
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(FormDanhMucmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    
    
    
}


avatar
NguyenHuuThanh [T1907A]
2020-04-10 04:48:40



/*
 * 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 newprojectjavaswing;

/**
 *
 * @author abc
 */
public class SanPham {
    String id , maDanhMuc;
    String namesp ;
    Float price;
    String inputdate,outputdate,hsd,description;
    
    public SanPham()
    {
        
    }

    public SanPham(String id, String maDanhMuc, String namesp, Float price, String inputdate, String outputdate, String hsd, String description) {
        this.id = id;
        this.maDanhMuc = maDanhMuc;
        this.namesp = namesp;
        this.price = price;
        this.inputdate = inputdate;
        this.outputdate = outputdate;
        this.hsd = hsd;
        this.description = description;
    }

    
    
    

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getMaDanhMuc() {
        return maDanhMuc;
    }

    public void setMaDanhMuc(String maDanhMuc) {
        this.maDanhMuc = maDanhMuc;
    }

    public String getNamesp() {
        return namesp;
    }

    public void setNamesp(String namesp) {
        this.namesp = namesp;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public String getInputdate() {
        return inputdate;
    }

    public void setInputdate(String inputdate) {
        this.inputdate = inputdate;
    }

    public String getOutputdate() {
        return outputdate;
    }

    public void setOutputdate(String outputdate) {
        this.outputdate = outputdate;
    }

    public String getHsd() {
        return hsd;
    }

    public void setHsd(String hsd) {
        this.hsd = hsd;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
    

    
}
/*
 * 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 newprojectjavaswing;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author abc
 */
public class FormSanPham extends javax.swing.JFrame {

    DefaultTableModel tableModel;
    
    List<SanPham> sanphamlist;
    /**
     * Creates new form FormSanPham
     */
    public FormSanPham() {
        initComponents();
         tableModel = (DefaultTableModel) jTable2.getModel();
         sanphamlist = new ArrayList<>();
    }

    /**
     * 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">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtIDSP = new javax.swing.JTextField();
        txtIDDanhmuc = new javax.swing.JTextField();
        txtNameSP = new javax.swing.JTextField();
        txtPrice = new javax.swing.JTextField();
        txtInputDate = new javax.swing.JTextField();
        txtOutputDate = new javax.swing.JTextField();
        txtExpiryDate = new javax.swing.JTextField();
        txtDescription = new javax.swing.JTextField();
        btnSave = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        btnfindproduct = new javax.swing.JButton();
        btnSoldProduct = new javax.swing.JButton();
        btnRemainproduct = new javax.swing.JButton();
        txtExpiredProduct = new javax.swing.JButton();
        btnGoingexpiredproduct = new javax.swing.JButton();
        btnAllProduct = new javax.swing.JButton();
        btnProductDM = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("IDSP");

        jLabel2.setText("ID Danh mục");

        jLabel3.setText("Name SP");

        jLabel4.setText("Price");

        jLabel5.setText("InputDate");

        jLabel6.setText("OutputDate");

        jLabel7.setText("ExpiryDate");

        jLabel8.setText("Description");

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

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

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

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

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addGap(101, 101, 101)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtDescription, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                            .addComponent(txtExpiryDate)
                            .addComponent(txtOutputDate)
                            .addComponent(txtInputDate)
                            .addComponent(txtPrice)
                            .addComponent(txtNameSP)
                            .addComponent(txtIDDanhmuc)
                            .addComponent(txtIDSP)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addComponent(btnSave)
                        .addGap(18, 18, 18)
                        .addComponent(btnDelete)
                        .addGap(18, 18, 18)
                        .addComponent(btnUpdate)
                        .addGap(29, 29, 29)
                        .addComponent(btnReset)))
                .addContainerGap(43, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(txtIDSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtIDDanhmuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtNameSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtInputDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtOutputDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtExpiryDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txtDescription, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSave)
                    .addComponent(btnDelete)
                    .addComponent(btnUpdate)
                    .addComponent(btnReset))
                .addContainerGap(45, Short.MAX_VALUE))
        );

        btnfindproduct.setText("Find product ");
        btnfindproduct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnfindproductActionPerformed(evt);
            }
        });

        btnSoldProduct.setText("Sold product");
        btnSoldProduct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSoldProductActionPerformed(evt);
            }
        });

        btnRemainproduct.setText("Remain product");

        txtExpiredProduct.setText("Expired Product");

        btnGoingexpiredproduct.setText("Goingtoexpired Product");

        btnAllProduct.setText("AllProduct");

        btnProductDM.setText("ProductDM");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(96, 96, 96)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnfindproduct)
                    .addComponent(btnSoldProduct)
                    .addComponent(btnRemainproduct)
                    .addComponent(txtExpiredProduct)
                    .addComponent(btnGoingexpiredproduct)
                    .addComponent(btnAllProduct)
                    .addComponent(btnProductDM))
                .addGap(162, 162, 162)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(70, 70, 70)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(82, 82, 82)
                        .addComponent(btnfindproduct)
                        .addGap(26, 26, 26)
                        .addComponent(btnSoldProduct)
                        .addGap(30, 30, 30)
                        .addComponent(btnRemainproduct)
                        .addGap(39, 39, 39)
                        .addComponent(txtExpiredProduct)
                        .addGap(32, 32, 32)
                        .addComponent(btnGoingexpiredproduct)
                        .addGap(38, 38, 38)
                        .addComponent(btnAllProduct)
                        .addGap(41, 41, 41)
                        .addComponent(btnProductDM)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

            },
            new String [] {
                "IDSP", "IDDM", "NameSP", "Price", "InputDate", "OutputDate", "ExpiryDate", "Description"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(421, 421, 421)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(1698, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(173, 173, 173)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 464, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 12, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        // Save
        String idsp = txtIDSP.getText();
        String idDM = txtIDDanhmuc.getText();
        String namesp = txtNameSP.getText();
        Float price = Float.parseFloat(txtPrice.getText());
        String inputDate = txtInputDate.getText();
        String outputDate = txtOutputDate.getText();
        String expiryDate = txtExpiryDate.getText();
        String description = txtDescription.getText();
        
        SanPham sp = new SanPham(idsp,idDM,namesp,price,inputDate,outputDate,expiryDate,description);
        
        showSanpham();
        
    }                                       

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        // Update
        String idsp = txtIDSP.getText();
        String idDM = txtIDDanhmuc.getText();
        String namesp = txtNameSP.getText();
        Float price = Float.parseFloat(txtPrice.getText());
        String inputDate = txtInputDate.getText();
        String outputDate = txtOutputDate.getText();
        String expiryDate = txtExpiryDate.getText();
        String description = txtDescription.getText();
        
        SanPham sp = new SanPham(idsp,idDM,namesp,price,inputDate,outputDate,expiryDate,description);
        
        Sanphammodify.insert(sp);
        
        txtIDSP.setText("");
        txtIDDanhmuc.setText("");
        txtNameSP.setText("");
        txtPrice.setText("");
        txtInputDate.setText("");
        txtOutputDate.setText("");
        txtExpiryDate.setText("");
        txtDescription.setText("");
    }                                         

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        txtIDSP.setText("");
        txtIDDanhmuc.setText("");
        txtNameSP.setText("");
        txtPrice.setText("");
        txtInputDate.setText("");
        txtOutputDate.setText("");
        txtExpiryDate.setText("");
        txtDescription.setText("");
    }                                        

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        // Delete
        
        int selectedIndex = jTable2.getSelectedRow();
        if( selectedIndex >= 0 )
        {
            SanPham sp = sanphamlist.get(selectedIndex);
            
            
            int option = JOptionPane.showConfirmDialog(this, "Do you want to delete this item :");
            System.out.println("option : " + option);
            
            if ( option == 0 )
            {
                Sanphammodify.delete(sp.getId());
                
                
                showSanpham();
            }
            
            
        }
    }                                         

    private void btnfindproductActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
        // find product
         String input = JOptionPane.showInputDialog(this, "Enter id");
        if (input.length() > 0)
        {
            sanphamlist = Sanphammodify.findById(input);
            
            sanphamlist.forEach((SanPham) -> {
            tableModel.addRow(new Object[] {tableModel.getRowCount()+ 1 , SanPham.getId(),
                SanPham.getMaDanhMuc(), SanPham.getNamesp() , SanPham.getInputdate() , SanPham.getOutputdate() , SanPham.getHsd() , SanPham.getDescription()});
        });
        }
    }                                              

    private void btnSoldProductActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
        
        
    }                                              

    /**
     * @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(FormSanPham.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormSanPham.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormSanPham.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormSanPham.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 FormSanPham().setVisible(true);
            }
        });
    }
    
    private void showSanpham()
    {
        sanphamlist = Sanphammodify.findAll();
        
        tableModel.setRowCount(0);
        
        sanphamlist.forEach((SanPham) -> {
            tableModel.addRow(new Object[] {tableModel.getRowCount()+ 1 , SanPham.getId(),
                SanPham.getMaDanhMuc(), SanPham.getNamesp(), SanPham.getPrice() , SanPham.getInputdate() , SanPham.getOutputdate() , SanPham.getHsd() , SanPham.getDescription()});
            
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btnAllProduct;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnGoingexpiredproduct;
    private javax.swing.JButton btnProductDM;
    private javax.swing.JButton btnRemainproduct;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnSoldProduct;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JButton btnfindproduct;
    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.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField txtDescription;
    private javax.swing.JButton txtExpiredProduct;
    private javax.swing.JTextField txtExpiryDate;
    private javax.swing.JTextField txtIDDanhmuc;
    private javax.swing.JTextField txtIDSP;
    private javax.swing.JTextField txtInputDate;
    private javax.swing.JTextField txtNameSP;
    private javax.swing.JTextField txtOutputDate;
    private javax.swing.JTextField txtPrice;
    // End of variables declaration                   
}

/*
 * 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 javaswingjava05;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author abc
 */
public class Studentmodify {
   public static List<Student> findAll() {
        List<Student> studentlist = new ArrayList<>();

        Connection connection = null;

        // an instruction that explains what should happen
        java.sql.Statement statement = null;
        try {
            // Ket noi toi database : jdbc : mysql://localhost:3306/ ten ; tai khoan + mat khau
            //muc dich lay danh sach tat ca sinh vien :
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student04?serverTimezone=UTC", "root", "");

            //Query
            String sql = "select * from student";
            // Creates a Statement object for sending SQL statements to the database
            statement = connection.createStatement();
            // A table of data representing a database result set, which is usually generated by executing a statement that queries the database.
            ResultSet resultSet = statement.executeQuery(sql);
            // reselutSet.next() == moves the pointer of the current (ResultSet) object to the next row, from the current position
            while (resultSet.next()) {
                
                // Create a student getting from resultSet ( database ) 
                Student std = new Student(
                        resultSet.getInt("id"),
                        resultSet.getString("fullname"),
                        resultSet.getString("gender"),
                        resultSet.getString("rollno"),
                        resultSet.getString("email"),
                        resultSet.getString("address"));

                studentlist.add(std);
            }

        } catch (SQLException ex) {
            Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // neu connection da chay , dong connection.
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        return studentlist;
    }
   
   public static void update(Student std) {
        Connection connection = null;

        // for parametirized query
        PreparedStatement statement = null;
        try {
            // Ket noi toi database : jdbc : mysql://localhost:3306/ ten ; tai khoan + mat khau
            //muc dich lay danh sach tat ca sinh vien :
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student04?serverTimezone=UTC", "root", "");

            //Query
            String sql = "update student set fullname = ?, gender = ? , rollno = ? , email = ? , address = ? where id = ?";
            
            //
            statement = connection.prepareCall(sql);
            
            statement.setString(1, std.getFullname());
            statement.setString(2, std.getGender());
            statement.setString(3, std.getRollno());
            statement.setString(4, std.getEmail());
            statement.setString(5, std.getAddress());
            statement.setInt(6, std.getId());
            
            statement.execute();
            

        } catch (SQLException ex) {
            Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // neu connection da chay , dong connection.
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
     
   
   public static List<Student> findByRollno( String rollno ) {
        List<Student> studentlist = new ArrayList<>();

        Connection connection = null;

        // an instruction that explains what should happen
        PreparedStatement statement = null;
        try {
            // Ket noi toi database : jdbc : mysql://localhost:3306/ ten ; tai khoan + mat khau
            //muc dich lay danh sach tat ca sinh vien :
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student04?serverTimezone=UTC", "root", "");

            //Query
            String sql = "select * from student WHERE rollno = ?";
            // Creates a Statement object for sending SQL statements to the database
            statement = connection.prepareCall(sql);
            
            statement.setString(1, rollno);
            
            
            // A table of data representing a database result set, which is usually generated by executing a statement that queries the database.
            ResultSet resultSet = statement.executeQuery(sql);
            // reselutSet.next() == moves the pointer of the current (ResultSet) object to the next row, from the current position
            while (resultSet.next()) {
                
                // Create a student getting from resultSet ( database ) 
                Student std = new Student(resultSet.getInt("id"),
                        resultSet.getString("fullname"),
                        resultSet.getString("gender"),
                        resultSet.getString("rollno"),
                        resultSet.getString("email"),
                        resultSet.getString("address"));
                        

                studentlist.add(std);
            }

        } catch (SQLException ex) {
            Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // neu connection da chay , dong connection.
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        return studentlist;
    }
   
   public static void insert(Student std) {
        Connection connection = null;

        // for parametirized query
        PreparedStatement statement = null;
        try {
            // Ket noi toi database : jdbc : mysql://localhost:3306/ ten ; tai khoan + mat khau
            //muc dich lay danh sach tat ca sinh vien :
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student04?serverTimezone=UTC", "root", "");

            //Query
            String sql = "insert into student(fullname , gender , rollno , email , address) values(?,?,?,?,?)";
            //
            statement = connection.prepareCall(sql);

            statement.setString(1, std.getFullname());
            statement.setString(2, std.getGender());
            statement.setString(3, std.getRollno());
            statement.setString(4, std.getEmail());
            statement.setString(5, std.getAddress());

            statement.execute();

        } catch (SQLException ex) {
            Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // neu connection da chay , dong connection.
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Studentmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}
/*
 * 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 newprojectjavaswing;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author abc
 */
public class Sanphammodify {
    public static List<SanPham> findAll()
    {
        List<SanPham> sanphamlist = new ArrayList<>();
        Connection connection = null;
        
        Statement statement = null;
        
        
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sanpham?serverTimezone=UTC","root","");
            
            String sql = "select * from sanpham";
            
            statement = connection.createStatement();
            
            ResultSet resultset = statement.executeQuery(sql);
            
            while(resultset.next())
            {
                SanPham sp = new SanPham(resultset.getString("id"),resultset.getString("idDM"),resultset.getString("nameSP"),resultset.getFloat("price"),resultset.getString("inputDate"),resultset.getString("outputDate"),resultset.getString("expiryDate"),resultset.getString("description"));
                
                
                sanphamlist.add(sp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            if(connection != null)
            {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(statement != null)
            {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        return sanphamlist;
    }
    
    public static void insert(SanPham sp) {
        Connection connection = null;

        // for parametirized query
        PreparedStatement statement = null;
        try {
            // Ket noi toi database : jdbc : mysql://localhost:3306/ ten ; tai khoan + mat khau
            //muc dich lay danh sach tat ca sinh vien :
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sanpham?serverTimezone=UTC", "root", "");

            //Query
            String sql = "insert into sanpham(id , idDM , nameSP , price ,inputDate ,outputDate, expiryDate ,description) values(?,?,?,?,?)";
            //
            statement = connection.prepareCall(sql);

            statement.setString(1, sp.getId());
            statement.setString(2, sp.getMaDanhMuc());
            statement.setString(3, sp.getNamesp());
            statement.setFloat(4, sp.getPrice());
            statement.setString(5, sp.getInputdate());
            statement.setString(6, sp.getOutputdate());
            statement.setString(7, sp.getHsd());
            statement.setString(8, sp.getDescription());

            statement.execute();

        } catch (SQLException ex) {
            Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // neu connection da chay , dong connection.
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
     public static void update(SanPham sp) {
        Connection connection = null;

        // for parametirized query
        PreparedStatement statement = null;
        try {
            // Ket noi toi database : jdbc : mysql://localhost:3306/ ten ; tai khoan + mat khau
            //muc dich lay danh sach tat ca sinh vien :
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sanpham?serverTimezone=UTC", "root", "");

            //Query
            String sql = "update sanpham set idDM = ? , namesp = ? , price = ? ,inputDate = ? , outputDate = ? ,expiryDate = ? , description = ? where id = ?";
            
            //
            statement = connection.prepareCall(sql);
            
            statement.setString(1, sp.getMaDanhMuc());
            statement.setString(2, sp.getNamesp());
            statement.setFloat(3, sp.getPrice());
            statement.setString(4, sp.getInputdate());
            statement.setString(5, sp.getOutputdate());
            statement.setString(6, sp.getHsd());
            statement.setString(7, sp.getDescription());
            statement.setString(8, sp.getId());
            
            
            statement.execute();
            

        } catch (SQLException ex) {
            Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // neu connection da chay , dong connection.
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
     
     public static void delete(String id) {
        Connection connection = null;

        // for parametirized query
        PreparedStatement statement = null;
        try {
            // Ket noi toi database : jdbc : mysql://localhost:3306/ ten ; tai khoan + mat khau
            //muc dich lay danh sach tat ca sinh vien :
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sanpham?serverTimezone=UTC", "root", "");

            //Query
            String sql = "delete from sanpham where id = ?";
            
            //
            statement = connection.prepareCall(sql);
            
            statement.setString(1, id);
            
            statement.execute();
            

        } catch (SQLException ex) {
            Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // neu connection da chay , dong connection.
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
     
     public static List<SanPham> findById( String id ) {
        List<SanPham> sanphamlist = new ArrayList<>();

        Connection connection = null;

        // an instruction that explains what should happen
        PreparedStatement statement = null;
        try {
            // Ket noi toi database : jdbc : mysql://localhost:3306/ ten ; tai khoan + mat khau
            //muc dich lay danh sach tat ca sinh vien :
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sanpham?serverTimezone=UTC", "root", "");

            //Query
            String sql = "select * from sanpham WHERE id = ?";
            // Creates a Statement object for sending SQL statements to the database
            statement = connection.prepareCall(sql);
            
            statement.setString(1, id);
            
            
            // A table of data representing a database result set, which is usually generated by executing a statement that queries the database.
            ResultSet resultSet = statement.executeQuery(sql);
            // reselutSet.next() == moves the pointer of the current (ResultSet) object to the next row, from the current position
            while (resultSet.next()) {
                
                // Create a student getting from resultSet ( database ) 
                SanPham sp = new SanPham(resultSet.getString("id"),
                        resultSet.getString("idDM"),
                        resultSet.getString("namesp"),
                        resultSet.getFloat("price"),
                        resultSet.getString("inputDate"),
                        resultSet.getString("outputDate"),
                        resultSet.getString("expiryDate"),
                        resultSet.getString("description"));
                        

                sanphamlist.add(sp);
            }

        } catch (SQLException ex) {
            Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // neu connection da chay , dong connection.
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Sanphammodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        return sanphamlist;
    }
}
/*
 * 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 newprojectjavaswing;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author abc
 */
public class FormDanhMucmodify {
    public static void insert(Danhmucsanpham sp)
    {
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/danhmuc?serverTimezone=UTC", "root" ,"");
            
            String sql = "insert into danhmuc(idDanhMuc,nameDanhMuc) values (?,?)";
            
            statement = connection.prepareCall(sql);
            
            statement.setString(1,sp.getIdDM());
            statement.setString(2,sp.getNameDM());
            
            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(FormDanhMuc.class.getName()).log(Level.SEVERE, null, ex);
        } finally
        {
            if (statement != null)
            {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(FormDanhMuc.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(connection != null)
            {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(FormDanhMuc.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
    }
    
    public static  List<Danhmucsanpham> findAll() {
        List<Danhmucsanpham> listdm = new ArrayList<>();

        Connection connection = null;

        // an instruction that explains what should happen
        Statement statement = null;
        try {
            // Ket noi toi database : jdbc : mysql://localhost:3306/ ten ; tai khoan + mat khau
            //muc dich lay danh sach tat ca sinh vien :
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/danhmuc?serverTimezone=UTC", "root", "");

            //Query
            String sql = "select * from danhmuc";
            // Creates a Statement object for sending SQL statements to the database
            statement = connection.createStatement();
            // A table of data representing a database result set, which is usually generated by executing a statement that queries the database.
            ResultSet resultSet = statement.executeQuery(sql);
            // reselutSet.next() == moves the pointer of the current (ResultSet) object to the next row, from the current position
            while (resultSet.next()) {
                
                // Create a student getting from resultSet ( database ) 
                Danhmucsanpham sp = new Danhmucsanpham(resultSet.getInt("rollNo"),resultSet.getString("idDanhMuc"), resultSet.getString("nameDanhMuc"));

                listdm.add(sp);
            }

        } catch (SQLException ex) {
            Logger.getLogger(FormDanhMucmodify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(FormDanhMucmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // neu connection da chay , dong connection.
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(FormDanhMucmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        return listdm;
    }
    
    public static void delete(int rollNo) {
        Connection connection = null;

        // for parametirized query
        PreparedStatement statement = null;
        try {
            // Ket noi toi database : jdbc : mysql://localhost:3306/ ten ; tai khoan + mat khau
            //muc dich lay danh sach tat ca sinh vien :
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/danhmuc?serverTimezone=UTC", "root", "");

            //Query
            String sql = "delete from danhmuc where rollNo = ?";
            
            //
            statement = connection.prepareCall(sql);
            
            statement.setInt(1, rollNo);
            
            statement.execute();
            

        } catch (SQLException ex) {
            Logger.getLogger(FormDanhMucmodify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(FormDanhMucmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // neu connection da chay , dong connection.
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(FormDanhMucmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
     public static void update(Danhmucsanpham sp) {
        Connection connection = null;

        // for parametirized query
        PreparedStatement statement = null;
        try {
            // Ket noi toi database : jdbc : mysql://localhost:3306/ ten ; tai khoan + mat khau
            //muc dich lay danh sach tat ca sinh vien :
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/danhmuc?serverTimezone=UTC", "root", "");

            //Query
            String sql = "update danhmuc set idDanhmuc = ? ,nameDanhmuc = ? where rollNo = ?";
            
            //
            statement = connection.prepareCall(sql);
            
            statement.setString(1, sp.getIdDM());
            statement.setString(2, sp.getNameDM());
            statement.setInt(3, sp.getRollNo());
            
            statement.execute();
            

        } catch (SQLException ex) {
            Logger.getLogger(FormDanhMucmodify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(FormDanhMucmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // neu connection da chay , dong connection.
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(FormDanhMucmodify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    
    
    
}


avatar
Minh Nghia [T1907A]
2020-04-09 07:26:37



/*
 * 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 QuanLiBanHang.Console;

import java.util.Scanner;

/**
 *
 * @author Administrator
 */
public class Product {
    String maSP, maDM, tenSP;
    int id, price ;
    int ngayNhap, ngayBan ,hsd;
    String moTaSP;

    public Product() {
    }

    public Product(String maSP, String maDM, String tenSP, int id, int price, int ngayNhap, int ngayBan, int hsd, String moTaSP) {
        this.maSP = maSP;
        this.maDM = maDM;
        this.tenSP = tenSP;
        this.id = id;
        this.price = price;
        this.ngayNhap = ngayNhap;
        this.ngayBan = ngayBan;
        this.hsd = hsd;
        this.moTaSP = moTaSP;
    }

    public Product(String maSP, String maDM, String tenSP, int price, int ngayNhap, int ngayBan, int hsd, String moTaSP) {
        this.maSP = maSP;
        this.maDM = maDM;
        this.tenSP = tenSP;
        this.price = price;
        this.ngayNhap = ngayNhap;
        this.ngayBan = ngayBan;
        this.hsd = hsd;
        this.moTaSP = moTaSP;
    }
    
    public String getMaSP() {
        return maSP;
    }

    public void setMaSP(String maSP) {
        this.maSP = maSP;
    }

    public String getMaDM() {
        return maDM;
    }

    public void setMaDM(String maDM) {
        this.maDM = maDM;
    }

    public String getTenSP() {
        return tenSP;
    }

    public void setTenSP(String tenSP) {
        this.tenSP = tenSP;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getNgayNhap() {
        return ngayNhap;
    }

    public void setNgayNhap(int ngayNhap) {
        this.ngayNhap = ngayNhap;
    }

    public int getNgayBan() {
        return ngayBan;
    }

    public void setNgayBan(int ngayBan) {
        this.ngayBan = ngayBan;
    }

    public int getHsd() {
        return hsd;
    }

    public void setHsd(int hsd) {
        this.hsd = hsd;
    }

    public String getMoTaSP() {
        return moTaSP;
    }

    public void setMoTaSP(String moTaSP) {
        this.moTaSP = moTaSP;
    }

    
    
    public void input(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap id:");
        id = Integer.parseInt(scan.nextLine());
        System.out.println("Nhap ma san pham :");
        maSP = scan.nextLine();
        System.out.println("Nhap ma danh muc :");
        maDM = scan.nextLine();
        System.out.println("Nhap ten san pham :");
        tenSP = scan.nextLine();
        System.out.println("Nhap gia ban :");
        price = Integer.parseInt(scan.nextLine());
        System.out.println("Nhap ngay nhap :");
        ngayNhap = Integer.parseInt(scan.nextLine());
        System.out.println("Nhap ngay ban :");
        ngayBan = Integer.parseInt(scan.nextLine());
        System.out.println("Nhap han su dung :");
        hsd = Integer.parseInt(scan.nextLine());
        System.out.println("Mo ta san pham");
        moTaSP = scan.nextLine();
    }
    public void display(){
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "product{" + "maSP=" + maSP + ", maDM=" + maDM + ", tenSP=" + tenSP + ", price=" + price + ", ngayNhap=" + ngayNhap + ", ngayBan=" + ngayBan + ", hsd=" + hsd + ", moTaSP=" + moTaSP + '}';
    }
    
}



/*
 * 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 QuanLiBanHang.Console;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Administrator
 */
public class ProductFrame extends javax.swing.JFrame {

    List<Product> pList = new ArrayList<>();
    List<productPortfolio> ppList = new ArrayList<>();
    DefaultTableModel tableModel;
    ProductModify productModify;

    /**
     * Creates new form ProductFrame
     */
    public ProductFrame() {
        initComponents();
        tableModel = (DefaultTableModel) tblProduct.getModel();
        showAll();
    }

    private void showAll() {

    }

    /**
     * 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">                          
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtMaDanhMuc = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtTenDanhMuc = new javax.swing.JTextField();
        btnUpdatePortfolio = new javax.swing.JButton();
        btnEditPortfolio = new javax.swing.JButton();
        btnDeletePortfolio = new javax.swing.JButton();
        btnSearchPortfolio = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblPortfolio = new javax.swing.JTable();
        jLabel8 = new javax.swing.JLabel();
        txtRollno = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txtMaSp = new javax.swing.JTextField();
        txtMaDm = new javax.swing.JTextField();
        txtTenSp = new javax.swing.JTextField();
        txtGia = new javax.swing.JTextField();
        txtNgayNhap = new javax.swing.JTextField();
        txtNgayBan = new javax.swing.JTextField();
        txtHanSd = new javax.swing.JTextField();
        txtMoTaSp = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        NgayBan = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblProduct = new javax.swing.JTable();
        btnUpdate = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnTimSp = new javax.swing.JButton();
        btnTonkho = new javax.swing.JButton();
        btnDaban = new javax.swing.JButton();
        btnTrongngay = new javax.swing.JButton();
        btnTrongtuan = new javax.swing.JButton();
        btnTrongthang = new javax.swing.JButton();
        btnConhan = new javax.swing.JButton();
        btnHethan = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Product-Frame");

        jLabel1.setText("MaDanhMuc");

        jLabel2.setText("TenDanhMuc");

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

        btnEditPortfolio.setText("Edit");
        btnEditPortfolio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditPortfolioActionPerformed(evt);
            }
        });

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

        btnSearchPortfolio.setText("Search");
        btnSearchPortfolio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchPortfolioActionPerformed(evt);
            }
        });

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

            },
            new String [] {
                "No", "MaDanhMuc", "TenDanhMuc"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblPortfolio);

        jLabel8.setText("RollNo :");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(75, 75, 75)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(btnUpdatePortfolio)
                        .addGap(57, 57, 57)
                        .addComponent(btnEditPortfolio, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(49, 49, 49)
                        .addComponent(btnDeletePortfolio)
                        .addGap(50, 50, 50)
                        .addComponent(btnSearchPortfolio))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTenDanhMuc, javax.swing.GroupLayout.PREFERRED_SIZE, 427, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtRollno, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtMaDanhMuc, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)))
                .addContainerGap(232, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(79, 79, 79)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8)
                    .addComponent(txtRollno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(46, 46, 46)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMaDanhMuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(52, 52, 52)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtTenDanhMuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(43, 43, 43)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnUpdatePortfolio)
                    .addComponent(btnEditPortfolio)
                    .addComponent(btnDeletePortfolio)
                    .addComponent(btnSearchPortfolio))
                .addGap(66, 66, 66)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(432, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("ProductPortfolio", jPanel2);

        jLabel3.setText("MaSanPham");

        jLabel4.setText("MaDanhMuc");

        jLabel5.setText("TenSanPham");

        jLabel6.setText("NgayNhap");

        jLabel7.setText("Gia");

        NgayBan.setText("NgayBan");

        jLabel9.setText("HanSuDung");

        jLabel11.setText("MoTaSanPham");

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

            },
            new String [] {
                "No", "MaSp", "MaDm", "TenSp", "Gia", "NgayNhap", "NgayBan", "HanSd", "MoTaSP"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, true, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tblProduct);

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

        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

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

        btnTimSp.setText("Tim San Pham");
        btnTimSp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimSpActionPerformed(evt);
            }
        });

        btnTonkho.setText("San Pham Ton Kho");
        btnTonkho.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTonkhoActionPerformed(evt);
            }
        });

        btnDaban.setText("San Pham Da Ban");
        btnDaban.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDabanActionPerformed(evt);
            }
        });

        btnTrongngay.setText("Thong ke sp ban trong ngay");
        btnTrongngay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTrongngayActionPerformed(evt);
            }
        });

        btnTrongtuan.setText("Thong ke sp ban trong tuan");
        btnTrongtuan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTrongtuanActionPerformed(evt);
            }
        });

        btnTrongthang.setText("Thong ke sp ban trong thang");
        btnTrongthang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTrongthangActionPerformed(evt);
            }
        });

        btnConhan.setText("San pham con han");
        btnConhan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConhanActionPerformed(evt);
            }
        });

        btnHethan.setText("San pham het han");
        btnHethan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHethanActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 813, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel3)
                            .addComponent(NgayBan)
                            .addComponent(jLabel5)
                            .addComponent(jLabel7)
                            .addComponent(jLabel4)
                            .addComponent(jLabel6)
                            .addComponent(jLabel11))
                        .addGap(43, 43, 43)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtHanSd, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 338, Short.MAX_VALUE)
                            .addComponent(txtNgayBan, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNgayNhap, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtGia, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtTenSp, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtMaDm, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtMaSp, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtMoTaSp)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                                .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(51, 51, 51)
                                .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(120, 120, 120)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnConhan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnTrongngay, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnTrongtuan, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnDaban, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnTonkho, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnTimSp, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnTrongthang, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnHethan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(27, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtMaSp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnTimSp))
                .addGap(43, 43, 43)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMaDm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(btnTonkho))
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtTenSp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addComponent(btnDaban)))
                .addGap(30, 30, 30)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(btnTrongngay))
                .addGap(34, 34, 34)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtNgayNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnTrongtuan)))
                .addGap(34, 34, 34)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(NgayBan)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtNgayBan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnTrongthang)))
                .addGap(26, 26, 26)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel9)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtHanSd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnConhan)))
                .addGap(58, 58, 58)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMoTaSp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnHethan)
                    .addComponent(jLabel11))
                .addGap(45, 45, 45)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnUpdate)
                    .addComponent(btnEdit)
                    .addComponent(btnDelete))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 134, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(108, 108, 108))
        );

        jTabbedPane1.addTab("Product", jPanel3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );

        pack();
    }// </editor-fold>                        

    private void btnUpdatePortfolioActionPerformed(java.awt.event.ActionEvent evt) {                                                   
        // TODO add your handling code here:
        int id = Integer.parseInt(txtRollno.getText());
        String maDM = txtMaDanhMuc.getText();
        String tenDM = txtTenDanhMuc.getText();
        productPortfolio pp = new productPortfolio(id, maDM, tenDM);
        pModify.update(pp);
        showAll();
    }                                                  

    private void btnEditPortfolioActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        // TODO add your handling code here:
        int id = Integer.parseInt(txtRollno.getText());
        String maDM = txtMaDanhMuc.getText();
        String tenDM = txtTenDanhMuc.getText();
        productPortfolio pp = new productPortfolio(id, maDM, tenDM);
        int selectedIndex = tblPortfolio.getSelectedRow();
        if (selectedIndex >= 0) {
            productPortfolio pro = ppList.get(selectedIndex);
            pp.setId(pro.getId());
        }
        pModify.edit(pp);
        showAll();
    }                                                

    private void btnDeletePortfolioActionPerformed(java.awt.event.ActionEvent evt) {                                                   
        // TODO add your handling code here:
        int selectedIndex = tblPortfolio.getSelectedRow();
        if (selectedIndex >= 0) {
            productPortfolio pp = ppList.get(selectedIndex);
            int option = JOptionPane.showConfirmDialog(this, "Do you want to delete this item");
            System.out.println("option :" + option);

            if (option == 0) {
                pModify.delete(pp.getId());
                showAll();
            }
        }
    }                                                  
//--------------------------------------------------------------------------------


    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        String maSP = txtMaSp.getText();
        String maDM = txtMaDm.getText();
        String TenSP = txtTenSp.getText();
        int price = Integer.parseInt(txtGia.getText());
        int ngayNhap = Integer.parseInt(txtNgayNhap.getText());
        int ngayBan = Integer.parseInt(txtNgayBan.getText());
        int hanSd = Integer.parseInt(txtHanSd.getText());
        String moTaSP = txtMoTaSp.getText();

        Product p = new Product();
        p.setMaSP(maSP);
        p.setMaDM(maDM);
        p.setTenSP(TenSP);
        p.setPrice(price);
        p.setNgayNhap(ngayNhap);
        p.setNgayBan(ngayBan);
        p.setHsd(hanSd);
        p.setMoTaSP(moTaSP);
        ProductModify.update(p);
        p.display();
        showAll();
    }                                         

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        String maSP = txtMaSp.getText();
        String maDM = txtMaDm.getText();
        String TenSP = txtTenSp.getText();
        int price = Integer.parseInt(txtGia.getText());
        int ngayNhap = Integer.parseInt(txtNgayNhap.getText());
        int ngayBan = Integer.parseInt(txtNgayBan.getText());
        int hanSd = Integer.parseInt(txtHanSd.getText());
        String moTaSP = txtMoTaSp.getText();

        Product p = new Product(maSP, maDM, TenSP, price, ngayNhap, ngayBan, hanSd, moTaSP);
        int selectedIndex = tblProduct.getSelectedRow();
        if (selectedIndex >= 0) {
            Product product = pList.get(selectedIndex);
            p.setId(p.getId());
        }
        ProductModify.edit(p);
        showAll();
    }                                       

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        int selectIndex = tblProduct.getRowCount();
        if (selectIndex >= 0) {
            Product p = pList.get(selectIndex);

            int option = JOptionPane.showConfirmDialog(this, "Do you want to delete this item");
            System.out.println("option : " + option);

            if (option == 0) {
                productModify.delete(p.getId());

                showAll();
            }
        }
    }                                         

    private void btnTimSpActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        String input = JOptionPane.showInputDialog(this, "Enter full name to search");
        if (input != null && input.length() > 0) {
            pList = ProductModify.findByTenSp(input);
            tableModel.setRowCount(0);

            pList.forEach((product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getMaSP(), product.getMaDM(), product.getTenSP(),
                    product.getPrice(), product.getNgayNhap(), product.getNgayBan(),
                    product.getMoTaSP()});
            });
        } else {
            showAll();
        }
    }                                        

    private void btnTonkhoActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        pList = ProductModify.tonkho();
        tableModel.setRowCount(0);

        pList.forEach((product) -> {
            tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                product.getMaSP(), product.getMaDM(), product.getTenSP(),
                product.getPrice(), product.getNgayNhap(), product.getNgayBan(),
                product.getMoTaSP()});
        });
    }                                         

    private void btnDabanActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        pList = ProductModify.trongngay();
        tableModel.setRowCount(0);

        pList.forEach((product) -> {
            tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                product.getMaSP(), product.getMaDM(), product.getTenSP(),
                product.getPrice(), product.getNgayNhap(), product.getNgayBan(),
                product.getMoTaSP()});
        });
    }                                        

    private void btnTrongngayActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
        pList = ProductModify.trongngay();
        tableModel.setRowCount(0);

        pList.forEach((product) -> {
            tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                product.getMaSP(), product.getMaDM(), product.getTenSP(),
                product.getPrice(), product.getNgayNhap(), product.getNgayBan(),
                product.getMoTaSP()});
        });
    }                                            

    private void btnTrongtuanActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
        pList = ProductModify.trongtuan();
        tableModel.setRowCount(0);

        pList.forEach((product) -> {
            tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                product.getMaSP(), product.getMaDM(), product.getTenSP(),
                product.getPrice(), product.getNgayNhap(), product.getNgayBan(),
                product.getMoTaSP()});
        });
    }                                            

    private void btnTrongthangActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
        pList = ProductModify.trongthang();
        tableModel.setRowCount(0);

        pList.forEach((product) -> {
            tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                product.getMaSP(), product.getMaDM(), product.getTenSP(),
                product.getPrice(), product.getNgayNhap(), product.getNgayBan(),
                product.getMoTaSP()});
        });
    }                                             

    private void btnConhanActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        pList = ProductModify.conhan();
        tableModel.setRowCount(0);

        pList.forEach((product) -> {
            tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                product.getMaSP(), product.getMaDM(), product.getTenSP(),
                product.getPrice(), product.getNgayNhap(), product.getNgayBan(),
                product.getMoTaSP()});
        });
    }                                         

    private void btnHethanActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        pList = ProductModify.hethan();
        tableModel.setRowCount(0);

        pList.forEach((product) -> {
            tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                product.getMaSP(), product.getMaDM(), product.getTenSP(),
                product.getPrice(), product.getNgayNhap(), product.getNgayBan(),
                product.getMoTaSP()});
        });
    }                                         

    private void btnSearchPortfolioActionPerformed(java.awt.event.ActionEvent evt) {                                                   
        // TODO add your handling code here:
        String input = JOptionPane.showInputDialog(this, "Enter ten san pham to search");
        if (input != null && input.length() > 0) {
            ppList = pModify.search(input);
            tableModel.setRowCount(0);
            pList.forEach((product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1, product.getId(),
                    product.getMaDM(), product.getMaSP()});
            });
        } else {
            showAll();
        }
    }                                                  

    /**
     * @param args the command line argumentss
     */
    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(ProductFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ProductFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ProductFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ProductFrame.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 ProductFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel NgayBan;
    private javax.swing.JButton btnConhan;
    private javax.swing.JButton btnDaban;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnDeletePortfolio;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnEditPortfolio;
    private javax.swing.JButton btnHethan;
    private javax.swing.JButton btnSearchPortfolio;
    private javax.swing.JButton btnTimSp;
    private javax.swing.JButton btnTonkho;
    private javax.swing.JButton btnTrongngay;
    private javax.swing.JButton btnTrongthang;
    private javax.swing.JButton btnTrongtuan;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JButton btnUpdatePortfolio;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    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.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable tblPortfolio;
    private javax.swing.JTable tblProduct;
    private javax.swing.JTextField txtGia;
    private javax.swing.JTextField txtHanSd;
    private javax.swing.JTextField txtMaDanhMuc;
    private javax.swing.JTextField txtMaDm;
    private javax.swing.JTextField txtMaSp;
    private javax.swing.JTextField txtMoTaSp;
    private javax.swing.JTextField txtNgayBan;
    private javax.swing.JTextField txtNgayNhap;
    private javax.swing.JTextField txtRollno;
    private javax.swing.JTextField txtTenDanhMuc;
    private javax.swing.JTextField txtTenSp;
    // End of variables declaration                   

}



/*
 * 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 QuanLiBanHang.Console;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class ProductModify {

    public static List<Product> ShowAll() {
        List<Product> pList = new ArrayList<>();

        Connection connection = null;
        Statement statement = null;

        try {
            //lay tat ca danh sach sinh vien
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");
            String sql = "select * from sanpham";
            statement = connection.createStatement();

            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                Product product = new Product(resultSet.getString("maSp"),
                        resultSet.getString("maDm"),
                        resultSet.getString("tenSp"),
                        resultSet.getInt("price"),
                        resultSet.getInt("ngayNhap"),
                        resultSet.getInt("ngayBan"),
                        resultSet.getInt("hsd"),
                        resultSet.getString("moTaSP"));
                pList.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
        } //query
        finally {
            if (statement != null) {

                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                }

                if (connection != null) {

                    try {
                        connection.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }
            }
            //ket thuc.

        }
        return pList;
    }

    public static void update(Product p) {
        Connection conn = null;
        PreparedStatement statement = null;
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");

            String sql = "insert into sanpham(id,MaSp, MaDm,TenSp, Gia, NgayNhap, NgayBan, HanSd, MoTaSp ) value(?,?,?,?,?,?,?,?,)";
            statement = conn.prepareCall(sql);

            statement.setInt(1, p.getId());
            statement.setString(2, p.getMaSP());
            statement.setString(3, p.getMaDM());
            statement.setString(4, p.getTenSP());
            statement.setInt(5, p.getPrice());
            statement.setInt(6, p.getNgayBan());
            statement.setInt(7, p.getNgayNhap());
            statement.setInt(8, p.getHsd());
            statement.setString(9, p.getMoTaSP());

            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {

                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                }

                if (conn != null) {

                    try {
                        conn.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }
            }
            //ket thuc.

        }

    }

    public static void edit(Product p) {
        Connection conn = null;
        PreparedStatement statement = null;
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");

            String sql = "update sanpham set MaSp=?,MaDm=?,TenSp=?,gia=?,NgayNhap=?, NgayBan= ?, HanSd=?, MoTaSp=? where = id=?";
            statement = conn.prepareCall(sql);

            statement.setInt(1, p.getId());
            statement.setString(2, p.getMaSP());
            statement.setString(3, p.getMaDM());
            statement.setString(4, p.getTenSP());
            statement.setInt(5, p.getPrice());
            statement.setInt(6, p.getNgayBan());
            statement.setInt(7, p.getNgayNhap());
            statement.setInt(8, p.getHsd());
            statement.setString(9, p.getMoTaSP());

            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {

                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                }

                if (conn != null) {

                    try {
                        conn.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }
            }
            //ket thuc.

        }

    }

    public static void delete(int id) {
        Connection conn = null;
        PreparedStatement statement = null;
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");

            String sql = "delete from sanpham where id = ?";
            statement = conn.prepareCall(sql);

            statement.setInt(1, id);
            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {

                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                }

                if (conn != null) {

                    try {
                        conn.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }
            }
            //ket thuc.

        }

    }

    public static List<Product> findByTenSp(String tenSp) {
        List<Product> pList = new ArrayList<>();
        Connection conn = null;
        PreparedStatement statement = null;
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");

            String sql = "select * from sanpham where TenSp like ?";
            statement = conn.prepareCall(sql);

            statement.setString(1, "%" + tenSp + "%");
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                Product product = new Product(
                        resultSet.getString("maSp"),
                        resultSet.getString("maDm"),
                        resultSet.getString("tenSp"),
                        resultSet.getInt("price"),
                        resultSet.getInt("ngayNhap"),
                        resultSet.getInt("ngayBan"),
                        resultSet.getInt("hsd"),
                        resultSet.getString("moTaSP"));
                pList.add(product);
            }

        } catch (SQLException ex) {
            Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {

                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                }

                if (conn != null) {

                    try {
                        conn.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }
            }
            //ket thuc.

        }
        return pList;
    }

    public static List<Product> tonkho() {
        List<Product> pList = new ArrayList<>();
        Connection conn = null;
        PreparedStatement statement = null;
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");

            String sql = "select * from sanpham where (NgayBan ='' or NgayBan = '' or NgayBan = null)";
            statement = conn.prepareCall(sql);

            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                Product product = new Product(resultSet.getString("maSp"),
                        resultSet.getString("maDm"),
                        resultSet.getString("tenSp"),
                        resultSet.getInt("price"),
                        resultSet.getInt("ngayNhap"),
                        resultSet.getInt("ngayBan"),
                        resultSet.getInt("hsd"),
                        resultSet.getString("moTaSP"));
                pList.add(product);
            }

        } catch (SQLException ex) {
            Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {

                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                }

                if (conn != null) {

                    try {
                        conn.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }
            }
            //ket thuc.

        }
        return pList;
    }

    public static List<Product> trongngay() {
        List<Product> pList = new ArrayList<>();
        Connection conn = null;
        PreparedStatement statement = null;
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");

            String sql = "select * from sanpham where (CURDATE() = NgayBan)";
            statement = conn.prepareCall(sql);

            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                Product product = new Product(resultSet.getString("maSp"),
                        resultSet.getString("maDm"),
                        resultSet.getString("tenSp"),
                        resultSet.getInt("price"),
                        resultSet.getInt("ngayNhap"),
                        resultSet.getInt("ngayBan"),
                        resultSet.getInt("hsd"),
                        resultSet.getString("moTaSP"));
                pList.add(product);
            }

        } catch (SQLException ex) {
            Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {

                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                }

                if (conn != null) {

                    try {
                        conn.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }
            }
            //ket thuc.

        }
        return pList;
    }

    public static List<Product> trongtuan() {
        List<Product> pList = new ArrayList<>();
        Connection conn = null;
        PreparedStatement statement = null;
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");

            String sql = "select * from sanpham where (YEARWEEK(NgayNhap) = YEARDATE(NgayBan))";
            statement = conn.prepareCall(sql);

            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                Product product = new Product(resultSet.getString("maSp"),
                        resultSet.getString("maDm"),
                        resultSet.getString("tenSp"),
                        resultSet.getInt("price"),
                        resultSet.getInt("ngayNhap"),
                        resultSet.getInt("ngayBan"),
                        resultSet.getInt("hsd"),
                        resultSet.getString("moTaSP"));
                pList.add(product);
            }

        } catch (SQLException ex) {
            Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {

                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                }

                if (conn != null) {

                    try {
                        conn.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }
            }
            //ket thuc.

        }
        return pList;
    }

    public static List<Product> trongthang() {
        List<Product> pList = new ArrayList<>();
        Connection conn = null;
        PreparedStatement statement = null;
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");

            String sql = "select * from sanpham where (MONTH(NgayNhap) = MONTH(NgayBan))";
            statement = conn.prepareCall(sql);

            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                Product product = new Product(resultSet.getString("maSp"),
                        resultSet.getString("maDm"),
                        resultSet.getString("tenSp"),
                        resultSet.getInt("price"),
                        resultSet.getInt("ngayNhap"),
                        resultSet.getInt("ngayBan"),
                        resultSet.getInt("hsd"),
                        resultSet.getString("moTaSP"));
                pList.add(product);
            }

        } catch (SQLException ex) {
            Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {

                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                }

                if (conn != null) {

                    try {
                        conn.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }
            }
            //ket thuc.

        }
        return pList;
    }

    public static List<Product> conhan() {
        List<Product> pList = new ArrayList<>();
        Connection conn = null;
        PreparedStatement statement = null;
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");

            String sql = "select * from sanpham where (CURDATE() < HanSd)";
            statement = conn.prepareCall(sql);

            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                Product product = new Product(resultSet.getString("maSp"),
                        resultSet.getString("maDm"),
                        resultSet.getString("tenSp"),
                        resultSet.getInt("price"),
                        resultSet.getInt("ngayNhap"),
                        resultSet.getInt("ngayBan"),
                        resultSet.getInt("hsd"),
                        resultSet.getString("moTaSP"));
                pList.add(product);
            }

        } catch (SQLException ex) {
            Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {

                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                }

                if (conn != null) {

                    try {
                        conn.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }
            }
            //ket thuc.

        }
        return pList;
    }

    public static List<Product> hethan() {
        List<Product> pList = new ArrayList<>();
        Connection conn = null;
        PreparedStatement statement = null;
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");

            String sql = "select * from sanpham where (CURDATE() > HanSd)";
            statement = conn.prepareCall(sql);

            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                Product product = new Product(resultSet.getString("maSp"),
                        resultSet.getString("maDm"),
                        resultSet.getString("tenSp"),
                        resultSet.getInt("price"),
                        resultSet.getInt("ngayNhap"),
                        resultSet.getInt("ngayBan"),
                        resultSet.getInt("hsd"),
                        resultSet.getString("moTaSP"));
                pList.add(product);
            }

        } catch (SQLException ex) {
            Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {

                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                }

                if (conn != null) {

                    try {
                        conn.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }
            }
            //ket thuc.

        }
        return pList;
    }

    public static List<Product> daban() {
        List<Product> pList = new ArrayList<>();
        Connection conn = null;
        PreparedStatement statement = null;
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");

            String sql = "select * from sanpham where (NgayBan > 0)";
            statement = conn.prepareCall(sql);

            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                Product product = new Product(resultSet.getString("maSp"),
                        resultSet.getString("maDm"),
                        resultSet.getString("tenSp"),
                        resultSet.getInt("price"),
                        resultSet.getInt("ngayNhap"),
                        resultSet.getInt("ngayBan"),
                        resultSet.getInt("hsd"),
                        resultSet.getString("moTaSP"));
                pList.add(product);
            }

        } catch (SQLException ex) {
            Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {

                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                }

                if (conn != null) {

                    try {
                        conn.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(ProductModify.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }
            }
            //ket thuc.

        }
        return pList;
    }
}



/*
 * 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 QuanLiBanHang.Console;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class pModify {
    public static List<productPortfolio> showAll(){
        List<productPortfolio> pProList = new ArrayList<>();
        Connection conn = null;
        Statement statement = null;
        
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");
            statement = conn.createStatement();
            String sql = "select * from danhmucsanpham";
            
            ResultSet resultSet = statement.executeQuery(sql);
            
            while(resultSet.next()){
                productPortfolio pp = new productPortfolio(resultSet.getInt("id"),
                resultSet.getString("maDM"), resultSet.getString("TenDM"));
                
                pProList.add(pp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(pModify.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(pModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(statement != null){
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(pModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return pProList;
    }
    public static void update(productPortfolio pp){
        Connection conn = null;
        PreparedStatement ps = null;
        
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");
            String sql = "insert into danhmucsanpham(id,MaDanhMuc,TenDanhMuc)";
            ps = conn.prepareCall(sql);
            
            ps.setInt(1, pp.getId());
            ps.setString(2, pp.getMaDM());
            ps.setString(3, pp.getTenDM());
            
            ps.execute();
        } catch (SQLException ex) {
            Logger.getLogger(pModify.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(pModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(ps != null){
                try {
                    ps.close();
                } catch (SQLException ex) {
                    Logger.getLogger(pModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
    }
    public static void edit(productPortfolio pp){
        Connection conn = null;
        PreparedStatement ps = null;
        
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");
            String sql = "update danhmucsanpham set  MaDanhMuc=?,TenSanPham=? where id = ?";
            ps = conn.prepareCall(sql);
            
            ps.setInt(1, pp.getId());
            ps.setString(2, pp.getMaDM());
            ps.setString(3, pp.getTenDM());
            
            ps.execute();
        } catch (SQLException ex) {
            Logger.getLogger(pModify.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(pModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(ps != null){
                try {
                    ps.close();
                } catch (SQLException ex) {
                    Logger.getLogger(pModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    public static void delete(int id){
        Connection conn = null;
        PreparedStatement ps = null;
        
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");
            String sql = "delete from danhmucsanpham where id = ?";
            ps = conn.prepareCall(sql);
            
            ps.setInt(1, id);
            
            
            ps.execute();
        } catch (SQLException ex) {
            Logger.getLogger(pModify.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(pModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(ps != null){
                try {
                    ps.close();
                } catch (SQLException ex) {
                    Logger.getLogger(pModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    public static List<productPortfolio> search(String tenDM){
        List<productPortfolio> pProList = new ArrayList<>();
        Connection conn = null;
        PreparedStatement ps = null;
        
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");
            String sql = "select * from danhmucsanpham where TenDanhMuc like ?";
            
            ps = conn.prepareCall(sql);
            
            ResultSet resultSet = ps.executeQuery();
            while(resultSet.next()){
                productPortfolio pp = new productPortfolio(resultSet.getInt("id"),
                        resultSet.getString("maDM"), resultSet.getString("tenDM"));
                pProList.add(pp);
            }
           
        } catch (SQLException ex) {
            Logger.getLogger(pModify.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(pModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(ps != null){
                try {
                    ps.close();
                } catch (SQLException ex) {
                    Logger.getLogger(pModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return pProList;
    }
}



/*
 * 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 QuanLiBanHang.Console;

import java.util.Scanner;

/**
 *
 * @author Administrator
 */
public class productPortfolio {
    int id;
    String maDM, tenDM;

    public productPortfolio() {
    }

    public productPortfolio(int id,String maDM, String tenDM) {
        this.id = id;
        this.maDM = maDM;
        this.tenDM = tenDM;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    
    
    public String getMaDM() {
        return maDM;
    }

    public void setMaDM(String maDM) {
        this.maDM = maDM;
    }

    public String getTenDM() {
        return tenDM;
    }

    public void setTenDM(String tenDM) {
        this.tenDM = tenDM;
    }
    public void input(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap ma id :");
        id = Integer.parseInt(scan.nextLine());
        System.out.println("Nhap ma danh muc :");
        maDM = scan.nextLine();
        System.out.println("Nhap ten danh muc :");
        tenDM = scan.nextLine();
        
    }
    public void display(){
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "productPortfolio{"+ "id" + id + "maDM=" + maDM + ", tenDM=" + tenDM + '}';
    }
    
}


avatar
lê văn phương [T1907A]
2020-04-09 02:30:13



// bổ sung phần db 


CREATE TABLE sanpham(
    id_sanpham int(10) PRIMARY KEY NOT NULL,
    id_danhmuc int(10) NOT NULL,
    tensanpham VARCHAR(20) NOT NULL,
    gia float(20) NOT NULL,
    ngaynhap data(10) ,
    ngayban data(10),
    hansudung data(10),
    mota VARCHAR(100) NOT NULL
)
CREATE TABLE danhmuc(
    id_danhMuc int(10) FOREIGN KEY NOT NULL,
    tendanhmuc VARCHAR(50) NOT NULL
)


avatar
lê văn phương [T1907A]
2020-04-08 16:05:25



/*
 * 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 QLHH;
import java.sql.*;
import javax.swing.*;
/**
 *
 * @author Admin
 */
public class CheckLog {
//    public static Connection conn = null;
//    public static String testConnect(){
//        try {
//            conn= SanPhamtModify.getConnection();
//            return "Ket noi co so du lieu thanh cong";
//        } catch (Exception e) {
//            return "Ket noi co so du lieu that bai";
//        }
//    }
//    public static ResultSet log(String use, String pass){
//        
//    }
}



/*
 * 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 QLHH;

/**
 *
 * @author Admin
 */
public class DanhMuc {
    int id_danhmuc;
    String tenDanhMuc;

    public DanhMuc() {
    }

    public DanhMuc(int id_danhmuc, String tenDanhMuc) {
        this.id_danhmuc = id_danhmuc;
        this.tenDanhMuc = tenDanhMuc;
    }

    public int getId_danhmuc() {
        return id_danhmuc;
    }

    public void setId_danhmuc(int id_danhmuc) {
        this.id_danhmuc = id_danhmuc;
    }

    public String getTenDanhMuc() {
        return tenDanhMuc;
    }

    public void setTenDanhMuc(String tenDanhMuc) {
        this.tenDanhMuc = tenDanhMuc;
    }
    
}



    /*
 * 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 QLHH;


import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Admin
 */
public class Form_SanPham extends javax.swing.JFrame {

    List<SanPham> list = new ArrayList<>();
    List<DanhMuc>  list12 = new ArrayList<>();
    DefaultTableModel tableModel;

    /**
     * Creates new form Form_SanPham
     */
    public Form_SanPham() {
        initComponents();
        tableModel = (DefaultTableModel) jTable1.getModel();
        showhanghoa();
    }

    public void showhanghoa() {
        list = SanPhamtModify.findAll();
        list.stream().forEach((list1) -> {
            tableModel.addRow(new Object[]{
                list1.getId_danhmuc(),
                list1.getTenDanhMuc(),
                list1.getId_sanpham(),
                list1.getTenSanPhamString(),
                list1.getGia(),
                list1.getNgayNhap(),
                list1.getNgayban(),
                list1.getNgayHetHan(),
                list1.getMoTa()
            });
        });
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jMenuItem1 = new javax.swing.JMenuItem();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtIDdanhmuc = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        txtdanhmuc = new javax.swing.JTextField();
        txtTensanpham = new javax.swing.JTextField();
        txtIDsanpham = new javax.swing.JTextField();
        txtgiaban = new javax.swing.JTextField();
        txtmota = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        them = new javax.swing.JButton();
        xoa = new javax.swing.JButton();
        timkiem1 = new javax.swing.JButton();
        txtngayban = new javax.swing.JFormattedTextField();
        txtngayhethan = new javax.swing.JFormattedTextField();
        txtngaynhap = new javax.swing.JFormattedTextField();
        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu11 = new javax.swing.JMenu();
        jMenu12 = new javax.swing.JMenu();
        jMenu13 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenuItem9 = new javax.swing.JMenuItem();

        jMenuItem1.setText("jMenuItem1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Thong tin san pham");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Nhap thong tin cho san pham"));

        jLabel1.setText("ID danh muc");

        jLabel2.setText("Ten danh muc");

        jLabel3.setText("ID san pham");

        jLabel4.setText("Ten san pham");

        jLabel5.setText("Gia ban");

        jLabel6.setText("Mo ta");

        jLabel7.setText("Ngay ban");

        jLabel8.setText("Ngay nhap");

        jLabel9.setText("Ngay het han");

        txtIDdanhmuc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIDdanhmucActionPerformed(evt);
            }
        });

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

            },
            new String [] {
                "ID danh muc", "Ten danh muc", "ID san pham", "Ten san pham", "Gia ban", "Ngay nhap", "Ngay ban", "Ngay het han", "mota"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        txtdanhmuc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtdanhmucActionPerformed(evt);
            }
        });

        txtTensanpham.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTensanphamActionPerformed(evt);
            }
        });

        txtIDsanpham.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIDsanphamActionPerformed(evt);
            }
        });

        txtgiaban.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtgiabanActionPerformed(evt);
            }
        });

        txtmota.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtmotaActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Chon chuc nang"));

        them.setText("Them san pham");
        them.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                themActionPerformed(evt);
            }
        });

        xoa.setText("Xoa san pham");
        xoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                xoaActionPerformed(evt);
            }
        });

        timkiem1.setText("Tim kiem san pham");
        timkiem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                timkiem1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(timkiem1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(them, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(xoa, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(22, 22, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(them)
                .addGap(37, 37, 37)
                .addComponent(xoa)
                .addGap(37, 37, 37)
                .addComponent(timkiem1)
                .addContainerGap(45, Short.MAX_VALUE))
        );

        txtngayban.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("yyyy-mm-dd"))));
        txtngayban.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtngaybanActionPerformed(evt);
            }
        });

        txtngayhethan.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("yyyy-mm-dd"))));
        txtngayhethan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtngayhethanActionPerformed(evt);
            }
        });

        try {
            txtngaynhap.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txtngaynhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtngaynhapActionPerformed(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()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(34, 34, 34)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtIDsanpham, javax.swing.GroupLayout.PREFERRED_SIZE, 334, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtIDdanhmuc, javax.swing.GroupLayout.PREFERRED_SIZE, 334, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtTensanpham, javax.swing.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE)
                        .addComponent(txtmota)
                        .addComponent(txtgiaban)
                        .addComponent(txtngayban)
                        .addComponent(txtngayhethan)
                        .addComponent(txtngaynhap, javax.swing.GroupLayout.Alignment.TRAILING))
                    .addComponent(txtdanhmuc, javax.swing.GroupLayout.PREFERRED_SIZE, 334, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(52, 52, 52)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 847, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(9, 9, 9)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtIDdanhmuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtdanhmuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtIDsanpham, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtTensanpham, 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(txtgiaban, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addGap(15, 15, 15)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtngaynhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtngayban, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jPanel2, 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(txtmota, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtngayhethan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(96, 96, 96))
        );

        jMenu11.setText("File");
        jMenuBar2.add(jMenu11);

        jMenu12.setText("Edit");
        jMenuBar2.add(jMenu12);

        jMenu13.setText("Hien thi");

        jMenuItem2.setText("San pham con han su dung");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu13.add(jMenuItem2);

        jMenuItem3.setText("San pham het han su dung");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu13.add(jMenuItem3);

        jMenuItem4.setText("San pham da ban");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu13.add(jMenuItem4);

        jMenuItem5.setText("San pham ton kho");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu13.add(jMenuItem5);

        jMenuItem6.setText("San pham ban trong ngay");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu13.add(jMenuItem6);

        jMenuItem7.setText("san pham ban trong tuan");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu13.add(jMenuItem7);

        jMenuItem8.setText("San pham ban trong thang");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        jMenu13.add(jMenuItem8);

        jMenuItem9.setText("Cac danh muc");
        jMenuItem9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem9ActionPerformed(evt);
            }
        });
        jMenu13.add(jMenuItem9);

        jMenuBar2.add(jMenu13);

        setJMenuBar(jMenuBar2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 489, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0))
        );

        pack();
    }// </editor-fold>                        

    private void xoaActionPerformed(java.awt.event.ActionEvent evt) {                                    
        int selec = jTable1.getSelectedRow();
        if (selec >= 0) {
            SanPham sp = list.get(selec);
            DanhMuc dm = list.get(selec);
            int op = JOptionPane.showConfirmDialog(this, "ban co muon xoa san pham ???");
            if (op == 0) {
                SanPhamtModify.deletesanpham(sp.getId_sanpham());
                SanPhamtModify.deletedanhmuc(dm.getId_danhmuc());
                showhanghoa();
            }
        }

    }                                   

    private void txtmotaActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
    }                                       

    private void txtgiabanActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
    }                                         

    private void txtIDsanphamActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
    }                                            

    private void txtTensanphamActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
    }                                             

    private void txtdanhmucActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                          

    private void txtIDdanhmucActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
    }                                            

    private void themActionPerformed(java.awt.event.ActionEvent evt) {                                     
        int id_danhmuc = Integer.parseInt(txtIDdanhmuc.getText());
        String tendanhuc = txtdanhmuc.getText();
        int id_sanpham = Integer.parseInt(txtIDsanpham.getText());
        String tensanpham = txtTensanpham.getText();
        float gia = Float.parseFloat(txtgiaban.getText());
        String ngaynhap = txtngaynhap.getText();
        String ngayban = txtngayban.getText();
        String hansudung = txtngayhethan.getText();
        String mota = txtmota.getText();

        DanhMuc danhMuc = new DanhMuc(id_danhmuc, tendanhuc);
        SanPhamtModify.insertdanhmuc(danhMuc);

        SanPham sanPham = new SanPham(id_sanpham, gia, tensanpham, ngaynhap, ngayban, hansudung, mota);
        SanPhamtModify.insertsanpham(sanPham, danhMuc);

        showhanghoa();
    }                                    

    private void timkiem1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        String input = JOptionPane.showInputDialog(this, "Nhap ten cua san pham can tim: ");

        if ( input != null && input.length()>0) {
            list = SanPhamtModify.timkiem(input);
            list.stream().forEach((list1) -> {
                tableModel.addRow(new Object[]{
                    list1.getId_danhmuc(),
                    list1.getTenDanhMuc(),
                    list1.getId_sanpham(),
                    list1.getTenSanPhamString(),
                    list1.getGia(),
                    list1.getNgayNhap(),
                    list1.getNgayban(),
                    list1.getNgayHetHan(),
                    list1.getMoTa()
                });
            });
        }else{
            showhanghoa();
        }
    }                                        

    private void txtngaybanActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                          

    private void txtngayhethanActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
    }                                             

    private void txtngaynhapActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        
         list = SanPhamtModify.conhansudung();
        list.stream().forEach((list1) -> {
            tableModel.addRow(new Object[]{
                list1.getId_danhmuc(),
                list1.getTenDanhMuc(),
                list1.getId_sanpham(),
                list1.getTenSanPhamString(),
                list1.getGia(),
                list1.getNgayNhap(),
                list1.getNgayban(),
                list1.getNgayHetHan(),
                list1.getMoTa()
            });
        });
        
        
    }                                          

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        list = SanPhamtModify.daban();
        list.stream().forEach((list1) -> {
            tableModel.addRow(new Object[]{
                list1.getId_danhmuc(),
                list1.getTenDanhMuc(),
                list1.getId_sanpham(),
                list1.getTenSanPhamString(),
                list1.getGia(),
                list1.getNgayNhap(),
                list1.getNgayban(),
                list1.getNgayHetHan(),
                list1.getMoTa()
            });
        });
        
    }                                          

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {                                           
       
        list = SanPhamtModify.tonkho();
        list.stream().forEach((list1) -> {
            tableModel.addRow(new Object[]{
                list1.getId_danhmuc(),
                list1.getTenDanhMuc(),
                list1.getId_sanpham(),
                list1.getTenSanPhamString(),
                list1.getGia(),
                list1.getNgayNhap(),
                list1.getNgayban(),
                list1.getNgayHetHan(),
                list1.getMoTa()
            });
        });
    }                                          

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {                                           
      list = SanPhamtModify.hethansudung();
        list.stream().forEach((list1) -> {
            tableModel.addRow(new Object[]{
                list1.getId_danhmuc(),
                list1.getTenDanhMuc(),
                list1.getId_sanpham(),
                list1.getTenSanPhamString(),
                list1.getGia(),
                list1.getNgayNhap(),
                list1.getNgayban(),
                list1.getNgayHetHan(),
                list1.getMoTa()
            });
        });       
    }                                          

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {                                           
       list = SanPhamtModify.bantrongtuan();
        list.stream().forEach((list1) -> {
            tableModel.addRow(new Object[]{
                list1.getId_danhmuc(),
                list1.getTenDanhMuc(),
                list1.getId_sanpham(),
                list1.getTenSanPhamString(),
                list1.getGia(),
                list1.getNgayNhap(),
                list1.getNgayban(),
                list1.getNgayHetHan(),
                list1.getMoTa()
            });
        });      
    }                                          

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {                                           
       list = SanPhamtModify.bantrongngay();
        list.stream().forEach((list1) -> {
            tableModel.addRow(new Object[]{
                list1.getId_danhmuc(),
                list1.getTenDanhMuc(),
                list1.getId_sanpham(),
                list1.getTenSanPhamString(),
                list1.getGia(),
                list1.getNgayNhap(),
                list1.getNgayban(),
                list1.getNgayHetHan(),
                list1.getMoTa()
            });
        });       
        
    }                                          

    private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {                                           
         list = SanPhamtModify.bantrongthang();
        list.stream().forEach((list1) -> {
            tableModel.addRow(new Object[]{
                list1.getId_danhmuc(),
                list1.getTenDanhMuc(),
                list1.getId_sanpham(),
                list1.getTenSanPhamString(),
                list1.getGia(),
                list1.getNgayNhap(),
                list1.getNgayban(),
                list1.getNgayHetHan(),
                list1.getMoTa()
            });
        });       
    }                                          

    private void jMenuItem9ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        list12 = SanPhamtModify.danhmuc();
        list12.stream().forEach((list1) -> {
            tableModel.addRow(new Object[]{
                list1.getId_danhmuc(),
                list1.getTenDanhMuc(),
            });
        });  
        
        
    }                                          

    /**
     * @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 | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Form_SanPham.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(() -> {
            new Form_SanPham().setVisible(true);
        });
    }

    // Variables declaration - do not modify                     
    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.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu11;
    private javax.swing.JMenu jMenu12;
    private javax.swing.JMenu jMenu13;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton them;
    private javax.swing.JButton timkiem1;
    private javax.swing.JTextField txtIDdanhmuc;
    private javax.swing.JTextField txtIDsanpham;
    private javax.swing.JTextField txtTensanpham;
    private javax.swing.JTextField txtdanhmuc;
    private javax.swing.JTextField txtgiaban;
    private javax.swing.JTextField txtmota;
    private javax.swing.JFormattedTextField txtngayban;
    private javax.swing.JFormattedTextField txtngayhethan;
    private javax.swing.JFormattedTextField txtngaynhap;
    private javax.swing.JButton xoa;
    // End of variables declaration                   
}



/*
 * 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 QLHH;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;

/**
 *
 * @author Admin
 */
public class LogIn extends javax.swing.JFrame {

    /**
     * Creates new form LogIn
     */
    public LogIn() {
        initComponents();
    }

    public static ResultSet rs = null;
    public static PreparedStatement statement = null;

    /**
     * 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton2 = new javax.swing.JButton();
        jTabbedPane4 = new javax.swing.JTabbedPane();
        jTabbedPane5 = new javax.swing.JTabbedPane();
        jPanel4 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txttaopass = new javax.swing.JPasswordField();
        txtnhaplaipass = new javax.swing.JPasswordField();
        txttaotaikhoan = new javax.swing.JTextField();
        txtemail = new javax.swing.JTextField();
        dangki = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txttaikhoan = new javax.swing.JTextField();
        dangnhap = new javax.swing.JButton();
        thoat = new javax.swing.JButton();
        txtpass = new javax.swing.JPasswordField();

        jButton2.setText("jButton2");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("LOG IN / SiGN IN");

        jLabel5.setText("Nhap tai khoan");

        jLabel6.setText("Nhap mat khau");

        jLabel7.setText("Nhap lai mat khau");

        jLabel8.setText("Nhap email");

        dangki.setText("Dang ki");
        dangki.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dangkiActionPerformed(evt);
            }
        });

        jButton3.setText("Thoat");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8)
                    .addComponent(jLabel7)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5))
                .addGap(60, 60, 60)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(dangki)
                        .addGap(46, 46, 46)
                        .addComponent(jButton3))
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtnhaplaipass)
                        .addComponent(txttaopass)
                        .addComponent(txttaotaikhoan)
                        .addComponent(txtemail, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)))
                .addContainerGap(27, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txttaotaikhoan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txttaopass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtnhaplaipass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txtemail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(47, 47, 47)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dangki)
                    .addComponent(jButton3))
                .addContainerGap(131, Short.MAX_VALUE))
        );

        jTabbedPane5.addTab("Nhap thong dang ki", jPanel4);

        jTabbedPane4.addTab("Dang ki", jTabbedPane5);

        jLabel2.setText("Tai khoan");

        jLabel3.setText("Mat khau");

        dangnhap.setText("Dang nhap");
        dangnhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dangnhapActionPerformed(evt);
            }
        });

        thoat.setText("thoat");
        thoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                thoatActionPerformed(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()
                .addGap(43, 43, 43)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(44, 44, 44)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(dangnhap)
                                .addGap(57, 57, 57)
                                .addComponent(thoat, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(txtpass, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txttaikhoan, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(59, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txttaikhoan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(41, 41, 41)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtpass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(56, 56, 56)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dangnhap)
                    .addComponent(thoat))
                .addContainerGap(222, Short.MAX_VALUE))
        );

        jTabbedPane4.addTab("Dang nhap", jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(57, 57, 57)
                .addComponent(jTabbedPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 461, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(67, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(jTabbedPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 458, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(137, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void dangnhapActionPerformed(java.awt.event.ActionEvent evt) {                                         
        if (txtpass.getPassword().length > 0 && txttaikhoan.getText().length() > 0) {
            try {
                Form_SanPham fsp = new Form_SanPham();
                this.setVisible(false);
                fsp.setVisible(true);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e, "Thong bao", 1);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Ban chua nhap thong tin tai khoan hoac mat khau", "Thong bao", 1);
        }

    }                                        

    private void thoatActionPerformed(java.awt.event.ActionEvent evt) {                                      
        if (JOptionPane.showConfirmDialog(rootPane, "Ban co muon thoat chuong trinh nay hay khong ?", "Thong bao", 2) == 0) {
            this.dispose();
        }

    }                                     

    private void dangkiActionPerformed(java.awt.event.ActionEvent evt) {                                       

        if (txttaopass.getPassword().length > 0 && txttaotaikhoan.getText().length() > 0
                && txtemail.getText().length() > 0 && txtnhaplaipass.getPassword().length > 0) {
            if ( txtnhaplaipass.getPassword() == txttaopass.getPassword()) {
                try {
                    Form_SanPham fsp = new Form_SanPham();
                    this.setVisible(false);
                    fsp.setVisible(true);
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, e, "Thong bao", 1);
                }
            }else{
                JOptionPane.showMessageDialog(null, "Ban nhap lai mat khau chua dung", "Thong bao", 1);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Ban chua nhap day du thong tin vao form", "Thong bao", 1);
        }
    }                                      

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        if (JOptionPane.showConfirmDialog(rootPane, "Ban co muon thoat chuong trinh nay hay khong ?", "Thong bao", 2) == 0) {
            this.dispose();
        }
    }                                        

    /**
     * @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(LogIn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LogIn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LogIn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LogIn.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 LogIn().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton dangki;
    private javax.swing.JButton dangnhap;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JTabbedPane jTabbedPane4;
    private javax.swing.JTabbedPane jTabbedPane5;
    private javax.swing.JButton thoat;
    private javax.swing.JTextField txtemail;
    private javax.swing.JPasswordField txtnhaplaipass;
    private javax.swing.JPasswordField txtpass;
    private javax.swing.JTextField txttaikhoan;
    private javax.swing.JPasswordField txttaopass;
    private javax.swing.JTextField txttaotaikhoan;
    // End of variables declaration                   
}



/*
 * 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 QLHH;

/**
 *
 * @author Admin
 */
public class SanPham extends DanhMuc{
   int id_sanpham;
   float gia;
   String tenSanPhamString, ngayNhap,ngayban,ngayHetHan,moTa;

    public SanPham() {
    }

    public SanPham(int id_sanpham, float gia, String tenSanPhamString, String ngayNhap, String ngayban, String ngayHetHan, String moTa, int id_danhmuc, String tenDanhMuc) {
        super(id_danhmuc, tenDanhMuc);
        this.id_sanpham = id_sanpham;
        this.gia = gia;
        this.tenSanPhamString = tenSanPhamString;
        this.ngayNhap = ngayNhap;
        this.ngayban = ngayban;
        this.ngayHetHan = ngayHetHan;
        this.moTa = moTa;
    }

    public SanPham(int id_sanpham, float gia, String tenSanPhamString, String ngayNhap, String ngayban, String ngayHetHan, String moTa) {
        this.id_sanpham = id_sanpham;
        this.gia = gia;
        this.tenSanPhamString = tenSanPhamString;
        this.ngayNhap = ngayNhap;
        this.ngayban = ngayban;
        this.ngayHetHan = ngayHetHan;
        this.moTa = moTa;
    }
    

    public int getId_sanpham() {
        return id_sanpham;
    }

    public void setId_sanpham(int id_sanpham) {
        this.id_sanpham = id_sanpham;
    }

    public float getGia() {
        return gia;
    }

    public void setGia(float gia) {
        this.gia = gia;
    }

    public String getTenSanPhamString() {
        return tenSanPhamString;
    }

    public void setTenSanPhamString(String tenSanPhamString) {
        this.tenSanPhamString = tenSanPhamString;
    }

    public String getNgayNhap() {
        return ngayNhap;
    }

    public void setNgayNhap(String ngayNhap) {
        this.ngayNhap = ngayNhap;
    }

    public String getNgayban() {
        return ngayban;
    }

    public void setNgayban(String ngayban) {
        this.ngayban = ngayban;
    }

    public String getNgayHetHan() {
        return ngayHetHan;
    }

    public void setNgayHetHan(String ngayHetHan) {
        this.ngayHetHan = ngayHetHan;
    }

    public String getMoTa() {
        return moTa;
    }

    public void setMoTa(String moTa) {
        this.moTa = moTa;
    }
   
}



/*
 * 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 QLHH;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Admin
 */
public class SanPhamtModify {
    
    

    public static List<SanPham> findAll() {
        List<SanPham> list = new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hanghoa?serverTimezone=UTC", "root", "");
            String sql = "SELECT sanpham.*, danhmuc.* FROM `sanpham` inner join danhmuc on sanpham.id_danhmuc = danhmuc.id_danhmuc";
            statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);

            while (resultSet.next()) {
                SanPham sanPham = new SanPham(
                        resultSet.getInt("id_sanpham"),
                        resultSet.getFloat("gia"),
                        resultSet.getString("tensanpham"),
                        resultSet.getString("ngaynhap"),
                        resultSet.getString("ngayban"),
                        resultSet.getString("hansudung"),
                        resultSet.getString("mota"),
                        resultSet.getInt("id_danhmuc"),
                        resultSet.getString("tendanhmuc")
                );
                list.add(sanPham);
            }

        } catch (SQLException ex) {
            Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return list;
    }

   

    public static void insertdanhmuc(DanhMuc dm) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hanghoa?serverTimezone=UTC", "root", "");
            String sql = "insert into danhmuc(id_danhmuc, tendanhmuc) values(?,?)";
            statement = connection.prepareCall(sql);
            statement.setInt(1, dm.getId_danhmuc());
            statement.setString(2, dm.getTenDanhMuc());
            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

    }

    public static void insertsanpham(SanPham sp, DanhMuc dm) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hanghoa?serverTimezone=UTC", "root", "");
            String sql = "insert into sanpham(id_sanpham,id_danhmuc, tensanpham,gia,ngaynhap,ngayban,hansudung,mota) values(?,?,?,?,?,?,?,?)";
            statement = connection.prepareCall(sql);
            statement.setInt(1, sp.getId_sanpham());
            statement.setInt(2, dm.getId_danhmuc());
            statement.setString(3, sp.getTenSanPhamString());
            statement.setFloat(4, sp.getGia());
            statement.setString(5, sp.getNgayNhap());
            statement.setString(6, sp.getNgayban());
            statement.setString(7, sp.getNgayHetHan());
            statement.setString(8, sp.getMoTa());
            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

    }
    
     public static void deletesanpham(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hanghoa?serverTimezone=UTC", "root", "");
            String sql = "delete from sanpham where id_sanpham = ?";
            statement = connection.prepareCall(sql);
            statement.setInt(1, id);           
            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
     
     public static void deletedanhmuc(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hanghoa?serverTimezone=UTC", "root", "");
            String sql = "delete from danhmuc where id_danhmuc = ?";
            statement = connection.prepareCall(sql);
            statement.setInt(1, id);           
            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
     
     public static List<SanPham> timkiem(String tensanpham) {
        List<SanPham> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hanghoa?serverTimezone=UTC", "root", "");
            String sql = "SELECT sanpham.*, danhmuc.* FROM `sanpham` inner "
                    + "join danhmuc on sanpham.id_danhmuc = danhmuc.id_danhmuc where sanpham.tensanpham like ?";
            statement = connection.prepareCall(sql);
            statement.setString(1,"%"+tensanpham+"%");
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                SanPham sanPham = new SanPham(
                        resultSet.getInt("id_sanpham"),
                        resultSet.getFloat("gia"),
                        resultSet.getString("tensanpham"),
                        resultSet.getString("ngaynhap"),
                        resultSet.getString("ngayban"),
                        resultSet.getString("hansudung"),
                        resultSet.getString("mota"),
                        resultSet.getInt("id_danhmuc"),
                        resultSet.getString("tendanhmuc")
                );
                list.add(sanPham);
            }

        } catch (SQLException ex) {
            Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return list;
    }

   
public static List<SanPham> tonkho() {
        List<SanPham> list = new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hanghoa?serverTimezone=UTC", "root", "");
            String sql = "SELECT sanpham.*, danhmuc.* FROM `sanpham` inner join danhmuc on sanpham.id_danhmuc = danhmuc.id_danhmuc WHERE sanpham.ngayban is null";
            statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);

            while (resultSet.next()) {
                SanPham sanPham = new SanPham(
                        resultSet.getInt("id_sanpham"),
                        resultSet.getFloat("gia"),
                        resultSet.getString("tensanpham"),
                        resultSet.getString("ngaynhap"),
                        resultSet.getString("ngayban"),
                        resultSet.getString("hansudung"),
                        resultSet.getString("mota"),
                        resultSet.getInt("id_danhmuc"),
                        resultSet.getString("tendanhmuc")
                );
                list.add(sanPham);
            }

        } catch (SQLException ex) {
            Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return list;
    }
  
public static List<SanPham> daban() {
        List<SanPham> list = new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hanghoa?serverTimezone=UTC", "root", "");
            String sql = "SELECT sanpham.*, danhmuc.* FROM `sanpham` inner join danhmuc on sanpham.id_danhmuc = danhmuc.id_danhmuc WHERE sanpham.ngayban is not null";
            statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);

            while (resultSet.next()) {
                SanPham sanPham = new SanPham(
                        resultSet.getInt("id_sanpham"),
                        resultSet.getFloat("gia"),
                        resultSet.getString("tensanpham"),
                        resultSet.getString("ngaynhap"),
                        resultSet.getString("ngayban"),
                        resultSet.getString("hansudung"),
                        resultSet.getString("mota"),
                        resultSet.getInt("id_danhmuc"),
                        resultSet.getString("tendanhmuc")
                );
                list.add(sanPham);
            }

        } catch (SQLException ex) {
            Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return list;
    }

public static List<SanPham> conhansudung() {
        List<SanPham> list = new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hanghoa?serverTimezone=UTC", "root", "");
            String sql = "SELECT sanpham.*, danhmuc.* FROM `sanpham` inner join danhmuc on sanpham.id_danhmuc = danhmuc.id_danhmuc WHERE (CURDATE() > sanpham.hansudung)";
            statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);

            while (resultSet.next()) {
                SanPham sanPham = new SanPham(
                        resultSet.getInt("id_sanpham"),
                        resultSet.getFloat("gia"),
                        resultSet.getString("tensanpham"),
                        resultSet.getString("ngaynhap"),
                        resultSet.getString("ngayban"),
                        resultSet.getString("hansudung"),
                        resultSet.getString("mota"),
                        resultSet.getInt("id_danhmuc"),
                        resultSet.getString("tendanhmuc")
                );
                list.add(sanPham);
            }

        } catch (SQLException ex) {
            Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return list;
    }

public static List<SanPham> hethansudung() {
        List<SanPham> list = new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hanghoa?serverTimezone=UTC", "root", "");
            String sql = "SELECT sanpham.*, danhmuc.* FROM `sanpham` inner join danhmuc on sanpham.id_danhmuc = danhmuc.id_danhmuc WHERE (CURDATE() < sanpham.hansudung)";
            statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);

            while (resultSet.next()) {
                SanPham sanPham = new SanPham(
                        resultSet.getInt("id_sanpham"),
                        resultSet.getFloat("gia"),
                        resultSet.getString("tensanpham"),
                        resultSet.getString("ngaynhap"),
                        resultSet.getString("ngayban"),
                        resultSet.getString("hansudung"),
                        resultSet.getString("mota"),
                        resultSet.getInt("id_danhmuc"),
                        resultSet.getString("tendanhmuc")
                );
                list.add(sanPham);
            }

        } catch (SQLException ex) {
            Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return list;
    }

public static List<SanPham> bantrongngay() {
        List<SanPham> list = new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hanghoa?serverTimezone=UTC", "root", "");
            String sql = "SELECT sanpham.*, danhmuc.* FROM `sanpham` inner join danhmuc on sanpham.id_danhmuc = danhmuc.id_danhmuc WHERE (CURDATE() = ngayban)";
            statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);

            while (resultSet.next()) {
                SanPham sanPham = new SanPham(
                        resultSet.getInt("id_sanpham"),
                        resultSet.getFloat("gia"),
                        resultSet.getString("tensanpham"),
                        resultSet.getString("ngaynhap"),
                        resultSet.getString("ngayban"),
                        resultSet.getString("hansudung"),
                        resultSet.getString("mota"),
                        resultSet.getInt("id_danhmuc"),
                        resultSet.getString("tendanhmuc")
                );
                list.add(sanPham);
            }

        } catch (SQLException ex) {
            Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return list;
    }

public static List<SanPham> bantrongthang() {
        List<SanPham> list = new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hanghoa?serverTimezone=UTC", "root", "");
            String sql = "SELECT sanpham.*, danhmuc.* FROM `sanpham` inner join danhmuc on sanpham.id_danhmuc = danhmuc.id_danhmuc WHERE (MONTH(ngaythem) = MONTH(ngayban))";
            statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);

            while (resultSet.next()) {
                SanPham sanPham = new SanPham(
                        resultSet.getInt("id_sanpham"),
                        resultSet.getFloat("gia"),
                        resultSet.getString("tensanpham"),
                        resultSet.getString("ngaynhap"),
                        resultSet.getString("ngayban"),
                        resultSet.getString("hansudung"),
                        resultSet.getString("mota"),
                        resultSet.getInt("id_danhmuc"),
                        resultSet.getString("tendanhmuc")
                );
                list.add(sanPham);
            }

        } catch (SQLException ex) {
            Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return list;
    }

public static List<SanPham> bantrongtuan() {
        List<SanPham> list = new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hanghoa?serverTimezone=UTC", "root", "");
            String sql = "SELECT sanpham.*, danhmuc.* FROM `sanpham` inner join danhmuc on sanpham.id_danhmuc = danhmuc.id_danhmuc WHERE (YEARWEEK(ngaynhap) = YEARWEEK(ngayban))";
            statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);

            while (resultSet.next()) {
                SanPham sanPham = new SanPham(
                        resultSet.getInt("id_sanpham"),
                        resultSet.getFloat("gia"),
                        resultSet.getString("tensanpham"),
                        resultSet.getString("ngaynhap"),
                        resultSet.getString("ngayban"),
                        resultSet.getString("hansudung"),
                        resultSet.getString("mota"),
                        resultSet.getInt("id_danhmuc"),
                        resultSet.getString("tendanhmuc")
                );
                list.add(sanPham);
            }

        } catch (SQLException ex) {
            Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return list;
    }

public static List<DanhMuc> danhmuc() {
        List<DanhMuc> list12 = new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hanghoa?serverTimezone=UTC", "root", "");
            String sql = "SELECT * FROM danhmuc";
            statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);

            while (resultSet.next()) {
                DanhMuc dm = new DanhMuc(
                resultSet.getInt("id_danhmuc"),
                resultSet.getString("tendanhmuc")
                );
                list12.add(dm);
            }

        } catch (SQLException ex) {
            Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(SanPhamtModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return list12;
    }
}


avatar
thienphu [T1907A]
2020-04-08 13:36:26


code MySql
// login
CREATE TABLE login(
    id INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
    username VARCHAR(30) NOT NULL,
    pass VARCHAR(30) NOT NULL
)
//sanpham
CREATE TABLE sanpham(
    idSp VARCHAR(10) PRIMARY KEY NOT NULL,
    idDM VARCHAR(10) NOT NULL,
    nameSp VARCHAR(20) NOT NULL,
    price VARCHAR(20) NOT NULL,
    inputDate VARCHAR(10) NOT NULL,
    outputDate VARCHAR(10),
    expiryDate VARCHAR(10),
    mota VARCHAR(100) NOT NULL
)
//Danh Muc
CREATE TABLE danhmuc(
    rollNo INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
    idDanhMuc VARCHAR(30) NOT NULL,
    nameDanhMuc VARCHAR(30) NOT NULL
)


avatar
hoangduyminh [T1907A]
2020-04-08 13:24:31



/*
 * 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 java2.lesson2.main;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Minh
 */
public class ProductFrame extends javax.swing.JFrame {

    ArrayList<Product> list = new ArrayList<>();
    DefaultTableModel tableModel;
    productModify productModify;

    /**
     * Creates new form ProductFrame
     */
    public ProductFrame() {
        initComponents();

        tableModel = (DefaultTableModel) tblProduct.getModel();

    }

    public void showAll() {
        list = productModify.showAll();
        tableModel.setRowCount(0);
        for (Product product : list) {
            tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                product.getProductName(),
                product.getnNhap(),
                product.getnBan(),
                product.getHSD(),
                product.getPrice()});
        }
    }

    /**
     * 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">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtProductname = new javax.swing.JTextField();
        txtnNhap = new javax.swing.JTextField();
        txtnBan = new javax.swing.JTextField();
        txtHSD = new javax.swing.JTextField();
        txtgBan = new javax.swing.JTextField();
        btnUpdate = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblProduct = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jCheckBoxMenuItem1 = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItem2 = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItem3 = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItem4 = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItem5 = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItem6 = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItem7 = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItem8 = new javax.swing.JCheckBoxMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Ten San Pham:");
        jLabel1.setToolTipText("");

        jLabel2.setText("Ngay Nhap:");
        jLabel2.setToolTipText("");

        jLabel3.setText("Ngay Ban:");

        jLabel4.setText("HSD:");

        jLabel5.setText("Gia Ban:");

        txtHSD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtHSDActionPerformed(evt);
            }
        });

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

        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(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()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(txtProductname))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addGap(36, 36, 36)
                                    .addComponent(txtnNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 342, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel3))
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(49, 49, 49)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(txtHSD, javax.swing.GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
                                            .addComponent(txtgBan)))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                                        .addComponent(txtnBan, javax.swing.GroupLayout.PREFERRED_SIZE, 342, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(180, 180, 180)
                        .addComponent(btnUpdate)
                        .addGap(68, 68, 68)
                        .addComponent(btnEdit)
                        .addGap(52, 52, 52)
                        .addComponent(btnDelete)))
                .addContainerGap(453, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtProductname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtnNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtnBan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(45, 45, 45)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtHSD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(49, 49, 49)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtgBan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(36, 36, 36)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnUpdate)
                    .addComponent(btnEdit)
                    .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(61, Short.MAX_VALUE))
        );

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

            },
            new String [] {
                "ID", "Ten San Pham", "Ngay Nhap", "Ngay Ban", "HSD", "Gia Ban"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                true, false, true, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblProduct);

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        jMenu3.setText("Fitler");

        jCheckBoxMenuItem1.setSelected(true);
        jCheckBoxMenuItem1.setText("Tim kiem san pham");
        jCheckBoxMenuItem1.setToolTipText("");
        jMenu3.add(jCheckBoxMenuItem1);

        jCheckBoxMenuItem2.setSelected(true);
        jCheckBoxMenuItem2.setText("Danh sach cac san pham ton kho");
        jMenu3.add(jCheckBoxMenuItem2);

        jCheckBoxMenuItem3.setSelected(true);
        jCheckBoxMenuItem3.setText("Danh sach cac san pham da ban");
        jMenu3.add(jCheckBoxMenuItem3);

        jCheckBoxMenuItem4.setSelected(true);
        jCheckBoxMenuItem4.setText("Bao cao san pham trong tuan");
        jMenu3.add(jCheckBoxMenuItem4);

        jCheckBoxMenuItem5.setSelected(true);
        jCheckBoxMenuItem5.setText("Bao cao san pham trong ngay");
        jMenu3.add(jCheckBoxMenuItem5);

        jCheckBoxMenuItem6.setSelected(true);
        jCheckBoxMenuItem6.setText("Bao cao san pham trong thang");
        jMenu3.add(jCheckBoxMenuItem6);

        jCheckBoxMenuItem7.setSelected(true);
        jCheckBoxMenuItem7.setText("San pham con han");
        jMenu3.add(jCheckBoxMenuItem7);

        jCheckBoxMenuItem8.setSelected(true);
        jCheckBoxMenuItem8.setText("San Pham het han");
        jMenu3.add(jCheckBoxMenuItem8);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        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.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>                        

    private void txtHSDActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
    }                                      

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        String productname = txtProductname.getText();
        String hsd = txtHSD.getText();
        String importproduct = txtnNhap.getText();
        String sellday = txtnBan.getText();
        float price = Float.parseFloat(txtgBan.getText());

        Product prt = new Product(productname, importproduct, sellday, hsd, price);
        int selectedIndex = tblProduct.getSelectedRow();
        if (selectedIndex >= 0) {
            Product pro = list.get(selectedIndex);
            prt.setProductId(pro.getProductId());
        }
        productModify.Edit(prt);
        showAll();

    }                                       

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        String productname = txtProductname.getText();
        String importproduct = txtnNhap.getText();
        String sellday = txtnBan.getText();
        String hsd = txtHSD.getText();
        float price = Float.parseFloat(txtgBan.getText());
        Product prt = new Product();
        prt.setProductName(productname);
        prt.setnNhap(importproduct);
        prt.setnBan(sellday);
        prt.setHSD(hsd);
        prt.setPrice(price);

        productModify.update(prt);

        System.out.println(prt.toString());

        showAll();
    }                                         

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        int selectedIndex = tblProduct.getSelectedRow();
        if (selectedIndex >= 0) {
            Product pro = list.get(selectedIndex);

            int option = JOptionPane.showConfirmDialog(this, "DO you want to delete this item?");
            System.out.println("option" + option);

            if (option == 0) {
                productModify.delete(pro.getProductId());

                showAll();

            }
        }
    }                                         

    /**
     * @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(ProductFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ProductFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ProductFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ProductFrame.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 ProductFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem2;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem3;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem4;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem5;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem6;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem7;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem8;
    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.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblProduct;
    private javax.swing.JTextField txtHSD;
    private javax.swing.JTextField txtProductname;
    private javax.swing.JTextField txtgBan;
    private javax.swing.JTextField txtnBan;
    private javax.swing.JTextField txtnNhap;
    // End of variables declaration                   
}
/*
 * 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 java2.lesson2.main;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Minh
 */
public class ProductFrame extends javax.swing.JFrame {

    ArrayList<Product> list = new ArrayList<>();
    DefaultTableModel tableModel;
    productModify productModify;

    /**
     * Creates new form ProductFrame
     */
    public ProductFrame() {
        initComponents();

        tableModel = (DefaultTableModel) tblProduct.getModel();

    }

    public void showAll() {
        list = productModify.showAll();
        tableModel.setRowCount(0);
        for (Product product : list) {
            tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                product.getProductName(),
                product.getnNhap(),
                product.getnBan(),
                product.getHSD(),
                product.getPrice()});
        }
    }

    /**
     * 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">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtProductname = new javax.swing.JTextField();
        txtnNhap = new javax.swing.JTextField();
        txtnBan = new javax.swing.JTextField();
        txtHSD = new javax.swing.JTextField();
        txtgBan = new javax.swing.JTextField();
        btnUpdate = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblProduct = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jCheckBoxMenuItem1 = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItem2 = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItem3 = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItem4 = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItem5 = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItem6 = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItem7 = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItem8 = new javax.swing.JCheckBoxMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Ten San Pham:");
        jLabel1.setToolTipText("");

        jLabel2.setText("Ngay Nhap:");
        jLabel2.setToolTipText("");

        jLabel3.setText("Ngay Ban:");

        jLabel4.setText("HSD:");

        jLabel5.setText("Gia Ban:");

        txtHSD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtHSDActionPerformed(evt);
            }
        });

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

        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(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()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(txtProductname))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addGap(36, 36, 36)
                                    .addComponent(txtnNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 342, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel3))
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(49, 49, 49)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(txtHSD, javax.swing.GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
                                            .addComponent(txtgBan)))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                                        .addComponent(txtnBan, javax.swing.GroupLayout.PREFERRED_SIZE, 342, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(180, 180, 180)
                        .addComponent(btnUpdate)
                        .addGap(68, 68, 68)
                        .addComponent(btnEdit)
                        .addGap(52, 52, 52)
                        .addComponent(btnDelete)))
                .addContainerGap(453, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtProductname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtnNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtnBan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(45, 45, 45)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtHSD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(49, 49, 49)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtgBan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(36, 36, 36)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnUpdate)
                    .addComponent(btnEdit)
                    .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(61, Short.MAX_VALUE))
        );

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

            },
            new String [] {
                "ID", "Ten San Pham", "Ngay Nhap", "Ngay Ban", "HSD", "Gia Ban"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                true, false, true, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblProduct);

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        jMenu3.setText("Fitler");

        jCheckBoxMenuItem1.setSelected(true);
        jCheckBoxMenuItem1.setText("Tim kiem san pham");
        jCheckBoxMenuItem1.setToolTipText("");
        jMenu3.add(jCheckBoxMenuItem1);

        jCheckBoxMenuItem2.setSelected(true);
        jCheckBoxMenuItem2.setText("Danh sach cac san pham ton kho");
        jMenu3.add(jCheckBoxMenuItem2);

        jCheckBoxMenuItem3.setSelected(true);
        jCheckBoxMenuItem3.setText("Danh sach cac san pham da ban");
        jMenu3.add(jCheckBoxMenuItem3);

        jCheckBoxMenuItem4.setSelected(true);
        jCheckBoxMenuItem4.setText("Bao cao san pham trong tuan");
        jMenu3.add(jCheckBoxMenuItem4);

        jCheckBoxMenuItem5.setSelected(true);
        jCheckBoxMenuItem5.setText("Bao cao san pham trong ngay");
        jMenu3.add(jCheckBoxMenuItem5);

        jCheckBoxMenuItem6.setSelected(true);
        jCheckBoxMenuItem6.setText("Bao cao san pham trong thang");
        jMenu3.add(jCheckBoxMenuItem6);

        jCheckBoxMenuItem7.setSelected(true);
        jCheckBoxMenuItem7.setText("San pham con han");
        jMenu3.add(jCheckBoxMenuItem7);

        jCheckBoxMenuItem8.setSelected(true);
        jCheckBoxMenuItem8.setText("San Pham het han");
        jMenu3.add(jCheckBoxMenuItem8);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        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.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>                        

    private void txtHSDActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
    }                                      

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        String productname = txtProductname.getText();
        String hsd = txtHSD.getText();
        String importproduct = txtnNhap.getText();
        String sellday = txtnBan.getText();
        float price = Float.parseFloat(txtgBan.getText());

        Product prt = new Product(productname, importproduct, sellday, hsd, price);
        int selectedIndex = tblProduct.getSelectedRow();
        if (selectedIndex >= 0) {
            Product pro = list.get(selectedIndex);
            prt.setProductId(pro.getProductId());
        }
        productModify.Edit(prt);
        showAll();

    }                                       

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        String productname = txtProductname.getText();
        String importproduct = txtnNhap.getText();
        String sellday = txtnBan.getText();
        String hsd = txtHSD.getText();
        float price = Float.parseFloat(txtgBan.getText());
        Product prt = new Product();
        prt.setProductName(productname);
        prt.setnNhap(importproduct);
        prt.setnBan(sellday);
        prt.setHSD(hsd);
        prt.setPrice(price);

        productModify.update(prt);

        System.out.println(prt.toString());

        showAll();
    }                                         

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        int selectedIndex = tblProduct.getSelectedRow();
        if (selectedIndex >= 0) {
            Product pro = list.get(selectedIndex);

            int option = JOptionPane.showConfirmDialog(this, "DO you want to delete this item?");
            System.out.println("option" + option);

            if (option == 0) {
                productModify.delete(pro.getProductId());

                showAll();

            }
        }
    }                                         

    /**
     * @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(ProductFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ProductFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ProductFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ProductFrame.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 ProductFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem2;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem3;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem4;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem5;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem6;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem7;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem8;
    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.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblProduct;
    private javax.swing.JTextField txtHSD;
    private javax.swing.JTextField txtProductname;
    private javax.swing.JTextField txtgBan;
    private javax.swing.JTextField txtnBan;
    private javax.swing.JTextField txtnNhap;
    // End of variables declaration                   
}



/*
 * 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 java2.lesson2.main;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Minh
 */
public class productModify {

    public static ArrayList<Product> showAll() {
        ArrayList<Product> list = new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/product?serverTimezone=UTC", "root", "");

            statement = connection.createStatement();
            String sql = "Select * from product";
            ResultSet resultSet = statement.executeQuery(sql);

            while (resultSet.next()) {
                Product product = new Product(resultSet.getString("productname"),
                        resultSet.getString("importproduct"),
                        resultSet.getString("sellday"),
                         resultSet.getString("hsd"),
                         resultSet.getFloat("price"));

                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return list;
    }

    public static void update(Product pro) {
         Connection conn = null;
        PreparedStatement statement = null;

        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/assiment?serverTimezone=UTC","root","");

            String sql = "insert into product(proName,importDate,sellDate,HSD,price) values(?,Convert(?,Datetime),Convert(?,Datetime),Convert(?,Datetime),?)";
            statement = conn.prepareCall(sql);
            statement.setString(1, pro.getProductName());
            statement.setString(2, pro.getnNhap());
            statement.setString(3, pro.getnBan());
            statement.setString(4, pro.getHSD());
            statement.setFloat(5, pro.getPrice());
            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public static void delete(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/assiment?serverTimezone=UTC","root","");

            String sql = "delete from product where id=?";
            statement = connection.prepareCall(sql);

            statement.setInt(1, id);
            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public static void Edit(Product prt) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/assiment?serverTimezone=UTC", "root", "");

            String sql = "update product set productname=?, importproduct=?, sellday=?,hsd=?,price=? where id=?";
            statement = connection.prepareCall(sql);

            statement.setString(1, prt.getProductName());
            statement.setString(2, prt.getnNhap());
            statement.setString(3, prt.getnBan());
            statement.setString(4, prt.getHSD());
            statement.setFloat(5, prt.getPrice());
            statement.setInt(6, prt.getProductId());

            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public static ArrayList<Product> findByproName(String productname) {
        ArrayList<Product> list = new ArrayList<>();

        Connection connection = null;
        PreparedStatement statement = null;

        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/assiment?serverTimezone=UTC", "root", "");

            String sql = "select * from product where productname like ?";
            statement = connection.prepareCall(sql);
            statement.setString(1, "%" + productname + "%");

            ResultSet resultset = statement.executeQuery();

            while (resultset.next()) {
                Product product = new Product(resultset.getString("productname"),
                        resultset.getString("importproduct"),
                        resultset.getString("sellday"),
                        resultset.getString("hsd"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        return list;
    }

    public static ArrayList<Product> tonkho() {
        ArrayList<Product> list = new ArrayList<>();

        Connection connection = null;
        PreparedStatement statement = null;

        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/assiment?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (sellday = '' or sellday = ' ' or sellday = null )";
            statement = connection.prepareCall(sql);

            ResultSet resultset = statement.executeQuery();

            while (resultset.next()) {
                Product product = new Product(resultset.getString("productname"),
                        resultset.getString("importproduct"),
                        resultset.getString("sellday"),
                        resultset.getString("hsd"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        return list;
    }

    public static ArrayList<Product> trongngay() {
        ArrayList<Product> list = new ArrayList<>();

        Connection connection = null;
        PreparedStatement statement = null;

        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/assiment?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (CURDATE() = sellday)";
            statement = connection.prepareCall(sql);

            ResultSet resultset = statement.executeQuery();

            while (resultset.next()) {
                Product product = new Product(resultset.getString("productname"),
                        resultset.getString("importproduct"),
                        resultset.getString("sellday"),
                        resultset.getString("hsd"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        return list;
    }

    public static ArrayList<Product> trongtuan() {
        ArrayList<Product> list = new ArrayList<>();

        Connection connection = null;
        PreparedStatement statement = null;

        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/assiment?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (YEARWEEK(importproduct) = YEARWEEK(sellday))";
            statement = connection.prepareCall(sql);

            ResultSet resultset = statement.executeQuery();

            while (resultset.next()) {
                Product product = new Product(resultset.getString("productname"),
                        resultset.getString("importproduct"),
                        resultset.getString("sellday"),
                        resultset.getString("hsd"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        return list;
    }

    public static ArrayList<Product> trongthang() {
        ArrayList<Product> list = new ArrayList<>();

        Connection connection = null;
        PreparedStatement statement = null;

        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/assiment?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (MONTH(importproduct) = MONTH(sellday))";
            statement = connection.prepareCall(sql);

            ResultSet resultset = statement.executeQuery();

            while (resultset.next()) {
                Product product = new Product(resultset.getString("productname"),
                        resultset.getString("importproduct"),
                        resultset.getString("sellday"),
                        resultset.getString("hsd"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        return list;
    }

    public static ArrayList<Product> conhan() {
        ArrayList<Product> list = new ArrayList<>();

        Connection connection = null;
        PreparedStatement statement = null;

        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/assiment?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (CURDATE() < hsd )";
            statement = connection.prepareCall(sql);

            ResultSet resultset = statement.executeQuery();

            while (resultset.next()) {
                Product product = new Product(resultset.getString("productname"),
                        resultset.getString("importproduct"),
                        resultset.getString("sellday"),
                        resultset.getString("hsd"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        return list;
    }

    public static ArrayList<Product> hethan() {
        ArrayList<Product> list = new ArrayList<>();

        Connection connection = null;
        PreparedStatement statement = null;

        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/assiment?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (CURDATE() > hsd)";
            statement = connection.prepareCall(sql);

            ResultSet resultset = statement.executeQuery();

            while (resultset.next()) {
                Product product = new Product(resultset.getString("productname"),
                        resultset.getString("importproduct"),
                        resultset.getString("sellday"),
                        resultset.getString("hsd"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        return list;
    }

    public static ArrayList<Product> daban() {
        ArrayList<Product> list = new ArrayList<>();

        Connection connection = null;
        PreparedStatement statement = null;

        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/assiment?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (sellday > 0)";
            statement = connection.prepareCall(sql);

            ResultSet resultset = statement.executeQuery();

            while (resultset.next()) {
                Product product = new Product(resultset.getString("productname"),
                        resultset.getString("importproduct"),
                        resultset.getString("sellday"),
                        resultset.getString("hsd"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        return list;
    }
}



/*
 * 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 java2.lesson2.main;

/**
 *
 * @author Minh
 */
public class Product {
    String ProductName, nNhap,nBan,HSD;
    float Price;
    int ProductId;

    public Product() {
    }

    public Product(String ProductName, String nNhap, String nBan, String HSD, float Price) {
        this.ProductName = ProductName;
        this.nNhap = nNhap;
        this.nBan = nBan;
        this.HSD = HSD;
        this.Price = Price;
    }

    public String getProductName() {
        return ProductName;
    }

    public void setProductName(String ProductName) {
        this.ProductName = ProductName;
    }

    public String getnNhap() {
        return nNhap;
    }

    public void setnNhap(String nNhap) {
        this.nNhap = nNhap;
    }

    public String getnBan() {
        return nBan;
    }

    public void setnBan(String nBan) {
        this.nBan = nBan;
    }

    public String getHSD() {
        return HSD;
    }

    public void setHSD(String HSD) {
        this.HSD = HSD;
    }

    public int getProductId() {
        return ProductId;
    }

    public void setProductId(int ProductId) {
        this.ProductId = ProductId;
    }

    public float getPrice() {
        return Price;
    }

    public void setPrice(float Price) {
        this.Price = Price;
    }

    @Override
    public String toString() {
        return "Product{" + "ProductName=" + ProductName + ", nNhap=" + nNhap + ", nBan=" + nBan + ", HSD=" + HSD + ", ProductId=" + ProductId + ", Price=" + Price + '}';
    }
    
    
}



create table product (
   id int primary key auto_increment,
   productname varchar(100),
   price float default 0,
   importproduct datetime,
   sellday datetime,
   hsd datetime
)


avatar
Trần Mạnh Dũng [T1907A]
2020-04-08 11:17:51



/*
 * 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 assignmentjavaswing;

/**
 *
 * @author admin
 */
public class Product {
    String ProductName, DayIn, DayOut, ExpiryDate;
    float Price;
    int ProductId;

    public Product() {
    }

    public Product(String ProductName, String DayIn, String DayOut, String ExpiryDate, float Price) {
        this.ProductName = ProductName;
        this.DayIn = DayIn;
        this.DayOut = DayOut;
        this.ExpiryDate = ExpiryDate;
        this.Price = Price;
       
    }

    
    

   
 
    public String getProductName() {
        return ProductName;
    }

    public void setProductName(String ProductName) {
        this.ProductName = ProductName;
    }

    public String getDayIn() {
        return DayIn;
    }

    public void setDayIn(String DayIn) {
        this.DayIn = DayIn;
    }

    public String getDayOut() {
        return DayOut;
    }

    public void setDayOut(String DayOut) {
        this.DayOut = DayOut;
    }

    public String getExpiryDate() {
        return ExpiryDate;
    }

    public void setExpiryDate(String ExpiryDate) {
        this.ExpiryDate = ExpiryDate;
    }

    public float getPrice() {
        return Price;
    }

    public void setPrice(float Price) {
        this.Price = Price;
    }

    public int getProductId() {
        return ProductId;
    }

    public void setProductId(int ProductId) {
        this.ProductId = ProductId;
    }

    @Override
    public String toString() {
        return "Product{" + "ProductName=" + ProductName + ", DayIn=" + DayIn + ", DayOut=" + DayOut + ", ExpiryDate=" + ExpiryDate + ", Price=" + Price + ", ProductId=" + ProductId + '}';
    }
    
}



/*
 * 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 assignmentjavaswing;

import java.awt.List;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author admin
 */
public class productModify {
    
    

    public static ArrayList<Product> showAll() {
        ArrayList<Product> list = new ArrayList<>();
        Connection conn = null;
        Statement statement = null;
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost/assignmentjavaswing?serverTimezone=UTC","root","");
            statement = conn.createStatement();
            String sql = "Select * from product" ;
            ResultSet resultset = statement.executeQuery(sql);
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            if (statement!= null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        

        return list;
    }

    public static void update(Product pro) {
        Connection conn = null;
        PreparedStatement statement = null;

        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost/assignmentjavaswing?serverTimezone=UTC","root","");

            String sql = "insert into product(proName,importDate,sellDate,HSD,price) values(?,Convert(?,Datetime),Convert(?,Datetime),Convert(?,Datetime),?)";
            statement = conn.prepareCall(sql);
            statement.setString(1, pro.getProductName());
            statement.setString(2, pro.getDayIn());
            statement.setString(3, pro.getDayOut());
            statement.setString(4, pro.getExpiryDate());
            statement.setFloat(5, pro.getPrice());
            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    public static void delete(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {
            //lay tat ca danh sach sinh vien
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/assignmentjavaswing?serverTimezone=UTC", "root", "");

            String sql = "delete from product where id = ?";
            statement = connection.prepareCall(sql);
            
            statement.setInt(1, id);
            
            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    public static void Edit(Product pro) {
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/assignmentjavaswing?serverTimezone=UTC", "root", "");

            String sql = "update product set proName=?,importDate=?,sellDate=?,HSD=?,price=? where ProID = ?";
            statement = connection.prepareCall(sql);
            statement.setString(1,pro.getProductName() );
            statement.setString(2,pro.getDayIn() );
            statement.setString(3,pro.getDayOut() );
            statement.setString(4, pro.getExpiryDate());
            statement.setFloat(5,pro.getPrice() );
            statement.setInt(6, pro.getProductId());
            
            
            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    public static ArrayList<Product> findByproName(String proName) {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/assignmentjavaswing?serverTimezone=UTC", "root", "");

            String sql = "select * from product where proName like ?";
            statement = connection.prepareCall(sql);
            statement.setString(1, "%"+proName+"%");
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
    public static ArrayList<Product> tonkho() {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/assignmentjavaswing?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (sellDate = '' or sellDate = ' ' or sellDate = null )";
            statement = connection.prepareCall(sql);
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
    public static ArrayList<Product> trongngay() {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/assignmentjavaswing?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (CURDATE() = sellDate)";
            statement = connection.prepareCall(sql);
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
    public static ArrayList<Product> trongtuan() {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/assignmentjavaswing?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (YEARWEEK(importDate) = YEARWEEK(sellDate))";
            statement = connection.prepareCall(sql);
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
    public static ArrayList<Product> trongthang() {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/assignmentjavaswing?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (MONTH(importDate) = MONTH(sellDate))";
            statement = connection.prepareCall(sql);
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
    
    
    public static ArrayList<Product> conhan() {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/assignmentjavaswing?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (CURDATE() < HSD )";
            statement = connection.prepareCall(sql);
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
    public static ArrayList<Product> hethan() {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/assignmentjavaswing?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (CURDATE() > HSD)";
            statement = connection.prepareCall(sql);
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
    public static ArrayList<Product> daban() {
        ArrayList<Product> list = new ArrayList<>();
        
        Connection connection = null;
        PreparedStatement statement = null;
        
        try {

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/assignmentjavaswing?serverTimezone=UTC", "root", "");

            String sql = "select * from product where (selldate > 0)";
            statement = connection.prepareCall(sql);
            
            ResultSet resultset = statement.executeQuery();
            
            while (resultset.next()) {                
                Product product = new Product(resultset.getString("proName"),
                        resultset.getString("importDate"),
                        resultset.getString("sellDate"),
                        resultset.getString("HSD"),
                        resultset.getFloat("price"));
                list.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(productModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        
        return list;
    }
}



/*
 * 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 assignmentjavaswing;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author admin
 */
public class AssignmentJFrame extends javax.swing.JFrame {

    ArrayList<Product>list = new ArrayList<>();
    DefaultTableModel tableModel;
    productModify proModify;
    
    public AssignmentJFrame() {
        initComponents();
        tableModel = (DefaultTableModel) tblProduct.getModel();
        showAll();
    }
    public void showAll(){
         list = productModify.showAll();
        tableModel.setRowCount(0);
        for (Product product : list) {
            tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                product.getProductName(),
                product.getDayIn(),
                product.getDayOut(),
                product.getExpiryDate(),
                product.getPrice()});
        }
    }
    /**
     * 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">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txttensp = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtngaynhap = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtngayban = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtHSD = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtprice = new javax.swing.JTextField();
        btnadd = new javax.swing.JButton();
        btnedit = new javax.swing.JButton();
        btndelete = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblProduct = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Product's Name:");

        jLabel2.setText("Day In:");

        txtngaynhap.setText("yyyy-mm-dd");

        jLabel3.setText("Day Out");

        txtngayban.setText("yyyy-mm-dd");

        jLabel4.setText("Expiry Date:");

        txtHSD.setText("yyyy-mm-dd");
        txtHSD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtHSDActionPerformed(evt);
            }
        });

        jLabel5.setText("Price:");

        txtprice.setText("$");

        btnadd.setText("Add");
        btnadd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnaddActionPerformed(evt);
            }
        });

        btnedit.setText("Edit");
        btnedit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btneditActionPerformed(evt);
            }
        });

        btndelete.setText("Delete");
        btndelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btndeleteActionPerformed(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()
                .addGap(27, 27, 27)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addGap(77, 77, 77)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txttensp)
                    .addComponent(txtngaynhap)
                    .addComponent(txtngayban)
                    .addComponent(txtHSD)
                    .addComponent(txtprice)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnadd)
                        .addGap(33, 33, 33)
                        .addComponent(btnedit)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
                        .addComponent(btndelete)
                        .addGap(29, 29, 29)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txttensp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtngaynhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(txtngayban, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtHSD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtprice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(42, 42, 42)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnadd)
                    .addComponent(btnedit)
                    .addComponent(btndelete))
                .addContainerGap(31, Short.MAX_VALUE))
        );

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

            },
            new String [] {
                "Product_ID", "Product_Name", "Day In", "Day uot", "Expiry Date", "Price"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, true, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tblProduct);

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        jMenu3.setText("Filter");

        jMenuItem1.setText("Search Product");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem1);

        jMenuItem2.setText("Product Inventory List");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem2);

        jMenuItem3.setText("List Of Products Sold");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem3);

        jMenuItem4.setText("Products Sold During The Week");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem4);

        jMenuItem6.setText("Products Sold During The Month");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem6);

        jMenuItem5.setText("Products Sold During The Day");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem5);

        jMenuItem7.setText("Still Valid");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem7);

        jMenuItem8.setText("Expired");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem8);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 567, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void txtHSDActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
    }                                      

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        String input = JOptionPane.showInputDialog(this, "Enter full name to search");
        if(input != null && input.length() > 0) {
            list = productModify.findByproName(input);
            
            tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getDayIn(),
                    product.getDayOut(),
                    product.getExpiryDate(),
                    product.getPrice()});
            });
        }
        else {
            showAll();
        }
    }                                          

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        list = productModify.tonkho();
        tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getDayIn(),
                    product.getDayOut(),
                    product.getExpiryDate(),
                    product.getPrice()});
            });
    }                                          

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
         list = productModify.daban();
        tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getDayIn(),
                    product.getDayOut(),
                    product.getExpiryDate(),
                    product.getPrice()});
            });
    }                                          

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:list = productModify.trongtuan();
        tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getDayIn(),
                    product.getDayOut(),
                    product.getExpiryDate(),
                    product.getPrice()});
            });
    }                                          

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        list = productModify.trongthang();
        tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getDayIn(),
                    product.getDayOut(),
                    product.getExpiryDate(),
                    product.getPrice()});
            });
    }                                          

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        list = productModify.trongngay();
        tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getDayIn(),
                    product.getDayOut(),
                    product.getExpiryDate(),
                    product.getPrice()});
            });
    }                                          

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        list = productModify.conhan();
        tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getDayIn(),
                    product.getDayOut(),
                    product.getExpiryDate(),
                    product.getPrice()});
            });
    }                                          

    private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        list = productModify.hethan();
        tableModel.setRowCount(0);
        
            list.forEach((Product product) -> {
                tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
                    product.getProductName(),
                    product.getDayIn(),
                    product.getDayOut(),
                    product.getExpiryDate(),
                    product.getPrice()});
            });
    }                                          

    private void btnaddActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        String proName = txttensp.getText();
        String HSD = txtHSD.getText();
        String importDate = txtngaynhap.getText();
        String sellDate = txtngayban.getText();
        float price = Float.parseFloat(txtprice.getText());
        Product pr = new Product();
        pr.setProductName(proName);
        pr.setExpiryDate(HSD);
        pr.setDayOut(sellDate);
        pr.setDayIn(importDate);
        pr.setPrice(price);

        productModify.update(pr);

        System.out.println(pr.toString());

        showAll();
    }                                      

    private void btndeleteActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        int selectedIndex = tblProduct.getSelectedRow();
        if(selectedIndex >= 0) {
            Product pro = list.get(selectedIndex);
            
            int option = JOptionPane.showConfirmDialog(this, "Do you want to delete this item?");
            System.out.println("option : " + option);
            
            if(option == 0) {
                productModify.delete(pro.getProductId());
                
                showAll();
            }
        }
    }                                         

    private void btneditActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        String proName = txttensp.getText();
        String HSD = txtHSD.getText();
        String importDate = txtngaynhap.getText();
        String sellDate = txtngayban.getText();
        float price = Float.parseFloat(txtprice.getText());
        Product pr = new Product(proName,importDate,sellDate,HSD,price);
        int selectedIndex = tblProduct.getSelectedRow();
        if(selectedIndex >= 0){
            Product pro = list.get(selectedIndex);
            pr.setProductId(pro.getProductId());
        }
        productModify.Edit(pr);
        showAll();
    }                                       

    /**
     * @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(AssignmentJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AssignmentJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AssignmentJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AssignmentJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
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(AssignmentJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AssignmentJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AssignmentJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AssignmentJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AssignmentJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btnadd;
    private javax.swing.JButton btndelete;
    private javax.swing.JButton btnedit;
    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.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable tblProduct;
    private javax.swing.JTextField txtHSD;
    private javax.swing.JTextField txtngayban;
    private javax.swing.JTextField txtngaynhap;
    private javax.swing.JTextField txtprice;
    private javax.swing.JTextField txttensp;
    // End of variables declaration                   
}



create table product (
   id int primary key auto_increment,
   proName varchar(100),
   price float default 0,
   importDate datetime,
   sellDate datetime,
   HSD datetime
)