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,885 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

thienphu [T1907A]

Ngày viết: 20:36 08/04/2020


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


thienphu [T1907A]

Ngày viết: 12:41 08/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 QuanLiSanPham;

import java.util.Scanner;

/**
 *
 * @author Thien Phu
 */
public class DanhMucSanPham {

    int rollNo;
    String idDM;
    String nameDm;

    public DanhMucSanPham() {
    }

    public DanhMucSanPham(int rollNo, String idDM, String nameDm) {
        this.rollNo = rollNo;
        this.idDM = idDM;
        this.nameDm = nameDm;
    }

    public DanhMucSanPham(String idDM, String nameDm) {
        this.idDM = idDM;
        this.nameDm = nameDm;
    }
    
    public int getRollNo() {
        return rollNo;
    }

    public void setRollNo(int rollNo) {
        this.rollNo = rollNo;
    }

    public String getIdDM() {
        return idDM;
    }

    public void setIdDM(String idDM) {
        this.idDM = idDM;
    }

    public String getNameDm() {
        return nameDm;
    }

    public void setNameDm(String nameDm) {
        this.nameDm = nameDm;
    }

    @Override
    public String toString() {
        return "DanhMucSanPham{" + "rollNo=" + rollNo + ", idDM=" + idDM + ", nameDm=" + nameDm + '}';
    }

    void display() {
        System.out.println(toString());
    }

    void input() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Nhap rollNo danh muc");
        rollNo = Integer.parseInt(sc.nextLine());
        System.out.println("Nhap id danh muc ");
        idDM = sc.nextLine();
        System.out.println("Nhap ten danh muc");
        nameDm = sc.nextLine();
    }
}



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

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;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Thien Phu
 */
public class FormDanhMuc extends javax.swing.JFrame {

    /**
     * Creates new form FormDanhMuc
     */
    List<DanhMucSanPham> listdm;
    DefaultTableModel tableModel;
    int index = 0;

    public FormDanhMuc() {
        initComponents();
        listdm = new ArrayList<>();
        tableModel = (DefaultTableModel) jtableDanhMuc.getModel();
        readData();
    }

