View.QuanLiSach.java Source code

Java tutorial

Introduction

Here is the source code for View.QuanLiSach.java

Source

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

import Model.DBConnection;
import com.itextpdf.text.PageSize;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author hctha_000
 */
public class QuanLiSach extends javax.swing.JFrame {

    DefaultTableModel dm;
    int error = -1;
    boolean checkFirstColumn = false;

    public QuanLiSach() {
        initComponents();
        setTitle("Sch - Phn m?m qun l th vin");
        setExtendedState(MAXIMIZED_BOTH);
        this.setVisible(true);
        refreshSachTable();
        refreshQuyenTable();
    }

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

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        SachTable = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        QuyenTable = new javax.swing.JTable();
        jLabel11 = new javax.swing.JLabel();
        jTextField_MaQuyen = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jTextField_MaSach1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField_MaSach = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jTextField_TenSach = new javax.swing.JTextField();
        jTextField_Gia = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField_Nxb = new javax.swing.JTextField();
        jTextField_NamXb = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTextField_TacGia = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jTextField_TheLoai = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jTextField_SoTrang = new javax.swing.JTextField();
        buttonRefresh = new javax.swing.JButton();
        buttonTimKiem = new javax.swing.JButton();
        buttonXoa = new javax.swing.JButton();
        buttonChinhSua = new javax.swing.JButton();
        buttonThemSach = new javax.swing.JButton();
        buttonClearText = new javax.swing.JButton();
        buttonLogout = new javax.swing.JButton();
        buttonTurnBack = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jComboBox_TrangThai = new javax.swing.JComboBox<>();
        jTextField_NgayNhap = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        buttonChinhSua1 = new javax.swing.JButton();
        buttonThemSach1 = new javax.swing.JButton();
        buttonXoa1 = new javax.swing.JButton();
        buttonTimKiem1 = new javax.swing.JButton();
        buttonRefresh1 = new javax.swing.JButton();
        buttonClearText1 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel13 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

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

        }, new String[] { "M Sch", "Tn Sch", "Gi (VN?)", "Nh Xut Bn", "Nm Xut Bn",
                "Tc Gi", "Th Loi", "S Trang" }));
        SachTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                SachTableMouseReleased(evt);
            }
        });
        SachTable.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                SachTableKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(SachTable);

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

        }, new String[] { "M Sch", "M Quyn", "Ngy Nhp", "Trng Thi" }));
        QuyenTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                QuyenTableMouseReleased(evt);
            }
        });
        QuyenTable.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                QuyenTableKeyReleased(evt);
            }
        });
        jScrollPane2.setViewportView(QuyenTable);

        jLabel11.setText("M Quyn");
        jLabel11.setMaximumSize(new java.awt.Dimension(40, 14));
        jLabel11.setMinimumSize(new java.awt.Dimension(40, 14));
        jLabel11.setPreferredSize(new java.awt.Dimension(40, 14));

        jTextField_MaQuyen.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField_MaQuyenKeyPressed(evt);
            }
        });

        jLabel12.setText("M Sch");

        jTextField_MaSach1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField_MaSach1KeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 841,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup().addGap(18, 18, 18).addComponent(
                                        jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 463,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel1Layout.createSequentialGroup().addGap(87, 87, 87)
                                        .addGroup(jPanel1Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(jPanel1Layout.createSequentialGroup()
                                                        .addComponent(jLabel11,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 96,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addGap(18, 18, 18).addComponent(jTextField_MaQuyen,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 163,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGroup(jPanel1Layout.createSequentialGroup()
                                                        .addComponent(jLabel12,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 96,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addGap(18, 18, 18).addComponent(jTextField_MaSach1,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 163,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addContainerGap(48, Short.MAX_VALUE)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 290,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 193,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 27,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jTextField_MaSach1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                31, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 27,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jTextField_MaQuyen, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                31, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addContainerGap()));

        getContentPane().add(jPanel1);
        jPanel1.setBounds(10, 11, 1380, 310);

        jLabel3.setText("M Sch");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(20, 360, 96, 27);

        jTextField_MaSach.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField_MaSachKeyPressed(evt);
            }
        });
        getContentPane().add(jTextField_MaSach);
        jTextField_MaSach.setBounds(140, 360, 163, 31);

        jLabel1.setText("Tn Sch");
        jLabel1.setMaximumSize(new java.awt.Dimension(40, 14));
        jLabel1.setMinimumSize(new java.awt.Dimension(40, 14));
        jLabel1.setPreferredSize(new java.awt.Dimension(40, 14));
        getContentPane().add(jLabel1);
        jLabel1.setBounds(20, 400, 96, 27);

        jTextField_TenSach.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField_TenSachKeyPressed(evt);
            }
        });
        getContentPane().add(jTextField_TenSach);
        jTextField_TenSach.setBounds(140, 400, 163, 31);

        jTextField_Gia.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField_GiaKeyPressed(evt);
            }
        });
        getContentPane().add(jTextField_Gia);
        jTextField_Gia.setBounds(140, 440, 163, 31);

        jLabel2.setText("Gi (VN?)");
        jLabel2.setMaximumSize(new java.awt.Dimension(40, 14));
        jLabel2.setMinimumSize(new java.awt.Dimension(40, 14));
        jLabel2.setPreferredSize(new java.awt.Dimension(40, 14));
        getContentPane().add(jLabel2);
        jLabel2.setBounds(20, 440, 96, 27);

        jLabel4.setText("Nh xut bn");
        jLabel4.setMaximumSize(new java.awt.Dimension(40, 14));
        jLabel4.setMinimumSize(new java.awt.Dimension(40, 14));
        jLabel4.setPreferredSize(new java.awt.Dimension(40, 14));
        getContentPane().add(jLabel4);
        jLabel4.setBounds(20, 480, 96, 27);

        jTextField_Nxb.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField_NxbKeyPressed(evt);
            }
        });
        getContentPane().add(jTextField_Nxb);
        jTextField_Nxb.setBounds(140, 480, 163, 31);

        jTextField_NamXb.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField_NamXbKeyPressed(evt);
            }
        });
        getContentPane().add(jTextField_NamXb);
        jTextField_NamXb.setBounds(470, 360, 163, 31);

        jLabel5.setText("Nm xut bn");
        jLabel5.setMaximumSize(new java.awt.Dimension(40, 14));
        jLabel5.setMinimumSize(new java.awt.Dimension(40, 14));
        jLabel5.setPreferredSize(new java.awt.Dimension(40, 14));
        getContentPane().add(jLabel5);
        jLabel5.setBounds(350, 360, 96, 27);

        jLabel6.setText("Tc gi");
        jLabel6.setMaximumSize(new java.awt.Dimension(40, 14));
        jLabel6.setMinimumSize(new java.awt.Dimension(40, 14));
        jLabel6.setPreferredSize(new java.awt.Dimension(40, 14));
        getContentPane().add(jLabel6);
        jLabel6.setBounds(350, 410, 96, 27);

        jTextField_TacGia.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField_TacGiaKeyPressed(evt);
            }
        });
        getContentPane().add(jTextField_TacGia);
        jTextField_TacGia.setBounds(470, 400, 163, 31);

        jLabel8.setText("Th loi");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(350, 450, 96, 27);

        jTextField_TheLoai.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField_TheLoaiKeyPressed(evt);
            }
        });
        getContentPane().add(jTextField_TheLoai);
        jTextField_TheLoai.setBounds(470, 450, 163, 31);

        jLabel7.setText("S trang");
        jLabel7.setMaximumSize(new java.awt.Dimension(40, 14));
        jLabel7.setMinimumSize(new java.awt.Dimension(40, 14));
        jLabel7.setPreferredSize(new java.awt.Dimension(40, 14));
        getContentPane().add(jLabel7);
        jLabel7.setBounds(350, 490, 96, 27);

        jTextField_SoTrang.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField_SoTrangKeyPressed(evt);
            }
        });
        getContentPane().add(jTextField_SoTrang);
        jTextField_SoTrang.setBounds(470, 490, 163, 31);

        buttonRefresh.setText("Refresh");
        buttonRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonRefreshActionPerformed(evt);
            }
        });
        getContentPane().add(buttonRefresh);
        buttonRefresh.setBounds(80, 620, 120, 40);

        buttonTimKiem.setText("Tm kim");
        buttonTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTimKiemActionPerformed(evt);
            }
        });
        getContentPane().add(buttonTimKiem);
        buttonTimKiem.setBounds(230, 560, 110, 40);

        buttonXoa.setText("Xa");
        buttonXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonXoaActionPerformed(evt);
            }
        });
        getContentPane().add(buttonXoa);
        buttonXoa.setBounds(390, 560, 110, 40);

        buttonChinhSua.setText("Chnh sa");
        buttonChinhSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonChinhSuaActionPerformed(evt);
            }
        });
        getContentPane().add(buttonChinhSua);
        buttonChinhSua.setBounds(80, 560, 120, 40);

        buttonThemSach.setText("Thm sch");
        buttonThemSach.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonThemSachActionPerformed(evt);
            }
        });
        getContentPane().add(buttonThemSach);
        buttonThemSach.setBounds(540, 560, 110, 40);

        buttonClearText.setText("Xa ch");
        buttonClearText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClearTextActionPerformed(evt);
            }
        });
        getContentPane().add(buttonClearText);
        buttonClearText.setBounds(230, 620, 110, 40);

        buttonLogout.setText("?ng xut");
        buttonLogout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonLogoutActionPerformed(evt);
            }
        });
        getContentPane().add(buttonLogout);
        buttonLogout.setBounds(690, 670, 90, 40);

        buttonTurnBack.setText("Back");
        buttonTurnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTurnBackActionPerformed(evt);
            }
        });
        getContentPane().add(buttonTurnBack);
        buttonTurnBack.setBounds(790, 670, 90, 40);

        jLabel9.setText("Trng Thi");
        jLabel9.setMaximumSize(new java.awt.Dimension(40, 14));
        jLabel9.setMinimumSize(new java.awt.Dimension(40, 14));
        jLabel9.setPreferredSize(new java.awt.Dimension(40, 14));
        getContentPane().add(jLabel9);
        jLabel9.setBounds(940, 390, 96, 27);

        jComboBox_TrangThai
                .setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "null", "Cn", "? Mun" }));
        getContentPane().add(jComboBox_TrangThai);
        jComboBox_TrangThai.setBounds(1060, 390, 163, 34);

        jTextField_NgayNhap.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField_NgayNhapKeyPressed(evt);
            }
        });
        getContentPane().add(jTextField_NgayNhap);
        jTextField_NgayNhap.setBounds(1060, 330, 163, 31);

        jLabel10.setText("Ngy Nhp");
        jLabel10.setMaximumSize(new java.awt.Dimension(40, 14));
        jLabel10.setMinimumSize(new java.awt.Dimension(40, 14));
        jLabel10.setPreferredSize(new java.awt.Dimension(40, 14));
        getContentPane().add(jLabel10);
        jLabel10.setBounds(940, 330, 96, 27);

        buttonChinhSua1.setText("Chnh sa");
        buttonChinhSua1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonChinhSua1ActionPerformed(evt);
            }
        });
        getContentPane().add(buttonChinhSua1);
        buttonChinhSua1.setBounds(930, 450, 120, 40);

        buttonThemSach1.setText("Thm quyn");
        buttonThemSach1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonThemSach1ActionPerformed(evt);
            }
        });
        getContentPane().add(buttonThemSach1);
        buttonThemSach1.setBounds(1090, 450, 110, 40);

        buttonXoa1.setText("Xa");
        buttonXoa1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonXoa1ActionPerformed(evt);
            }
        });
        getContentPane().add(buttonXoa1);
        buttonXoa1.setBounds(1240, 450, 100, 40);

        buttonTimKiem1.setText("Tm kim");
        buttonTimKiem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTimKiem1ActionPerformed(evt);
            }
        });
        getContentPane().add(buttonTimKiem1);
        buttonTimKiem1.setBounds(930, 510, 110, 40);

        buttonRefresh1.setText("Refresh");
        buttonRefresh1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonRefresh1ActionPerformed(evt);
            }
        });
        getContentPane().add(buttonRefresh1);
        buttonRefresh1.setBounds(1090, 510, 120, 40);

        buttonClearText1.setText("Xa ch");
        buttonClearText1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClearText1ActionPerformed(evt);
            }
        });
        getContentPane().add(buttonClearText1);
        buttonClearText1.setBounds(1240, 510, 100, 40);

        jButton1.setText("Xut File");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(390, 620, 110, 40);
        getContentPane().add(jLabel13);
        jLabel13.setBounds(20, 320, 220, 20);

        jButton2.setText("Thm Sch t file");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(530, 620, 120, 40);

        jButton3.setText("Thm Quyn t file");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3);
        jButton3.setBounds(980, 560, 300, 50);

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

    private void jTextField_MaSachKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_MaSachKeyPressed
        // TODO add your handling code here:

        if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
            jTextField_TenSach.requestFocus();
        }
        if (evt.getKeyCode() == KeyEvent.VK_UP) {
            jTextField_Nxb.requestFocus();
        }
        if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
            jTextField_NamXb.requestFocus();
        }
    }//GEN-LAST:event_jTextField_MaSachKeyPressed

    private void jTextField_TenSachKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_TenSachKeyPressed
        // TODO add your handling code here:

        if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
            jTextField_Gia.requestFocus();
        }
        if (evt.getKeyCode() == KeyEvent.VK_UP) {
            jTextField_MaSach.requestFocus();
        }
        if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
            jTextField_TacGia.requestFocus();
        }
    }//GEN-LAST:event_jTextField_TenSachKeyPressed

    private void jTextField_GiaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_GiaKeyPressed
        // TODO add your handling code here:
        // TODO add your handling code here:

        if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
            jTextField_Nxb.requestFocus();
        }
        if (evt.getKeyCode() == KeyEvent.VK_UP) {
            jTextField_TenSach.requestFocus();
        }
        if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
            jTextField_TheLoai.requestFocus();
        }
    }//GEN-LAST:event_jTextField_GiaKeyPressed

    private void jTextField_NxbKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_NxbKeyPressed
        // TODO add your handling code here:

        if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
            jTextField_MaSach.requestFocus();
        }
        if (evt.getKeyCode() == KeyEvent.VK_UP) {
            jTextField_Gia.requestFocus();
        }
        if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
            jTextField_SoTrang.requestFocus();
        }
    }//GEN-LAST:event_jTextField_NxbKeyPressed

    private void jTextField_NamXbKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_NamXbKeyPressed
        // TODO add your handling code here:

        if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
            jTextField_TacGia.requestFocus();
        }
        if (evt.getKeyCode() == KeyEvent.VK_UP) {
            jTextField_SoTrang.requestFocus();
        }
        if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
            jTextField_MaSach.requestFocus();
        }
    }//GEN-LAST:event_jTextField_NamXbKeyPressed

    private void jTextField_TacGiaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_TacGiaKeyPressed
        // TODO add your handling code here:

        if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
            jTextField_TheLoai.requestFocus();
        }
        if (evt.getKeyCode() == KeyEvent.VK_UP) {
            jTextField_NamXb.requestFocus();
        }
        if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
            jTextField_TenSach.requestFocus();
        }
    }//GEN-LAST:event_jTextField_TacGiaKeyPressed

    private void jTextField_TheLoaiKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_TheLoaiKeyPressed
        // TODO add your handling code here:

        if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
            jTextField_SoTrang.requestFocus();
        }
        if (evt.getKeyCode() == KeyEvent.VK_UP) {
            jTextField_TacGia.requestFocus();
        }
        if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
            jTextField_Gia.requestFocus();
        }
    }//GEN-LAST:event_jTextField_TheLoaiKeyPressed

    private void jTextField_SoTrangKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_SoTrangKeyPressed
        // TODO add your handling code here:

        if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
            jTextField_SoTrang.requestFocus();
        }
        if (evt.getKeyCode() == KeyEvent.VK_UP) {
            jTextField_TacGia.requestFocus();
        }
        if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
            jTextField_Nxb.requestFocus();
        }
    }//GEN-LAST:event_jTextField_SoTrangKeyPressed

    private void buttonRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRefreshActionPerformed
        // TODO add your handling code here:
        refreshSachTable();
        resetTable(QuyenTable);
        refreshQuyenTable();
        jLabel10.setText("");
    }//GEN-LAST:event_buttonRefreshActionPerformed

    private void buttonTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTimKiemActionPerformed
        searchSach(getNoiDungTextField(0));

    }//GEN-LAST:event_buttonTimKiemActionPerformed

    private void buttonXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonXoaActionPerformed
        // TODO add your handling code here:
        xoaSach();
    }//GEN-LAST:event_buttonXoaActionPerformed

    private void buttonChinhSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonChinhSuaActionPerformed
        // TODO add your handling code here:
        editSach(getNoiDungTextField(0));
    }//GEN-LAST:event_buttonChinhSuaActionPerformed

    private void buttonThemSachActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonThemSachActionPerformed
        // TODO add your handling code here:
        addSach(getNoiDungTextField(0));
    }//GEN-LAST:event_buttonThemSachActionPerformed

    private void buttonClearTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonClearTextActionPerformed
        // TODO add your handling code here:
        setNoiDungTextField(0, new String[] { "", "", "", "", "", "", "", "" }); //Sach
    }//GEN-LAST:event_buttonClearTextActionPerformed

    private void buttonLogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonLogoutActionPerformed
        // TODO add your handling code here:
        this.dispose();
        LoginForm newlogin = new LoginForm();
        newlogin.showWindows();
    }//GEN-LAST:event_buttonLogoutActionPerformed

    private void buttonTurnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTurnBackActionPerformed
        // TODO add your handling code here:
        this.dispose();
        LibrarianForm newLibrarianForm = new LibrarianForm();
        newLibrarianForm.showWindows();
    }//GEN-LAST:event_buttonTurnBackActionPerformed

    private void jTextField_NgayNhapKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_NgayNhapKeyPressed
        // TODO add your handling code here:
        // TODO add your handling code here:

        if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
            jTextField_MaSach1.requestFocus();
        }
        if (evt.getKeyCode() == KeyEvent.VK_UP) {
            jTextField_MaQuyen.requestFocus();
        }
    }//GEN-LAST:event_jTextField_NgayNhapKeyPressed

    private void jTextField_MaQuyenKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_MaQuyenKeyPressed
        // TODO add your handling code here:

        if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
            jTextField_NgayNhap.requestFocus();
        }
        if (evt.getKeyCode() == KeyEvent.VK_UP) {
            jTextField_MaSach1.requestFocus();
        }
    }//GEN-LAST:event_jTextField_MaQuyenKeyPressed

    private void jTextField_MaSach1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_MaSach1KeyPressed
        // TODO add your handling code here:

        if (evt.getKeyCode() == KeyEvent.VK_UP) {
            jTextField_NgayNhap.requestFocus();
        }

        if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
            jTextField_MaQuyen.requestFocus();
        }
    }//GEN-LAST:event_jTextField_MaSach1KeyPressed

    private void buttonChinhSua1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonChinhSua1ActionPerformed
        // TODO add your handling code here:
        editQuyen(getNoiDungTextField(1));
    }//GEN-LAST:event_buttonChinhSua1ActionPerformed

    private void buttonThemSach1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonThemSach1ActionPerformed
        // TODO add your handling code here:

        addQuyen(getNoiDungTextField(1));
    }//GEN-LAST:event_buttonThemSach1ActionPerformed

    private void buttonXoa1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonXoa1ActionPerformed
        xoaQuyen();

    }//GEN-LAST:event_buttonXoa1ActionPerformed

    private void buttonTimKiem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTimKiem1ActionPerformed
        searchQuyen(1, getNoiDungTextField(1));
    }//GEN-LAST:event_buttonTimKiem1ActionPerformed

    private void buttonRefresh1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRefresh1ActionPerformed
        // TODO add your handling code here:
        resetTable(QuyenTable);
        refreshQuyenTable();

    }//GEN-LAST:event_buttonRefresh1ActionPerformed

    private void buttonClearText1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonClearText1ActionPerformed
        // TODO add your handling code here:
        setNoiDungTextField(1, new String[] { "", "", "", "" }); //Quyen
    }//GEN-LAST:event_buttonClearText1ActionPerformed

    private void SachTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_SachTableMouseReleased
        getNoiDungtuSachTable();
        chiTietSach();
    }//GEN-LAST:event_SachTableMouseReleased

    private void SachTableKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_SachTableKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_UP || evt.getKeyCode() == KeyEvent.VK_DOWN) {
            getNoiDungtuSachTable();
            chiTietSach();
        }
    }//GEN-LAST:event_SachTableKeyReleased

    private void QuyenTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_QuyenTableMouseReleased
        getNoiDungtuQuyenTable();
    }//GEN-LAST:event_QuyenTableMouseReleased

    private void QuyenTableKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_QuyenTableKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_UP || evt.getKeyCode() == KeyEvent.VK_DOWN) {
            getNoiDungtuQuyenTable();
        }
    }//GEN-LAST:event_QuyenTableKeyReleased

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
        addtuFileTxt newaddSachtuFileTxt = new addtuFileTxt();
        newaddSachtuFileTxt.showWindows();
        newaddSachtuFileTxt.type = 1;
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        this.dispose();
        addtuFileTxt newaddQuyentuFileTxt = new addtuFileTxt();
        newaddQuyentuFileTxt.showWindows();
        newaddQuyentuFileTxt.type = 2;
    }//GEN-LAST:event_jButton3ActionPerformed

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

        xuatFile();
    }//GEN-LAST:event_jButton1ActionPerformed

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable QuyenTable;
    private javax.swing.JTable SachTable;
    private javax.swing.JButton buttonChinhSua;
    private javax.swing.JButton buttonChinhSua1;
    private javax.swing.JButton buttonClearText;
    private javax.swing.JButton buttonClearText1;
    private javax.swing.JButton buttonLogout;
    private javax.swing.JButton buttonRefresh;
    private javax.swing.JButton buttonRefresh1;
    private javax.swing.JButton buttonThemSach;
    private javax.swing.JButton buttonThemSach1;
    private javax.swing.JButton buttonTimKiem;
    private javax.swing.JButton buttonTimKiem1;
    private javax.swing.JButton buttonTurnBack;
    private javax.swing.JButton buttonXoa;
    private javax.swing.JButton buttonXoa1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox<String> jComboBox_TrangThai;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField_Gia;
    private javax.swing.JTextField jTextField_MaQuyen;
    private javax.swing.JTextField jTextField_MaSach;
    private javax.swing.JTextField jTextField_MaSach1;
    private javax.swing.JTextField jTextField_NamXb;
    private javax.swing.JTextField jTextField_NgayNhap;
    private javax.swing.JTextField jTextField_Nxb;
    private javax.swing.JTextField jTextField_SoTrang;
    private javax.swing.JTextField jTextField_TacGia;
    private javax.swing.JTextField jTextField_TenSach;
    private javax.swing.JTextField jTextField_TheLoai;
    // End of variables declaration//GEN-END:variables

    private void refreshSachTable() {
        try {
            dm = (DefaultTableModel) SachTable.getModel();
            resetTable(SachTable);
            String sqlQuerty = "SELECT * FROM Sach ;";
            DBConnection dbRefreshTable = new DBConnection();
            dbRefreshTable.getConnection();
            dbRefreshTable.excuteQuery(sqlQuerty);
            System.out.println(sqlQuerty);
            while (dbRefreshTable.result.next()) {
                String[] rowData = { dbRefreshTable.result.getString("MaSach"),
                        dbRefreshTable.result.getString("TenSach"), dbRefreshTable.result.getString("Gia"),
                        dbRefreshTable.result.getString("Nxb"), dbRefreshTable.result.getString("NamXB"),
                        dbRefreshTable.result.getString("TacGia"), dbRefreshTable.result.getString("TheLoai"),
                        dbRefreshTable.result.getString("SoTrang") };

                dm.addRow(rowData);

            }
            dbRefreshTable.freeConnection();
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "? c li xy ra +RefreshSachTable", "ERROR", 2);
        }
        jLabel13.setText("");
    }

    private void refreshQuyenTable() {
        try {
            dm = (DefaultTableModel) QuyenTable.getModel();
            resetTable(QuyenTable);
            String sqlQuerty = "SELECT * FROM ChiTietSach ;";
            DBConnection dbRefreshTable = new DBConnection();
            dbRefreshTable.getConnection();
            dbRefreshTable.excuteQuery(sqlQuerty);
            System.out.println(sqlQuerty);
            while (dbRefreshTable.result.next()) {

                String[] rowData = { dbRefreshTable.result.getString("MaSach"),
                        dbRefreshTable.result.getString("MaQuyen"), dbRefreshTable.result.getString("NgayNhap"),
                        dbRefreshTable.result.getString("TrangThai") };

                dm.addRow(rowData);

            }
            dbRefreshTable.freeConnection();
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "? c li xy ra +RefreshQuyenTable", "ERROR", 2);
        }
    }

    private void resetTable(JTable table) {
        dm = (DefaultTableModel) table.getModel();
        int rowCount = dm.getRowCount();
        for (int i = rowCount - 1; i >= 0; i--) {
            dm.removeRow(i);
        }
    }

    private String[] getNoiDungTextField(int getType) { //getType = 0 : Sach, getType = 1 : quyen
        if (getType == 0) {
            String[] NoiDungTextField = new String[8];
            NoiDungTextField[0] = jTextField_MaSach.getText().trim();
            NoiDungTextField[1] = jTextField_TenSach.getText().trim();
            NoiDungTextField[2] = jTextField_Gia.getText().trim();
            NoiDungTextField[3] = jTextField_Nxb.getText().trim();
            NoiDungTextField[4] = jTextField_NamXb.getText().trim();
            NoiDungTextField[5] = jTextField_TacGia.getText().trim();
            NoiDungTextField[6] = jTextField_TheLoai.getText().trim();
            NoiDungTextField[7] = jTextField_SoTrang.getText().trim();
            return NoiDungTextField;
        }
        if (getType == 1) {
            String[] NoiDungTextField = new String[4];
            NoiDungTextField[0] = jTextField_MaSach1.getText().trim();
            NoiDungTextField[1] = jTextField_MaQuyen.getText().trim();
            NoiDungTextField[2] = jTextField_NgayNhap.getText().trim();

            if (((String) jComboBox_TrangThai.getSelectedItem()).equals("null"))
                NoiDungTextField[3] = "";
            else
                NoiDungTextField[3] = (String) jComboBox_TrangThai.getSelectedItem();

            return NoiDungTextField;
        }
        return null;
    }

    private void searchSach(String[] input) {
        try {
            resetTable(QuyenTable);
            if (checkInputSach(0, input)) {

                String[] sqlInput = setSqlInputSach(1, input);
                String sqlSearch = "SELECT * FROM Sach WHERE ";
                for (int i = 0; i < 8; i++) {
                    sqlSearch += sqlInput[i];
                }
                sqlSearch += ";";

                DBConnection SearchSach = new DBConnection();
                SearchSach.getConnection();
                System.out.println(sqlSearch);
                SearchSach.excuteQuery(sqlSearch);
                int soSach = 0;
                while (SearchSach.result.next()) {
                    soSach++;
                }

                if (soSach == 0) {
                    JOptionPane.showMessageDialog(null, "Khng c quyn sch no c!!!");
                } else {
                    jLabel13.setText("C " + soSach + " bn ghi c tm thy");
                    dm = (DefaultTableModel) SachTable.getModel();
                    resetTable(SachTable);
                    SearchSach.result.beforeFirst();
                    String[] maSach = new String[soSach];
                    while (SearchSach.result.next()) {
                        String[] rowData = { SearchSach.result.getString("MaSach"),
                                SearchSach.result.getString("TenSach"), SearchSach.result.getString("Gia"),
                                SearchSach.result.getString("Nxb"), SearchSach.result.getString("NamXB"),
                                SearchSach.result.getString("TacGia"), SearchSach.result.getString("TheLoai"),
                                SearchSach.result.getString("SoTrang") };
                        dm.addRow(rowData);
                    }

                }
            }

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "? c li xy ra +searchSach", "ERROR", 2);
        } finally {

            resetTable(QuyenTable);
            refreshQuyenTable();
        }
    }

    public boolean checkInputSach(int checkType, String[] input) { // false : c li, true : khng c li
        // checkType = 0 : check ca Tm kim sch
        // checkType = 1 : check ca Thm sch hoc Chnh sa sch
        // checkType = 2 : check ly error

        boolean check = false;
        for (int i = 0; i < 8; i++) {
            if (!input[i].equals(""))
                check = true;
        }
        if (check == false) {
            if (checkType != 2)
                JOptionPane.showMessageDialog(null, "Bn cha nhp thng tin no!!");
            error = 0;
            return false;
        }
        if (input[0].equals("") && checkType != 0) {
            JOptionPane.showMessageDialog(null, "Bn cha nhp m sch!!");
            return false;
        }
        if (input[1].equals("") && checkType != 0) {
            JOptionPane.showMessageDialog(null, "Bn cha nhp tn sch!!");
            return false;
        }

        if (!Model.ThuVien.checkNumeric(input[2]) && !input[2].equals("")) {
            JOptionPane.showMessageDialog(null, "Thng tin Gi bn nhp khng hp l!!");
            return false;
        }
        if (!Model.ThuVien.checkNumeric(input[4]) && !input[4].equals("")) {
            JOptionPane.showMessageDialog(null, "Thng tin Nm xut bn bn nhp khng hp l!!");
            return false;
        }
        if (!Model.ThuVien.checkNumeric(input[7]) && !input[7].equals("")) {
            JOptionPane.showMessageDialog(null, "Thng tin S trang bn nhp khng hp l!!");
            return false;
        }
        return true;
    }

    public boolean checkInputQuyen(int checkType, String[] input) { // false : c li, true : khng c li
        // checkType = 0 : check ca Tm kim quyen
        // checkType = 1 : check ca Thm quyen hoc Chnh sa quyen
        // checkType = 2 : check ly error

        if (input[0].equals("") && input[1].equals("") && input[2].equals("") && input[3].equals("")) {
            if (checkType != 2)
                JOptionPane.showMessageDialog(null, "Quyen Bn cha nhp thng tin no!!");
            error = 0;
            return false;
        }
        if (input[1].equals("") && checkType == 1) {
            JOptionPane.showMessageDialog(null, "Bn cha nhp m quyn!!");
            return false;
        }
        if (input[0].equals("") && checkType == 1) {
            JOptionPane.showMessageDialog(null, "Bn cha nhp m sch!!");
            return false;
        }
        if (!Model.ThuVien.checkValidDate(input[2]) && !input[2].equals("")) {
            JOptionPane.showMessageDialog(null, "Thng tin Ngy Nhp bn nhp khng hp l!!");
            return false;
        }

        if (checkMaQuyen(input[1]) && !input[1].equals("") && checkType == 1) {
            JOptionPane.showMessageDialog(null,
                    "Thng tin m quyn bn nhp  tn ti. Vui lng nhp m quyn khc!!");
            return false;
        }
        if (!checkMaSach(input[0]) && !input[0].equals("") && checkType == 1) {
            JOptionPane.showMessageDialog(null,
                    "Thng tin m sch bn nhp cha tn ti. Vui lng to 1 u sch mi!!");
            return false;
        }
        return true; // Khng c li
    }

    private boolean checkMaQuyen(String MaQuyen) {
        // true tng ng vi M Quyn  tn ti
        // false tng ng vi M Quyn cha tn ti

        String sqlCheckMaQuyen = "SELECT * FROM ChiTietSach WHERE (MaQuyen = '" + MaQuyen + "');";
        DBConnection checkMaQuyen = new DBConnection();
        checkMaQuyen.getConnection();
        checkMaQuyen.excuteQuery(sqlCheckMaQuyen);
        boolean rs = true;
        try {
            rs = checkMaQuyen.result.next();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "? c li xy ra +checkMaQuyen", "ERROR", 2);
        }
        checkMaQuyen.freeConnection();
        return (rs);
    }

    private boolean checkMaSach(String MaSach) {
        // true tng ng vi M Sch  tn ti
        // false tng ng vi M Sch cha tn ti

        String sqlCheckMaSach = "SELECT * FROM Sach WHERE (MaSach = '" + MaSach + "');";
        DBConnection checkMaSach = new DBConnection();
        checkMaSach.getConnection();
        checkMaSach.excuteQuery(sqlCheckMaSach);
        boolean rs = true;
        try {
            rs = checkMaSach.result.next();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "? c li xy ra +checkMaSach", "ERROR", 2);
        }
        checkMaSach.freeConnection();
        return (rs);
    }

    public String[] setSqlInputSach(int setType, String[] input) {
        //setType = 0 : cu lnh sql chnh sa, setType = 1 : cu lnh sql tm kim
        String[] sqlInput = new String[8];
        checkFirstColumn = false;
        String link = "";
        if (setType == 1)
            link = " AND ";
        if (setType == 0)
            link = " , ";

        if (input[0].equals(""))
            sqlInput[0] = "";
        else {
            sqlInput[0] = "MaSach = '" + input[0] + "'";
            checkFirstColumn = true;
        }
        if (input[1].equals(""))
            sqlInput[1] = "";
        else {
            if (checkFirstColumn) {
                sqlInput[1] = link + "TenSach = '" + input[1] + "'";
            } else {
                sqlInput[1] = "TenSach = '" + input[1] + "'";
                checkFirstColumn = true;
            }
        }
        if (input[2].equals(""))
            sqlInput[2] = "";
        else {
            if (checkFirstColumn) {
                sqlInput[2] = link + "Gia = " + input[2];
            } else {
                sqlInput[2] = "Gia = " + input[2];
                checkFirstColumn = true;
            }
        }
        if (input[3].equals(""))
            sqlInput[3] = "";
        else {
            if (checkFirstColumn) {
                sqlInput[3] = link + "Nxb = '" + input[3] + "'";
            } else {
                sqlInput[3] = "Nxb = '" + input[3] + "'";
                checkFirstColumn = true;
            }
        }
        if (input[4].equals(""))
            sqlInput[4] = "";
        else {
            if (checkFirstColumn) {
                sqlInput[4] = link + "NamXB = " + input[4];
            } else {
                sqlInput[4] = "NamXB = " + input[4];
                checkFirstColumn = true;
            }
        }
        if (input[5].equals(""))
            sqlInput[5] = "";
        else {
            if (checkFirstColumn) {
                sqlInput[5] = link + "TacGia = '" + input[5] + "' ";
            } else {
                sqlInput[5] = "TacGia = '" + input[5] + "' ";
                checkFirstColumn = true;
            }
        }
        if (input[6].equals(""))
            sqlInput[6] = "";
        else {
            if (checkFirstColumn) {
                sqlInput[6] = link + "TheLoai = '" + input[6] + "' ";
            } else {
                sqlInput[6] = "TheLoai = '" + input[6] + "' ";
                checkFirstColumn = true;
            }
        }
        if (input[7].equals(""))
            sqlInput[7] = "";
        else {
            if (checkFirstColumn) {
                sqlInput[7] = link + "SoTrang = " + input[7];
            } else {
                sqlInput[7] = "SoTrang = " + input[7];
            }
        }

        return sqlInput;
    }

    public String[] setSqlInputQuyen(int setType, String[] input) {
        //setType = 0 : cu lnh sql chnh sa, setType = 1 : cu lnh sql tm kim
        checkFirstColumn = false;
        String[] sqlInput = new String[4];
        String link = "";
        if (setType == 1)
            link = " AND ";
        if (setType == 0)
            link = " , ";

        if (input[0].equals(""))
            sqlInput[0] = "";
        else {
            sqlInput[0] = "MaSach = '" + input[0] + "'";
            checkFirstColumn = true;
        }
        if (input[1].equals(""))
            sqlInput[1] = "";
        else {
            if (checkFirstColumn) {
                sqlInput[1] = link + "MaQuyen = '" + input[1] + "'";
            } else {
                sqlInput[1] = "MaQuyen = '" + input[1] + "'";
                checkFirstColumn = true;
            }
        }
        if (input[2].equals(""))
            sqlInput[2] = "";
        else {
            if (checkFirstColumn) {
                sqlInput[2] = link + "NgayNhap = '" + input[2] + "'";
            } else {
                sqlInput[2] = "NgayNhap = '" + input[2] + "'";
                checkFirstColumn = true;
            }
        }
        if (input[3].equals(""))
            sqlInput[3] = "";
        else {
            if (checkFirstColumn) {
                sqlInput[3] = link + "TrangThai = '" + input[3] + "'";
            } else {
                sqlInput[3] = "TrangThai = '" + input[3] + "'";
            }
        }
        return sqlInput;

    }

    public void setNoiDungTextField(int setType, String[] input) { //setType = 0: Sach, setType = 1 : Quyen
        if (setType == 0) {
            jTextField_MaSach.setText(input[0]);
            jTextField_TenSach.setText(input[1]);
            jTextField_Gia.setText(input[2]);
            jTextField_Nxb.setText(input[3]);
            jTextField_NamXb.setText(input[4]);
            jTextField_TacGia.setText(input[5]);
            jTextField_TheLoai.setText(input[6]);
            jTextField_SoTrang.setText(input[7]);
        }
        if (setType == 1) {
            jTextField_MaSach1.setText(input[0]);
            jTextField_MaQuyen.setText(input[1]);
            jTextField_NgayNhap.setText(input[2]);

            if (input[3].trim().equals(""))
                jComboBox_TrangThai.setSelectedItem("null");
            else
                jComboBox_TrangThai.setSelectedItem(input[3]);
        }

    }

    private void getNoiDungtuSachTable() {
        int i = SachTable.getSelectedRow();
        TableModel model = (TableModel) SachTable.getModel();
        String[] input = new String[8];
        input[0] = model.getValueAt(i, 0).toString();
        input[1] = model.getValueAt(i, 1).toString();
        input[2] = model.getValueAt(i, 2).toString();
        input[3] = model.getValueAt(i, 3).toString();
        input[4] = model.getValueAt(i, 4).toString();
        input[5] = model.getValueAt(i, 5).toString();
        input[6] = model.getValueAt(i, 6).toString();
        input[7] = model.getValueAt(i, 7).toString();

        setNoiDungTextField(0, input);
    }

    private void getNoiDungtuQuyenTable() {
        int i = QuyenTable.getSelectedRow();
        TableModel model = (TableModel) QuyenTable.getModel();
        String[] input = new String[4];
        input[0] = model.getValueAt(i, 0).toString();
        input[1] = model.getValueAt(i, 1).toString();
        input[2] = model.getValueAt(i, 2).toString();
        input[3] = model.getValueAt(i, 3).toString();
        setNoiDungTextField(1, input);
    }

    private void chiTietSach() {
        String[] input = new String[4];
        input[0] = jTextField_MaSach.getText();
        input[1] = "";
        input[2] = "";
        input[3] = "";
        error = -1;
        checkInputQuyen(2, input);
        //System.out.println(error);
        if (error == -1)
            searchQuyen(0, input);
        jTextField_MaSach1.setText(input[0]);

    }

    public void searchQuyen(int searchType, String[] input) {//searchType =1 : Tim Kiem Quyen, searchType = 0 : tim nhung ko bao loi
        try {
            resetTable(QuyenTable);
            refreshQuyenTable();
            if (checkInputQuyen(0, input)) {
                String[] sqlInput = setSqlInputQuyen(1, input);
                String sqlSearch = "SELECT * FROM ChiTietSach WHERE " + sqlInput[0] + sqlInput[1] + sqlInput[2]
                        + sqlInput[3] + ";";

                DBConnection SearchQuyen = new DBConnection();
                SearchQuyen.getConnection();
                System.out.println(sqlSearch);
                SearchQuyen.excuteQuery(sqlSearch);

                if (SearchQuyen.result.next() == false && searchType == 1) {
                    JOptionPane.showMessageDialog(null, "Khng c quyn no c!!!");
                } else {
                    dm = (DefaultTableModel) QuyenTable.getModel();
                    resetTable(QuyenTable);
                    SearchQuyen.result.beforeFirst();
                    while (SearchQuyen.result.next()) {
                        String[] rowData = { SearchQuyen.result.getString("MaSach"),
                                SearchQuyen.result.getString("MaQuyen"), SearchQuyen.result.getString("NgayNhap"),
                                SearchQuyen.result.getString("TrangThai") };

                        dm.addRow(rowData);

                    }
                }
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "? c li xy ra +buttonTimKiemQuyen", "ERROR", 2);
        } finally {

        }
    }

    private void addQuyen(String[] input) {
        try {
            if (checkInputQuyen(1, input)) {
                if (checkMaQuyen(input[1]) == true) {
                    JOptionPane.showMessageDialog(null,
                            "M Quyn  tn ti. Vui lng nhp M Quyn khc!", "ERROR", 2);
                } else {
                    String[] sqlInput = new String[4];
                    sqlInput = setSqlInputQuyen(0, input);
                    String sqlAddQuyen = "INSERT INTO ChiTietSach (MaSach, MaQuyen, NgayNhap, TrangThai) VALUES ( '"
                            + input[0] + "','" + input[1] + "', '" + Model.ThuVien.getToday() + "','Cn');";

                    String sqlUpdateQuyen = "UPDATE ChiTietSach SET " + sqlInput[0] + sqlInput[1] + sqlInput[2]
                            + sqlInput[3] + " WHERE MaQuyen = '" + input[1] + "';";
                    DBConnection addQuyen = new DBConnection();
                    addQuyen.getConnection();
                    System.out.println(sqlAddQuyen);
                    addQuyen.state.execute(sqlAddQuyen);
                    System.out.println(sqlUpdateQuyen);
                    addQuyen.state.execute(sqlUpdateQuyen);
                    addQuyen.freeConnection();

                    JOptionPane.showMessageDialog(null, "? thm quyn " + input[1] + " thnh cng");

                }
            }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "? c li xy ra +addQuyen", "ERROR", 2);
        }
        refreshQuyenTable();

    }

    private void editQuyen(String[] input) {
        if (checkInputQuyen(1, input)) {
            if (checkMaSach(input[0]) == true) {
                String[] sqlInput = setSqlInputQuyen(0, input);
                String sqlUpdateSach = "UPDATE ChiTietSach SET " + sqlInput[0] + sqlInput[1] + sqlInput[2]
                        + sqlInput[3] + " WHERE MaSach = '" + input[0] + "';";
                DBConnection updateSach = new DBConnection();
                updateSach.getConnection();
                try {
                    System.out.println(sqlUpdateSach);
                    updateSach.state.execute(sqlUpdateSach);
                    JOptionPane.showMessageDialog(null, "Chnh sa quyn " + input[1] + " thnh cng");
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "? c li xy ra +editQuyen", "ERROR", 2);
                }
                updateSach.freeConnection();
            } else {
                if (JOptionPane.showConfirmDialog(null,
                        "Bn  nhp 1 M Quyn mi. Bn c mun thm Quyn ny khng ?") == JOptionPane.YES_OPTION) {
                    addQuyen(input);
                }
            }
            resetTable(QuyenTable);
            refreshQuyenTable();

        }
    }

    private void addSach(String[] input) {

        try {
            if (checkInputSach(1, input)) {

                if (checkMaSach(input[0]) == true) {
                    JOptionPane.showMessageDialog(null,
                            "M Sch  tn ti. Vui lng nhp M Sch khc!", "ERROR", 2);
                } else {
                    String[] sqlInput = new String[8];
                    sqlInput = setSqlInputSach(0, input);
                    String sqlAddSach = "INSERT INTO Sach (MaSach, TenSach, Gia, Nxb, NamXB, TacGia, TheLoai, SoTrang) VALUES ( '"
                            + input[0] + "','" + input[1] + "'," + "0,'" + "NONAME'," + "0,'" + "NONAME','"
                            + "NONAME'," + "0);";

                    String sqlUpdateSach = "UPDATE Sach SET ";
                    for (int i = 0; i < 8; i++) {
                        sqlUpdateSach += sqlInput[i];
                    }
                    sqlUpdateSach += " WHERE MaSach = '" + input[0] + "';";
                    DBConnection addSach = new DBConnection();
                    addSach.getConnection();
                    System.out.println(sqlAddSach);
                    addSach.state.execute(sqlAddSach);
                    System.out.println(sqlUpdateSach);
                    addSach.state.execute(sqlUpdateSach);
                    addSach.freeConnection();

                    JOptionPane.showMessageDialog(null, "? thm quyn " + input[1] + " thnh cng");

                }
            }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "? c li xy ra +addSach", "ERROR", 2);
        }
        resetTable(SachTable);
        refreshSachTable();

    }

    private void editSach(String[] input) {
        if (checkInputSach(1, input)) {
            if (checkMaSach(input[0]) == true) {
                String[] sqlInput = setSqlInputSach(0, input);
                String sqlUpdateSach = "UPDATE Sach SET " + sqlInput[0] + sqlInput[1] + sqlInput[2] + sqlInput[3]
                        + sqlInput[4] + sqlInput[5] + sqlInput[6] + sqlInput[7] + " WHERE MaSach = '" + input[0]
                        + "';";
                DBConnection updateSach = new DBConnection();
                updateSach.getConnection();
                try {
                    System.out.println(sqlUpdateSach);
                    updateSach.state.execute(sqlUpdateSach);
                    JOptionPane.showMessageDialog(null, "Chnh sa quyn " + input[1] + " thnh cng");
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "? c li xy ra +editSach", "ERROR", 2);
                }
                updateSach.freeConnection();
            } else {
                if (JOptionPane.showConfirmDialog(null,
                        "Bn  nhp 1 M Sch mi. Bn c mun thm Sch ny khng ?") == JOptionPane.YES_OPTION) {
                    addSach(getNoiDungTextField(0));
                }
            }
            resetTable(SachTable);
            refreshSachTable();

        }
    }

    private void xoaSach() {
        int[] sachTableSelectedRows = SachTable.getSelectedRows();
        int soSachDuocChon = sachTableSelectedRows.length;
        if (soSachDuocChon <= 0) {
            JOptionPane.showMessageDialog(null, "Bn phi ch?n 1 bn ghi");
        } else {
            TableModel model = (TableModel) SachTable.getModel();
            String sachDaChon = "", sachCoNhieuQuyen = "";
            for (int i = 0; i < soSachDuocChon; i++) {
                sachDaChon += model.getValueAt(sachTableSelectedRows[i], 1).toString() + "\n";
            }
            if (JOptionPane.showConfirmDialog(null, "Bn c mun xa quyn sch : \n" + sachDaChon
                    + "khng ?") == JOptionPane.YES_OPTION) {
                try {
                    DBConnection deleteSach = new DBConnection();
                    deleteSach.getConnection();
                    String[] maSach = new String[soSachDuocChon];
                    String[] tenSach = new String[soSachDuocChon];
                    for (int i = 0; i < soSachDuocChon; i++) {
                        maSach[i] = model.getValueAt(sachTableSelectedRows[i], 0).toString();
                        tenSach[i] = model.getValueAt(sachTableSelectedRows[i], 1).toString();
                    }
                    for (int i = 0; i < soSachDuocChon; i++) {
                        if (isExistMaSachinChiTietSach(maSach[i])) {
                            sachCoNhieuQuyen += tenSach[i] + "\n";
                        } else {
                            String querty = "DELETE FROM Sach WHERE (MaSach = '" + maSach[i] + "');";

                            System.out.println(querty);
                            deleteSach.state.execute(querty);
                        }
                    }
                    if (!sachCoNhieuQuyen.equals("")) {
                        JOptionPane.showMessageDialog(null,
                                "Hy xa cc quyn ca cun \n" + sachCoNhieuQuyen + "trc khi xa.");
                    }
                    deleteSach.freeConnection();
                } catch (SQLException e) {
                    JOptionPane.showMessageDialog(null, "? c li xy ra +xa t SachTable", "ERROR", 2);
                }
            }
        }

        resetTable(SachTable);
        refreshSachTable();
        setNoiDungTextField(0, new String[] { "", "", "", "", "", "", "", "" }); //Sach
    }

    private boolean isExistMaSachinChiTietSach(String MaSach) {
        // true tng ng vi M sach  tn ti
        // false tng ng vi M sach cha tn ti

        String sqlCheckMaSach = "SELECT * FROM ChiTietSach WHERE (MaSach = '" + MaSach + "');";
        DBConnection checkMaSach = new DBConnection();
        checkMaSach.getConnection();
        checkMaSach.excuteQuery(sqlCheckMaSach);
        boolean rs = true;
        try {
            rs = checkMaSach.result.next();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "? c li xy ra +isExistMaSachinChiTietSach", "ERROR", 2);
        }
        checkMaSach.freeConnection();
        return (rs);
    }

    private boolean isExistMaQuyeninMuonTra(String MaQuyen) {
        // true tng ng vi M Quyn  tn ti
        // false tng ng vi M Quyn cha tn ti

        String sqlCheckMaQuyen = "SELECT * FROM ChiTietMuonTra WHERE (MaQuyen = '" + MaQuyen + "');";
        DBConnection checkMaQuyen = new DBConnection();
        checkMaQuyen.getConnection();
        checkMaQuyen.excuteQuery(sqlCheckMaQuyen);
        boolean rs = true;
        try {
            rs = checkMaQuyen.result.next();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "? c li xy ra +isExistMaQuyeninMuonTra", "ERROR", 2);
        }
        checkMaQuyen.freeConnection();
        return (rs);
    }

    private void xoaQuyen() {
        int[] quyenTableSelectedRows = QuyenTable.getSelectedRows();
        if (quyenTableSelectedRows.length <= 0) {
            JOptionPane.showMessageDialog(null, "Bn phi ch?n 1 bn ghi");
        } else {
            if (quyenTableSelectedRows.length > 0) {
                TableModel model = (TableModel) QuyenTable.getModel();
                String quyenDaChon = "", quyenDangMuon = "";
                for (int i = 0; i < quyenTableSelectedRows.length; i++) {
                    quyenDaChon += model.getValueAt(quyenTableSelectedRows[i], 1).toString() + "\n";
                }
                if (JOptionPane.showConfirmDialog(null, "Bn c mun xa quyn : \n" + quyenDaChon
                        + "khng ?") == JOptionPane.YES_OPTION) {
                    try {
                        DBConnection deleteQuyen = new DBConnection();
                        deleteQuyen.getConnection();
                        String[] naQuyen = new String[quyenTableSelectedRows.length];
                        for (int i = 0; i < quyenTableSelectedRows.length; i++) {
                            naQuyen[i] = model.getValueAt(quyenTableSelectedRows[i], 1).toString();
                        }
                        for (int i = 0; i < quyenTableSelectedRows.length; i++) {
                            if (isExistMaQuyeninMuonTra(naQuyen[i])) {
                                quyenDangMuon += naQuyen[i] + "\n";
                            } else {
                                String querty = "DELETE FROM ChiTietSach WHERE (MaQuyen = '" + naQuyen[i] + "');";
                                System.out.println(querty);
                                deleteQuyen.state.execute(querty);
                                refreshQuyenTable();
                                chiTietSach();
                            }
                        }

                        if (!quyenDangMuon.equals("")) {
                            JOptionPane.showMessageDialog(null, "Hy xa quyn " + quyenDangMuon
                                    + "trong Bng Chi Tit Mn Tr trc khi xa");
                        }
                        deleteQuyen.freeConnection();
                    } catch (SQLException e) {
                        JOptionPane.showMessageDialog(null, "? c li xy ra +xa t QuyenTable",
                                "ERROR", 2);
                    }
                }
            }

        }

        setNoiDungTextField(1, new String[] { "", "", "", "" }); //Quyen
    }

    public void showWindows() {
        //this.setSize(600,600);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    private void xuatFile() {
        String[] tenCot = { "M Sch", "Tn Sch", "Gi (VN?)", "Nh Xut Bn", "Nm Xut Bn",
                "Tc Gi", "Th Loi", "S Trang" };
        ;
        String title = "                                                                           Tm Kim Sch";
        String fileName = "TimKiemSach";
        String bf = "                             H? v tn         : L Ng?c Long\n"
                + "                              MSSV                : 20142659\n"
                + "                              ?? ti               : Xy dng chng trnh qun l th vin";
        bf += "\n\n                              Tr?ng ?i H?c Bch Khoa H Ni                                                                                                              Cng Ha - X Hi - Ch Ngha - Vit Nam\n"
                + "                                    Th vin T Quang Bu                                                                                                                                   ?c Lp - T Do - Hnh Phc";
        bf += "\n\n                                                           " + title.toUpperCase();

        String af = "                             H Ni, Vit Nam Ngy "
                + Model.ThuVien.getTodayNgayThangNam() + "\n"
                + "                             Ng?i to bng : L Ng?c Long";

        Model.ThuVien.xuatFilePDF2(fileName, bf, tenCot, af, SachTable, title, PageSize.A2);
    }

}