IMG-LOGO
×

Tài Liệu Học

Khoá học lập trình Java Core nâng cao

Exception & Collection

[Video] Tìm hiểu exception + debug + package - lập trình Java Nâng Cao [Video] Tìm hiểu collections, Sets, Maps và Generic - lập trình java nâng cao [Video] Chương trình quản lý sinh viên bằng HashMap - Java Advanced

File

[Video] Hướng dẫn đọc ghi file FileInputStream, FileOutputStream, ObjectInputStream trong java - lập trình java nâng cao [Video] Bài tập quản lý sách & lưu thông tin trên Files [Video] File - Quản lý thông tin sinh viên [Video] Quản lý thông tin sinh & lưu object file - Java nâng cao [Video] Viết ứng dụng WinRar | WinZip | 7Zip bằng Java - Nén file Java & giải nén file Java - Khoá học Java [Video] Bài tập quản lý bán vé máy bay - java nâng cao

Thread

[Video] Phân tích quản lý ứng dụng trong HĐH (Window, Mac, Linux)-Phân tích Game đa luồng Java [Video] Tìm hiểu Thread - Đồng bộ Thread - lập trình java nâng cao - Bổ sung thêm lớp học mới. [Video] Giải thích synchronized + Lap trinh Java nang cao [Video] Thread thực hiện in ngẫu nhiên các số nguyên và ký tự - Phần 1 [Video] Thread thực hiện in ngẫu nhiên các số nguyên và ký tự & Đông bộ thread + wait notify + synchronized + Java nâng cao - Phần 2 [Video] Synchronized 2 Thread - Bài tập mảng tên + địa chỉ - Java nâng cao [Video] Bài tập ôn luyện tổng hợp File, OOP, Thread trong java [Video] Sử dụng MultiThreading sinh ký tự a-zA-Z trong java

CSDL

[Video] Kết nối MySQL trong java swing JDBC trong java - lập trình java nâng cao [Video] Kết nối CSDL bằng java swing phần 1 - lập trình java nâng cao [Video] Kết nối CSDL bằng java swing phần 2 - lập trình java nâng cao [Video] Chương trình quản lý thư viện Java + XML Java + CSDL Java- SQL Java [Video] Phần mềm quản lý sinh viên MySQL + Java - Chương trình quản lý sinh viên MySQL + Java - Lập Trình Java [Video] Bài Tập Quản Lý Sinh Viên - CSDL - Java Swing [Video] Chương trình quản lý sản phẩm - quản lý tin tức - quản lý bán hàng - Lập trình Java [Video] Chức năng register & login + CSDL - Lập trình Java nâng cao - Phần 1 [Video] Hướng dẫn sử dụng Persistence Java - Kết nối database Java - MySQL Java nâng cao

Design Pattern & Đa Ngôn Ngữ

[Video] Design Pattern lập trình java nâng cao - lập trình java nâng cao

XML & JSON & CSDL

[Video] C1 > Chương trình quản lý sinh viên Java + Import/Export JSON + File - Lập Trình Java [Video] C2 > Chương trình quản lý sinh viên Java + Import/Export JSON + File - Lập Trình Java

Ôn Tập Tổng Quát & Exmination

[Video] Bài tập - Chường trình quản lý sở thú - Lập trình Java - Lập trình Java nâng cao - Phần 1 [Video] Bài tập - Chường trình quản lý sở thú - Lập trình Java - Lập trình Java nâng cao - Phần 2




Trang Chủ Java Advanced [Assignment] >> Quản lý bán hàng + java swing

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

by GokiSoft.com - 17:23 29/10/2021 20,891 Lượt Xem

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

Bình luận



Chia sẻ từ lớp học

NguyenHuuThanh [T1907A]

Ngày viết: 11:48 10/04/2020



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


NguyenHuuThanh [T1907A]

Ngày viết: 11:48 10/04/2020



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


Tài Liệu Tham Khảo

Đã sao chép!!!