    /**
     * 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();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tfIDDM = new javax.swing.JTextField();
        tfNameDm = new javax.swing.JTextField();
        btsave = new javax.swing.JButton();
        btdelete = new javax.swing.JButton();
        btupdate = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jtableDanhMuc = 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);

        jTable2.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"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Quản Lí Danh Mục", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14))); // NOI18N

        jLabel1.setText("ID Danh Muc");

        jLabel2.setText("Name Danh Muc");

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

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

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

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

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

            },
            new String [] {
                "RollNo", "ID Danh Muc", "Name Danh Muc"
            }
        ));
        jtableDanhMuc.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtableDanhMucMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(jtableDanhMuc);

        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(58, 58, 58)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(btsave, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(34, 34, 34)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btdelete)
                        .addGap(18, 18, 18)
                        .addComponent(btupdate))
                    .addComponent(tfIDDM, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                    .addComponent(tfNameDm))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfIDDM, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tfNameDm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(42, 42, 42)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btsave)
                    .addComponent(btdelete)
                    .addComponent(btupdate))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(31, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .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()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

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

    private void btsaveActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        DanhMucSanPham dm = new DanhMucSanPham(tfIDDM.getText(), tfNameDm.getText());
        inserIntoMySql(dm);
        readData();
        reset();

    }                                      

    private void jtableDanhMucMouseClicked(java.awt.event.MouseEvent evt) {                                           
        // TODO add your handling code here:
        index = jtableDanhMuc.getSelectedRow();
        tfIDDM.setText(listdm.get(index).getIdDM());
        tfNameDm.setText(listdm.get(index).getNameDm());
    }                                          

    private void btdeleteActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        if (tfIDDM.getText().equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Chọn danh muc sp cần xóa dưới bảng thông tin");
        } else {
            delete(tfIDDM.getText());
            JOptionPane.showMessageDialog(rootPane, "Xoa thanh cong id san pham : " + tfIDDM.getText());
            readData();
            reset();
        }
    }                                        
    private void reset(){
        tfIDDM.setText("");
        tfNameDm.setText("");
    }
    private void btupdateActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
//lay rollNo cua danh muc update
        int roll = listdm.get(index).getRollNo();
        System.out.println("Roll: "+roll);
        //update sql
        Connection conn = null;
        Statement statement = null;
        System.out.println(tfIDDM.getText()+"\t"+tfNameDm.getText());

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t1907a", "root", "");
            String sql = "UPDATE danhmuc SET idDanhMuc= '" + tfIDDM.getText() + "',nameDanhMuc= '" + tfNameDm.getText() + "' where rollNo = '"
                    + roll + "'";
            statement = conn.createStatement();
             statement.executeUpdate(sql);
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(FormDanhMuc.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(FormDanhMuc.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(FormDanhMuc.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        readData();
        reset();
    }                                        
    private void inserIntoMySql(DanhMucSanPham sp) {
        Connection conn = null;
        PreparedStatement preparedStatement = null;

        try {

            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t1907a", "root", "");
            //query

            String sql = "insert into danhmuc(idDanhMuc,nameDanhMuc )values (?,?)";

            //  System.out.println("Chay qua day");
            preparedStatement = conn.prepareStatement(sql);

            preparedStatement.setString(1, sp.getIdDM());
            preparedStatement.setString(2, sp.getNameDm());

            preparedStatement.execute();

        } catch (SQLException ex) {
            Logger.getLogger(FormDanhMuc.class
                    .getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(FormDanhMuc.class
                    .getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();

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

                } catch (SQLException ex) {
                    Logger.getLogger(FormDanhMuc.class
                            .getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        // System.err.println("Chay qua day");
    }

    private void readData() {
        listdm.clear();
        Connection conn = null;
        Statement statement = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t1907a", "root", "");
            String sql = "select * from danhmuc";

            statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            //doc du lieu từng hàng trong mysql
            while (resultSet.next()) {

                DanhMucSanPham sp = new DanhMucSanPham(Integer.parseInt(resultSet.getString("rollNo")), resultSet.getString("idDanhMuc"),
                        resultSet.getString("nameDanhMuc"));

                listdm.add(sp);

            }
        } catch (SQLException ex) {
            Logger.getLogger(QuanLiSp.class
                    .getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(QuanLiSp.class
                    .getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (conn != null) {
                try {
                    conn.close();

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

                } catch (SQLException ex) {
                    Logger.getLogger(QuanLiSp.class
                            .getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        display();
    }

    private void delete(String data) {
        Connection conn = null;
        Statement statement = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t1907a", "root", "");

            String sql = "DELETE FROM danhmuc WHERE idDanhMuc =" + "'" + data + "'";

            statement = conn.createStatement();
            statement.executeUpdate(sql);
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(QuanLiSp.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(QuanLiSp.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(QuanLiSp.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public void display() {
        tableModel.setRowCount(0);
        for (DanhMucSanPham listdm1 : listdm) {
            tableModel.addRow(new Object[]{tableModel.getRowCount() + 1, listdm1.getIdDM(), listdm1.getNameDm()});
        }
    }

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

    // Variables declaration - do not modify                     
    private javax.swing.JButton btdelete;
    private javax.swing.JButton btsave;
    private javax.swing.JButton btupdate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable jtableDanhMuc;
    private javax.swing.JTextField tfIDDM;
    private javax.swing.JTextField tfNameDm;
    // 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 QuanLiSanPham;

/**
 *
 * @author Thien Phu
 */
public class SanPham {

    String idSp, maDanhMuc;
    String nameSp, price;
    String inputdate, outputdate, hsd, mota;

    public SanPham() {
    }

    public SanPham(String idSp, String maDanhMuc, String nameSp, String price, String inputdate, String outputdate, String hsd, String mota) {
        this.idSp = idSp;
        this.maDanhMuc = maDanhMuc;
        this.nameSp = nameSp;
        this.price = price;
        this.inputdate = inputdate;
        this.outputdate = outputdate;
        this.hsd = hsd;
        this.mota = mota;
    }

    public String getIdSp() {
        return idSp;
    }

    public void setIdSp(String idSp) {
        this.idSp = idSp;
    }

    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 String getPrice() {
        return price;
    }

    public void setPrice(String 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 getMota() {
        return mota;
    }

    public void setMota(String mota) {
        this.mota = mota;
    }

    @Override
    public String toString() {
        return "SanPham{" + "idSp=" + idSp + ", maDanhMuc=" + maDanhMuc + ", nameSp=" + nameSp + ", price=" + price + ", inputdate=" + inputdate + ", outputdate=" + outputdate + ", hsd=" + hsd + ", mota=" + mota + '}';
    }

    void display() {
        System.out.println(toString());
    }
}



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

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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Thien Phu
 */
public class QuanLiSp extends javax.swing.JFrame {

    /**
     * Creates new form QuanLiSp
     */
    String getdata;
    List<SanPham> listSanPham;
    DefaultTableModel tableModel;
    List<SanPham> listSpSell;
    List<SanPham> listSpTonKho;
    List<SanPham> listSpHetHSd;
    List<SanPham> listSaphetHsd;//sp sap het han su dung

    public QuanLiSp() {
        initComponents();
        listSanPham = new ArrayList<>();
        tableModel = (DefaultTableModel) showthongtin.getModel();
        readData();
        listSpSell = new ArrayList<>();
        listSpTonKho = new ArrayList<>();
        listSpHetHSd = new ArrayList<>();
        listSaphetHsd = new ArrayList<>();
        setLocationRelativeTo(this);
        //tfIDSp.setEditable(false);
    }

    /**
     * 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();
        tfIDSp = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        tfIDDM = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        tfNameSp = new javax.swing.JTextField();
        tfPrice = new javax.swing.JTextField();
        tfinputDate = new javax.swing.JTextField();
        tfOutputDate = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        tfHSd = new javax.swing.JTextField();
        tfMota = new javax.swing.JTextField();
        btSave = new javax.swing.JButton();
        btDelete = new javax.swing.JButton();
        btUpdate = new javax.swing.JButton();
        btReset = new javax.swing.JButton();
        btTonkho = new javax.swing.JButton();
        btSaphetHSd = new javax.swing.JButton();
        btSpSell = new javax.swing.JButton();
        btFind = new javax.swing.JButton();
        btSanphamHetHsd = new javax.swing.JButton();
        btAllSanpham = new javax.swing.JButton();
        btdanhmuc = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        showthongtin = 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);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Quan Li San Pham", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 18))); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Nhap Thong Tin San Pham", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        jLabel1.setText("ID SP");
        jLabel1.setMaximumSize(new java.awt.Dimension(26, 20));
        jLabel1.setMinimumSize(new java.awt.Dimension(26, 20));
        jLabel1.setPreferredSize(new java.awt.Dimension(26, 20));

        jLabel2.setText("ID Danh Muc");
        jLabel2.setMaximumSize(new java.awt.Dimension(26, 20));
        jLabel2.setMinimumSize(new java.awt.Dimension(26, 20));
        jLabel2.setPreferredSize(new java.awt.Dimension(26, 20));

        jLabel3.setText("Name SP");
        jLabel3.setMaximumSize(new java.awt.Dimension(26, 20));
        jLabel3.setMinimumSize(new java.awt.Dimension(26, 20));
        jLabel3.setPreferredSize(new java.awt.Dimension(26, 20));

        jLabel4.setText("Price");
        jLabel4.setMaximumSize(new java.awt.Dimension(26, 20));
        jLabel4.setMinimumSize(new java.awt.Dimension(26, 20));
        jLabel4.setPreferredSize(new java.awt.Dimension(26, 20));

        jLabel5.setText("Input Date");
        jLabel5.setMaximumSize(new java.awt.Dimension(26, 20));
        jLabel5.setMinimumSize(new java.awt.Dimension(26, 20));
        jLabel5.setPreferredSize(new java.awt.Dimension(26, 20));

        jLabel6.setText("Output Date");
        jLabel6.setMaximumSize(new java.awt.Dimension(26, 20));
        jLabel6.setMinimumSize(new java.awt.Dimension(26, 20));
        jLabel6.setPreferredSize(new java.awt.Dimension(26, 20));

        jLabel7.setText("Expiry Date");
        jLabel7.setMaximumSize(new java.awt.Dimension(26, 20));
        jLabel7.setMinimumSize(new java.awt.Dimension(26, 20));
        jLabel7.setPreferredSize(new java.awt.Dimension(26, 20));

        jLabel8.setText("Describe");
        jLabel8.setMaximumSize(new java.awt.Dimension(26, 20));
        jLabel8.setMinimumSize(new java.awt.Dimension(26, 20));
        jLabel8.setPreferredSize(new java.awt.Dimension(26, 20));

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

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

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

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

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(77, 77, 77)
                        .addComponent(btSave, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32)
                        .addComponent(btDelete)
                        .addGap(29, 29, 29)
                        .addComponent(btUpdate)
                        .addGap(18, 18, 18)
                        .addComponent(btReset, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(tfMota, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(tfHSd, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(tfOutputDate, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(tfinputDate, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(tfPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(tfNameSp, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                                .addComponent(tfIDDM, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(tfIDSp, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(75, 75, 75))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tfIDSp, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, 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(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfIDDM, 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(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfNameSp, 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(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfinputDate, 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(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfOutputDate, 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(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfHSd, 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(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfMota, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(56, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btSave)
                    .addComponent(btDelete)
                    .addComponent(btUpdate)
                    .addComponent(btReset))
                .addContainerGap())
        );

        btTonkho.setText("Sản Phẩm Tồn Kho");
        btTonkho.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTonkhoActionPerformed(evt);
            }
        });

        btSaphetHSd.setText("Sản Phẩm Sắp Hết HSD");
        btSaphetHSd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSaphetHSdActionPerformed(evt);
            }
        });

        btSpSell.setText("Sản Phẩm Đã bán");
        btSpSell.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSpSellActionPerformed(evt);
            }
        });

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

        btSanphamHetHsd.setText("Sản Phẩm Hết HSd");
        btSanphamHetHsd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSanphamHetHsdActionPerformed(evt);
            }
        });

        btAllSanpham.setText("Thống Kê Sản Phẩm");
        btAllSanpham.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAllSanphamActionPerformed(evt);
            }
        });

        btdanhmuc.setText("Quản Lí Danh Mục");
        btdanhmuc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btdanhmucActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(66, 66, 66)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btSaphetHSd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btSanphamHetHsd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btTonkho, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btSpSell, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btFind, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btAllSanpham, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btdanhmuc, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 66, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(58, 58, 58))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(btFind, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btSpSell, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btTonkho, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30)
                        .addComponent(btSanphamHetHsd, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33)
                        .addComponent(btSaphetHSd, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32)
                        .addComponent(btAllSanpham, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(btdanhmuc, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .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))
        );

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

            },
            new String [] {
                "IDSP", "IDDM", "NameSP", "Price", "InputDate", "outputDate", "ExpỉyDate", "Describe"
            }
        ));
        showthongtin.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                showthongtinMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(showthongtin);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane2)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(26, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 11, Short.MAX_VALUE))
        );

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

    private void btFindActionPerformed(java.awt.event.ActionEvent evt) {                                       
        String input = JOptionPane.showInputDialog(rootPane, "Nhập id sản phẩm cần tìm");
        List<SanPham> findSp = new ArrayList<>();
        boolean check = false;
        if (input != null && input.length() > 0) {
            for (SanPham sp : listSanPham) {
                if (sp.getIdSp().equalsIgnoreCase(input)) {
                    findSp.add(sp);
                    check = true;
                }
            }
            if (check) {
                tableModel.setRowCount(0);
                for (SanPham sp : findSp) {
                    tableModel.addRow(new Object[]{sp.getIdSp(),
                        sp.getMaDanhMuc(), sp.getNameSp(), sp.getPrice(), sp.getInputdate(), sp.getOutputdate(),
                        sp.getHsd(), sp.getMota()});
                }

            } else {
                JOptionPane.showMessageDialog(rootPane, input + " không tồn tại");
            }

        }
    }                                      

    private void btSaveActionPerformed(java.awt.event.ActionEvent evt) {                                       
        //save
        SanPham sp = new SanPham();
        sp.setIdSp(tfIDSp.getText());
        sp.setMaDanhMuc(tfIDDM.getText());
        sp.setNameSp(tfNameSp.getText());
        sp.setPrice(tfPrice.getText());
        sp.setInputdate(tfinputDate.getText());
        sp.setOutputdate(tfOutputDate.getText());
        sp.setHsd(tfHSd.getText());
        sp.setMota(tfMota.getText());
        inserIntoMySql(sp);
        //ghi xuong my sql
        readData();
    }                                      

    private void btSpSellActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        listSpSell.clear();
        for (SanPham sanpham1 : listSanPham) {
            if (!"".equals(sanpham1.getOutputdate())) {
                listSpSell.add(sanpham1);
            }
        }
        //System.out.println("San pham da ban");
        display(listSpSell);


    }                                        

    private void btTonkhoActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        //danh sach san pham ton kho
        listSpTonKho.clear();
        for (SanPham sanpham1 : listSanPham) {
            if ("".equals(sanpham1.getOutputdate())) {
                listSpTonKho.add(sanpham1);
            }
        }
        //System.out.println("San pham da ban");
        display(listSpTonKho);

    }                                        

    private void btSanphamHetHsdActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // TODO add your handling code here:
        //danh sach sp het han su dung
        listSpHetHSd.clear();
        listSpTonKho.clear();
        for (SanPham sanpham1 : listSanPham) {
            if ("".equals(sanpham1.getOutputdate())) {
                listSpTonKho.add(sanpham1);
            }
        }
        for (int i = 0; i < listSpTonKho.size(); i++) {
            if (checkHsd(listSpTonKho.get(i).getHsd()) > 0) {
                listSpHetHSd.add(listSpTonKho.get(i));
            }
        }
        System.out.println("Danh sach san pham het hsd:\n");
        display(listSpHetHSd);
    }                                               

    private void btSaphetHSdActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        //danh sach sp sap het han su dung
        listSaphetHsd.clear();
        listSpTonKho.clear();
        for (SanPham sanpham1 : listSanPham) {
            if ("".equals(sanpham1.getOutputdate())) {
                listSpTonKho.add(sanpham1);
            }
        }
        for (SanPham sp : listSpTonKho) {
            if ((checkHsd(sp.getHsd())) >= -7 && checkHsd(sp.getHsd()) < 0) {

                listSaphetHsd.add(sp);
            }
        }
        if (listSaphetHsd.isEmpty()) {
            JOptionPane.showMessageDialog(rootPane, "Không có sản phẩm nào sắp hết hạn ");

        } else {
            display(listSaphetHsd);
        }
        System.out.println("Danh sach sp sap het han su dung:\n");
        for (SanPham sp : listSaphetHsd) {
            sp.display();
        }
    }                                           

    private void btAllSanphamActionPerformed(java.awt.event.ActionEvent evt) {                                             
        readData();
    }                                            

    private void showthongtinMouseClicked(java.awt.event.MouseEvent evt) {                                          
        // TODO add your handling code here:
        int index = showthongtin.getSelectedRow();
        tfIDSp.setText(listSanPham.get(index).getIdSp());
        tfIDDM.setText(listSanPham.get(index).getMaDanhMuc());
        tfNameSp.setText(listSanPham.get(index).getNameSp());
        tfPrice.setText(listSanPham.get(index).getPrice());
        tfinputDate.setText(listSanPham.get(index).getInputdate());
        tfOutputDate.setText(listSanPham.get(index).getOutputdate());
        tfHSd.setText(listSanPham.get(index).getHsd());
        tfMota.setText(listSanPham.get(index).getMota());
        getdata = tfIDSp.getText();
        System.out.println("Data:" + getdata);
    }                                         

    private void btDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        if (tfIDSp.getText().equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Chọn sản phẩm cần xóa dưới bảng thông tin");
        } else {
            delete(tfIDSp.getText());
            JOptionPane.showMessageDialog(rootPane, "Xoa thanh cong id san pham : " + tfIDSp.getText());
            readData();
        }
    }                                        

    private void btResetActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        tfIDSp.setText("");
        tfIDDM.setText("");
        tfNameSp.setText("");
        tfPrice.setText("");
        tfinputDate.setText("");
        tfOutputDate.setText("");
        tfHSd.setText("");
        tfMota.setText("");


    }                                       

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

        //update
        if (tfIDSp.getText().equalsIgnoreCase(getdata)) {
            String idDM = tfIDDM.getText();
            String nameSp = tfNameSp.getText();
            String price = tfPrice.getText();
            String inputDate = tfinputDate.getText();
            String outputDate = tfOutputDate.getText();
            String hs = tfHSd.getText();
            String mota = tfMota.getText();

            //ket noi sql
            Connection conn = null;
            Statement statement = null;

            try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t1907a", "root", "");
                String sql = "UPDATE sanpham SET idDM=" + "'" + idDM + "'" + ",nameSp=" + "'" + nameSp + "',price="
                        + "'" + price + "',inputDate= '" + inputDate + "',outputDate= '" + outputDate + "',expiryDate= '"
                        + hs + "',mota = '" + mota + "' where idSp= '" + getdata+"'";
                statement = conn.createStatement();
                statement.executeUpdate(sql);
            } catch (ClassNotFoundException | SQLException ex) {
                Logger.getLogger(QuanLiSp.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(QuanLiSp.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                if (statement != null) {
                    try {
                        statement.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(QuanLiSp.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
            readData();
        } else {
            JOptionPane.showMessageDialog(rootPane, "không được sửa idSP");
        }

    }                                        

    private void btdanhmucActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        FormDanhMuc dm = new FormDanhMuc();
        dm.setVisible(true);
    }                                         
    private int checkHsd(String d1) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        int detDiff = 0;
        boolean check = false;
        try {
            // String ngay = "2020-04-01";
            Date curent = new Date();
            String nowDate = sdf.format(curent);
            Date date1 = sdf.parse(d1);
            Date date2 = sdf.parse(nowDate);

            detDiff = (int) (date2.getTime() - date1.getTime());
            detDiff = detDiff / (24 * 60 * 60 * 1000);
            System.out.println("Date khoangcach: " + detDiff);

        } catch (ParseException ex) {
            Logger.getLogger(QuanLiSp.class.getName()).log(Level.SEVERE, null, ex);
        }
        return detDiff;
    }

    private void inserIntoMySql(SanPham sp) {
        Connection conn = null;
        PreparedStatement preparedStatement = null;

        try {

            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t1907a", "root", "");
            //query

            String sql = "insert into sanpham(idSp,idDM,nameSp,price,inputDate,outputDate,expiryDate,mota)values (?,?,?,?,?,?,?,?)";

            //  System.out.println("Chay qua day");
            preparedStatement = conn.prepareStatement(sql);

            preparedStatement.setString(1, sp.getIdSp());
            preparedStatement.setString(2, sp.getMaDanhMuc());
            preparedStatement.setString(3, sp.getNameSp());
            preparedStatement.setString(4, sp.getPrice());
            preparedStatement.setString(5, sp.getInputdate());
            preparedStatement.setString(6, sp.getOutputdate());
            preparedStatement.setString(7, sp.getHsd());
            preparedStatement.setString(8, sp.getMota());
            preparedStatement.execute();

        } catch (SQLException ex) {
            Logger.getLogger(QuanLiSp.class
                    .getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(QuanLiSp.class
                    .getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();

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

                } catch (SQLException ex) {
                    Logger.getLogger(QuanLiSp.class
                            .getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        // System.err.println("Chay qua day");
    }

    private void readData() {
        listSanPham.clear();
        Connection conn = null;
        Statement statement = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t1907a", "root", "");
            String sql = "select * from sanpham";

            statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            //doc du lieu từng hàng trong mysql
            while (resultSet.next()) {

                SanPham sp = new SanPham(resultSet.getString("idSp"),
                        resultSet.getString("idDM"),
                        resultSet.getString("nameSp"),
                        resultSet.getString("price"),
                        resultSet.getString("inputDate"),
                        resultSet.getString("outputDate"),
                        resultSet.getString("expiryDate"),
                        resultSet.getString("mota"));
                listSanPham.add(sp);

            }
        } catch (SQLException ex) {
            Logger.getLogger(QuanLiSp.class
                    .getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(QuanLiSp.class
                    .getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (conn != null) {
                try {
                    conn.close();

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

                } catch (SQLException ex) {
                    Logger.getLogger(QuanLiSp.class
                            .getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        display(listSanPham);
    }

    private void delete(String data) {
        Connection conn = null;
        Statement statement = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t1907a", "root", "");

            String sql = "DELETE FROM sanpham WHERE idSp =" + "'" + data + "'";

            statement = conn.createStatement();
            statement.executeUpdate(sql);
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(QuanLiSp.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(QuanLiSp.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(QuanLiSp.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public void display(List<SanPham> list) {
        tableModel.setRowCount(0);
        for (SanPham sp : list) {
            Vector vtdata = new Vector();
            vtdata.add(sp.getIdSp());
            vtdata.add(sp.getMaDanhMuc());
            vtdata.add(sp.getNameSp());
            vtdata.add(sp.getPrice());
            vtdata.add(sp.getInputdate());
            vtdata.add(sp.getOutputdate());
            vtdata.add(sp.getHsd());
            vtdata.add(sp.getMota());
            tableModel.addRow(vtdata);
        }
        for (SanPham list1 : list) {
            list1.display();
        }
    }

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

    // Variables declaration - do not modify                     
    private javax.swing.JButton btAllSanpham;
    private javax.swing.JButton btDelete;
    private javax.swing.JButton btFind;
    private javax.swing.JButton btReset;
    private javax.swing.JButton btSanphamHetHsd;
    private javax.swing.JButton btSaphetHSd;
    private javax.swing.JButton btSave;
    private javax.swing.JButton btSpSell;
    private javax.swing.JButton btTonkho;
    private javax.swing.JButton btUpdate;
    private javax.swing.JButton btdanhmuc;
    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 showthongtin;
    private javax.swing.JTextField tfHSd;
    private javax.swing.JTextField tfIDDM;
    private javax.swing.JTextField tfIDSp;
    private javax.swing.JTextField tfMota;
    private javax.swing.JTextField tfNameSp;
    private javax.swing.JTextField tfOutputDate;
    private javax.swing.JTextField tfPrice;
    private javax.swing.JTextField tfinputDate;
    // 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 QuanLiSanPham;

/**
 *
 * @author Thien Phu
 */
public class login {

    int rollNo;
    String username, pass;

    public login() {
    }

    public login(int rollNo, String username, String pass) {
        this.rollNo = rollNo;
        this.username = username;
        this.pass = pass;
    }

    public int getRollNo() {
        return rollNo;
    }

    public void setRollNo(int rollNo) {
        this.rollNo = rollNo;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    @Override
    public String toString() {
        return "login{" + "rollNo=" + rollNo + ", username=" + username + ", pass=" + pass + '}';
    }

    void display() {
        System.out.println(toString());
    }
}



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

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Thien Phu
 */
public class DangNhap extends javax.swing.JFrame {

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

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tfusername = new javax.swing.JTextField();
        tfpassword = new javax.swing.JTextField();
        btDangNhap = new javax.swing.JButton();
        btExit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Đăng nhập hệ thống", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 18))); // NOI18N

        jLabel1.setText("UserName");

        jLabel2.setText("Pass word");

        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(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(tfpassword, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(tfusername, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfusername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfpassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(38, Short.MAX_VALUE))
        );

        btDangNhap.setText("Dang Nhap");
        btDangNhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDangNhapActionPerformed(evt);
            }
        });

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

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(20, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btDangNhap)
                        .addGap(33, 33, 33)
                        .addComponent(btExit))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btDangNhap)
                    .addComponent(btExit))
                .addContainerGap(19, Short.MAX_VALUE))
        );

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

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

    private void btExitActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        System.exit(0);
    }                                      
    private void readData() {
        login lg = new login();
        Connection conn = null;
        Statement statement = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t1907a", "root", "");
            String sql = "select * from login";

            statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            //doc du lieu từng hàng trong mysql
            while (resultSet.next()) {

                lg.setRollNo(Integer.parseInt(resultSet.getString("id")));
                lg.setUsername(resultSet.getString("username"));
                lg.setPass(resultSet.getString("pass"));
                ;

            }
        } catch (SQLException ex) {
            Logger.getLogger(DangNhap.class
                    .getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DangNhap.class
                    .getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (conn != null) {
                try {
                    conn.close();

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

                } catch (SQLException ex) {
                    Logger.getLogger(DangNhap.class
                            .getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        if (tfusername.getText().equals("") || tfpassword.getText().equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Nhập dữ liệu");
        } else {
            if (lg.getUsername().equals(tfusername.getText()) && lg.getPass().equals(tfpassword.getText())) {
                QuanLiSp sp = new QuanLiSp();
                sp.setVisible(true);
                this.setVisible(false);
            } else {
                JOptionPane.showConfirmDialog(rootPane, "Nhập sai mời nhập lại");
            }
        }

    }

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

    // Variables declaration - do not modify                     
    private javax.swing.JButton btDangNhap;
    private javax.swing.JButton btExit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField tfpassword;
    private javax.swing.JTextField tfusername;
    // End of variables declaration                   
}


Tài Liệu Tham Khảo

Đã sao chép!!!