Java tutorial
/* * 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.ThuVien; import Model.DBConnection; import com.itextpdf.text.DocumentException; import com.itextpdf.text.PageSize; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfTemplate; import com.itextpdf.text.pdf.PdfWriter; import java.awt.Graphics2D; import java.awt.event.KeyEvent; import java.io.FileOutputStream; import java.io.IOException; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JOptionPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; /** * * @author hctha_000 */ public class QuanLiDocGia extends javax.swing.JFrame { DefaultTableModel dm; String homNay = ""; boolean checkFirstColumn = false; int error = -1; public QuanLiDocGia() { initComponents(); setTitle("?c Gi - Phn m?m qun l th vin"); setExtendedState(MAXIMIZED_BOTH); this.setVisible(true); refreshDocGiaTable(); homNay = Model.ThuVien.getToday(); } /** * 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(); DocGiaTable = new javax.swing.JTable(); jLabel3 = new javax.swing.JLabel(); jTextField_MaDocGia = new javax.swing.JTextField(); jTextField_TenDocGia = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jTextField_Mssv = new javax.swing.JTextField(); jTextField_Lop = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jTextField_Khoa = new javax.swing.JTextField(); jComboBox_GioiTinh = new javax.swing.JComboBox<>(); jLabel6 = new javax.swing.JLabel(); jLabel8 = new javax.swing.JLabel(); jTextField_NgaySinh = new javax.swing.JTextField(); jTextField_Sdt = new javax.swing.JTextField(); jLabel7 = new javax.swing.JLabel(); jLabel9 = new javax.swing.JLabel(); jTextField_DiaChi = new javax.swing.JTextField(); jTextField_NgayTao = new javax.swing.JTextField(); jLabel10 = new javax.swing.JLabel(); buttonTimKiem = new javax.swing.JButton(); buttonXoa = new javax.swing.JButton(); buttonChinhSua = new javax.swing.JButton(); buttonClearText = new javax.swing.JButton(); buttonThemDocGia = new javax.swing.JButton(); buttonRefresh = new javax.swing.JButton(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); buttonLogout = new javax.swing.JButton(); buttonTurnBack = new javax.swing.JButton(); jLabel11 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); DocGiaTable.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "M ?c Gi", "Tn ?c Gi", "MSSV", "Lp", "Kha", "Gii Tnh", "Ngy Sinh", "S?T", "?a Ch", "Ngy To" })); DocGiaTable.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseReleased(java.awt.event.MouseEvent evt) { DocGiaTableMouseReleased(evt); } }); DocGiaTable.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(java.awt.event.KeyEvent evt) { DocGiaTableKeyReleased(evt); } }); jScrollPane1.setViewportView(DocGiaTable); jLabel3.setText("M ?c Gi"); jTextField_MaDocGia.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { jTextField_MaDocGiaKeyPressed(evt); } }); jTextField_TenDocGia.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { jTextField_TenDocGiaKeyPressed(evt); } }); jLabel1.setText("Tn ?c Gi"); jLabel1.setMaximumSize(new java.awt.Dimension(40, 14)); jLabel1.setMinimumSize(new java.awt.Dimension(40, 14)); jLabel1.setPreferredSize(new java.awt.Dimension(40, 14)); jLabel2.setText("MSSV"); jLabel2.setMaximumSize(new java.awt.Dimension(40, 14)); jLabel2.setMinimumSize(new java.awt.Dimension(40, 14)); jLabel2.setPreferredSize(new java.awt.Dimension(40, 14)); jTextField_Mssv.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { jTextField_MssvKeyPressed(evt); } }); jTextField_Lop.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { jTextField_LopKeyPressed(evt); } }); jLabel4.setText("Lp"); jLabel4.setMaximumSize(new java.awt.Dimension(40, 14)); jLabel4.setMinimumSize(new java.awt.Dimension(40, 14)); jLabel4.setPreferredSize(new java.awt.Dimension(40, 14)); jLabel5.setText("Kha"); jLabel5.setMaximumSize(new java.awt.Dimension(40, 14)); jLabel5.setMinimumSize(new java.awt.Dimension(40, 14)); jLabel5.setPreferredSize(new java.awt.Dimension(40, 14)); jTextField_Khoa.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { jTextField_KhoaKeyPressed(evt); } }); jComboBox_GioiTinh .setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "null", "Nam", "N", "Khc" })); jComboBox_GioiTinh.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox_GioiTinhActionPerformed(evt); } }); jLabel6.setText("Gii tnh"); jLabel6.setMaximumSize(new java.awt.Dimension(40, 14)); jLabel6.setMinimumSize(new java.awt.Dimension(40, 14)); jLabel6.setPreferredSize(new java.awt.Dimension(40, 14)); jLabel8.setText("Ngy sinh"); jTextField_NgaySinh.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { jTextField_NgaySinhKeyPressed(evt); } }); jTextField_Sdt.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { jTextField_SdtKeyPressed(evt); } }); jLabel7.setText("S in thoi"); jLabel7.setMaximumSize(new java.awt.Dimension(40, 14)); jLabel7.setMinimumSize(new java.awt.Dimension(40, 14)); jLabel7.setPreferredSize(new java.awt.Dimension(40, 14)); jLabel9.setText("?a ch"); jLabel9.setMaximumSize(new java.awt.Dimension(40, 14)); jLabel9.setMinimumSize(new java.awt.Dimension(40, 14)); jLabel9.setPreferredSize(new java.awt.Dimension(40, 14)); jTextField_DiaChi.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { jTextField_DiaChiKeyPressed(evt); } }); jTextField_NgayTao.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { jTextField_NgayTaoKeyPressed(evt); } }); jLabel10.setText("Ngy to"); jLabel10.setMaximumSize(new java.awt.Dimension(40, 14)); jLabel10.setMinimumSize(new java.awt.Dimension(40, 14)); jLabel10.setPreferredSize(new java.awt.Dimension(40, 14)); buttonTimKiem.setText("Tm kim"); buttonTimKiem.setPreferredSize(new java.awt.Dimension(90, 30)); buttonTimKiem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonTimKiemActionPerformed(evt); } }); buttonXoa.setText("Xa"); buttonXoa.setMaximumSize(new java.awt.Dimension(90, 30)); buttonXoa.setMinimumSize(new java.awt.Dimension(90, 30)); buttonXoa.setPreferredSize(new java.awt.Dimension(90, 30)); buttonXoa.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonXoaActionPerformed(evt); } }); buttonChinhSua.setText("Chnh sa"); buttonChinhSua.setPreferredSize(new java.awt.Dimension(90, 30)); buttonChinhSua.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonChinhSuaActionPerformed(evt); } }); buttonClearText.setText("Xa ch"); buttonClearText.setPreferredSize(new java.awt.Dimension(90, 30)); buttonClearText.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonClearTextActionPerformed(evt); } }); buttonThemDocGia.setText("Thm"); buttonThemDocGia.setPreferredSize(new java.awt.Dimension(90, 30)); buttonThemDocGia.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonThemDocGiaActionPerformed(evt); } }); buttonRefresh.setText("Refresh"); buttonRefresh.setPreferredSize(new java.awt.Dimension(90, 30)); buttonRefresh.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonRefreshActionPerformed(evt); } }); jButton1.setText("Thm c gi t file"); jButton1.setPreferredSize(new java.awt.Dimension(90, 30)); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Xut file PDF"); jButton2.setPreferredSize(new java.awt.Dimension(90, 30)); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); buttonLogout.setText("?ng xut"); buttonLogout.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonLogoutActionPerformed(evt); } }); buttonTurnBack.setText("Back"); buttonTurnBack.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonTurnBackActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout .createSequentialGroup().addGap(93, 93, 93).addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( jPanel1Layout.createSequentialGroup() .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jTextField_Lop, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jTextField_Mssv, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(2, 2, 2)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout .createSequentialGroup().addGap(2, 2, 2) .addGroup(jPanel1Layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jTextField_MaDocGia, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent( jTextField_TenDocGia, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE))))) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jTextField_Khoa, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(93, 93, 93) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup() .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jComboBox_GioiTinh, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jTextField_NgaySinh, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jTextField_Sdt, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jTextField_DiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout .createSequentialGroup() .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jTextField_NgayTao, javax.swing.GroupLayout.PREFERRED_SIZE, 163, 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.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout .createSequentialGroup() .addComponent(buttonLogout, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(50, 50, 50) .addComponent(buttonTurnBack, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(13, 13, 13)) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(buttonTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(47, 47, 47) .addComponent(buttonXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(53, 53, 53).addComponent(buttonChinhSua, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout .createSequentialGroup() .addComponent(buttonRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(48, 48, 48) .addComponent(buttonThemDocGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(buttonClearText, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(91, 91, 91)).addGroup( jPanel1Layout.createSequentialGroup().addGap(21, 21, 21) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1323, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(36, Short.MAX_VALUE))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 286, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(52, 52, 52) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField_MaDocGia, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField_TenDocGia, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextField_Mssv, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField_Lop, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField_DiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(30, 30, 30) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField_Khoa, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField_NgayTao, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jComboBox_GioiTinh, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField_NgaySinh, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField_Sdt, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(buttonTimKiem, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(buttonXoa, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(buttonChinhSua, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(48, 48, 48) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(buttonRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(buttonThemDocGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(buttonClearText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(34, 34, 34) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(14, 14, 14) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(buttonLogout, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(buttonTurnBack, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addContainerGap(61, 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(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.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void jTextField_MaDocGiaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_MaDocGiaKeyPressed // TODO add your handling code here: if (evt.getKeyCode() == KeyEvent.VK_UP) { jTextField_Khoa.requestFocus(); } if (evt.getKeyCode() == KeyEvent.VK_DOWN) { jTextField_TenDocGia.requestFocus(); } }//GEN-LAST:event_jTextField_MaDocGiaKeyPressed private void jTextField_TenDocGiaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_TenDocGiaKeyPressed // TODO add your handling code here: if (evt.getKeyCode() == KeyEvent.VK_DOWN) { jTextField_Mssv.requestFocus(); } if (evt.getKeyCode() == KeyEvent.VK_UP) { jTextField_MaDocGia.requestFocus(); } if (evt.getKeyCode() == KeyEvent.VK_RIGHT) { jTextField_NgaySinh.requestFocus(); } }//GEN-LAST:event_jTextField_TenDocGiaKeyPressed private void jTextField_MssvKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_MssvKeyPressed // TODO add your handling code here: // TODO add your handling code here: if (evt.getKeyCode() == KeyEvent.VK_DOWN) { jTextField_Lop.requestFocus(); } if (evt.getKeyCode() == KeyEvent.VK_UP) { jTextField_TenDocGia.requestFocus(); } if (evt.getKeyCode() == KeyEvent.VK_RIGHT) { jTextField_Sdt.requestFocus(); } }//GEN-LAST:event_jTextField_MssvKeyPressed private void jTextField_LopKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_LopKeyPressed // TODO add your handling code here: if (evt.getKeyCode() == KeyEvent.VK_DOWN) { jTextField_Khoa.requestFocus(); } if (evt.getKeyCode() == KeyEvent.VK_UP) { jTextField_Mssv.requestFocus(); } if (evt.getKeyCode() == KeyEvent.VK_RIGHT) { jTextField_DiaChi.requestFocus(); } }//GEN-LAST:event_jTextField_LopKeyPressed private void jTextField_KhoaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_KhoaKeyPressed // TODO add your handling code here: if (evt.getKeyCode() == KeyEvent.VK_DOWN) { jTextField_MaDocGia.requestFocus(); } if (evt.getKeyCode() == KeyEvent.VK_UP) { jTextField_Lop.requestFocus(); } if (evt.getKeyCode() == KeyEvent.VK_RIGHT) { jTextField_NgayTao.requestFocus(); } }//GEN-LAST:event_jTextField_KhoaKeyPressed private void jComboBox_GioiTinhActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_GioiTinhActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jComboBox_GioiTinhActionPerformed private void jTextField_NgaySinhKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_NgaySinhKeyPressed // TODO add your handling code here: if (evt.getKeyCode() == KeyEvent.VK_DOWN) { jTextField_Sdt.requestFocus(); } if (evt.getKeyCode() == KeyEvent.VK_UP) { jTextField_NgayTao.requestFocus(); } if (evt.getKeyCode() == KeyEvent.VK_LEFT) { jTextField_TenDocGia.requestFocus(); } }//GEN-LAST:event_jTextField_NgaySinhKeyPressed private void jTextField_SdtKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_SdtKeyPressed // TODO add your handling code here: if (evt.getKeyCode() == KeyEvent.VK_DOWN) { jTextField_DiaChi.requestFocus(); } if (evt.getKeyCode() == KeyEvent.VK_UP) { jTextField_NgaySinh.requestFocus(); } if (evt.getKeyCode() == KeyEvent.VK_LEFT) { jTextField_Mssv.requestFocus(); } }//GEN-LAST:event_jTextField_SdtKeyPressed private void jTextField_DiaChiKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_DiaChiKeyPressed // TODO add your handling code here: if (evt.getKeyCode() == KeyEvent.VK_DOWN) { jTextField_NgayTao.requestFocus(); } if (evt.getKeyCode() == KeyEvent.VK_UP) { jTextField_Sdt.requestFocus(); } if (evt.getKeyCode() == KeyEvent.VK_LEFT) { jTextField_Lop.requestFocus(); } }//GEN-LAST:event_jTextField_DiaChiKeyPressed private void jTextField_NgayTaoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_NgayTaoKeyPressed // TODO add your handling code here: if (evt.getKeyCode() == KeyEvent.VK_DOWN) { jTextField_NgaySinh.requestFocus(); } if (evt.getKeyCode() == KeyEvent.VK_UP) { jTextField_DiaChi.requestFocus(); } if (evt.getKeyCode() == KeyEvent.VK_LEFT) { jTextField_Khoa.requestFocus(); } }//GEN-LAST:event_jTextField_NgayTaoKeyPressed private void buttonTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTimKiemActionPerformed timKiem(getNoiDungTextField()); }//GEN-LAST:event_buttonTimKiemActionPerformed private void buttonXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonXoaActionPerformed xoaDocGia(); }//GEN-LAST:event_buttonXoaActionPerformed private void buttonChinhSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonChinhSuaActionPerformed chinhSuaDocGia(getNoiDungTextField()); }//GEN-LAST:event_buttonChinhSuaActionPerformed private void buttonClearTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonClearTextActionPerformed // TODO add your handling code here: setNoiDungTextField(new String[] { "", "", "", "", "", "", "", "", "", "" }); }//GEN-LAST:event_buttonClearTextActionPerformed private void buttonThemDocGiaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonThemDocGiaActionPerformed // TODO add your handling code here: addDocGia(getNoiDungTextField()); }//GEN-LAST:event_buttonThemDocGiaActionPerformed private void buttonRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRefreshActionPerformed // TODO add your handling code here: resetDocGiaTable(); refreshDocGiaTable(); }//GEN-LAST:event_buttonRefreshActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed this.dispose(); addtuFileTxt newaddDocGiatuFileTxt = new addtuFileTxt(); newaddDocGiatuFileTxt.showWindows(); newaddDocGiatuFileTxt.type = 0; }//GEN-LAST:event_jButton1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed xuatFile(); }//GEN-LAST:event_jButton2ActionPerformed 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 DocGiaTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_DocGiaTableMouseReleased getNoiDungtuDocGiaTable(); }//GEN-LAST:event_DocGiaTableMouseReleased private void DocGiaTableKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_DocGiaTableKeyReleased if (evt.getKeyCode() == KeyEvent.VK_UP || evt.getKeyCode() == KeyEvent.VK_DOWN) { getNoiDungtuDocGiaTable(); } }//GEN-LAST:event_DocGiaTableKeyReleased /** * @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(QuanLiDocGia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(QuanLiDocGia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(QuanLiDocGia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(QuanLiDocGia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new QuanLiDocGia().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTable DocGiaTable; private javax.swing.JButton buttonChinhSua; private javax.swing.JButton buttonClearText; private javax.swing.JButton buttonLogout; private javax.swing.JButton buttonRefresh; private javax.swing.JButton buttonThemDocGia; private javax.swing.JButton buttonTimKiem; private javax.swing.JButton buttonTurnBack; private javax.swing.JButton buttonXoa; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JComboBox<String> jComboBox_GioiTinh; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel10; private javax.swing.JLabel jLabel11; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextField jTextField_DiaChi; private javax.swing.JTextField jTextField_Khoa; private javax.swing.JTextField jTextField_Lop; private javax.swing.JTextField jTextField_MaDocGia; private javax.swing.JTextField jTextField_Mssv; private javax.swing.JTextField jTextField_NgaySinh; private javax.swing.JTextField jTextField_NgayTao; private javax.swing.JTextField jTextField_Sdt; private javax.swing.JTextField jTextField_TenDocGia; // End of variables declaration//GEN-END:variables private void refreshDocGiaTable() { try { dm = (DefaultTableModel) DocGiaTable.getModel(); resetDocGiaTable(); String sqlQuerty = "SELECT * FROM DocGia ;"; DBConnection dbRefreshTable = new DBConnection(); dbRefreshTable.getConnection(); dbRefreshTable.excuteQuery(sqlQuerty); System.out.println(sqlQuerty); while (dbRefreshTable.result.next()) { String[] rowData = { dbRefreshTable.result.getString("MaDocGia"), dbRefreshTable.result.getString("TenDocGia"), dbRefreshTable.result.getString("Mssv"), dbRefreshTable.result.getString("Lop"), dbRefreshTable.result.getString("Khoa"), dbRefreshTable.result.getString("GioiTinh"), dbRefreshTable.result.getString("NgaySinh"), dbRefreshTable.result.getString("Sdt"), dbRefreshTable.result.getString("DiaChi"), dbRefreshTable.result.getString("NgayTao") }; dm.addRow(rowData); } dbRefreshTable.freeConnection(); } catch (SQLException e) { JOptionPane.showMessageDialog(null, "? c li xy ra +RefreshDocGiaTable", "ERROR", 2); } jLabel11.setText(""); } private void resetDocGiaTable() { dm = (DefaultTableModel) DocGiaTable.getModel(); int rowCount = dm.getRowCount(); for (int i = rowCount - 1; i >= 0; i--) { dm.removeRow(i); } } private void getNoiDungtuDocGiaTable() { int i = DocGiaTable.getSelectedRow(); TableModel model = (TableModel) DocGiaTable.getModel(); String[] input = new String[10]; for (int j = 0; j < 10; j++) { input[j] = model.getValueAt(i, j).toString(); } setNoiDungTextField(input); } private void setNoiDungTextField(String[] input) { jTextField_MaDocGia.setText(input[0]); jTextField_TenDocGia.setText(input[1]); jTextField_Mssv.setText(input[2]); jTextField_Lop.setText(input[3]); jTextField_Khoa.setText(input[4]); if (input[5].equals("")) { jComboBox_GioiTinh.setSelectedItem("null"); } else { jComboBox_GioiTinh.setSelectedItem(input[5]); } jTextField_NgaySinh.setText(input[6]); jTextField_Sdt.setText(input[7]); jTextField_DiaChi.setText(input[8]); jTextField_NgayTao.setText(input[9]); } private void addDocGia(String[] input) { try { if (checkInput(1, input)) { if (isExistMaDocGia(input[0]) == true) { JOptionPane.showMessageDialog(null, "M ?c Gi tn ti. Vui lng nhp M ?c Gi khc!", "ERROR", 2); } else { String[] sqlInput = new String[10]; sqlInput = setSqlInput(0, input); String sqlAddDocGia = "INSERT INTO DocGia (MaDocGia, TenDocGia, Mssv, Lop, Khoa, GioiTinh, NgaySinh, Sdt, DiaChi, NgayTao) VALUES ( '" + input[0] + "','" + input[1] + "'," + "0,'" + "NONAME'," + "'NONAME'," + "'Khc','" + "1990-01-01'," + "0,'" + "NONAME','" + homNay + "');"; String sqlUpdateDocGia = "UPDATE DocGia SET "; for (int i = 0; i < 10; i++) { sqlUpdateDocGia += sqlInput[i]; } sqlUpdateDocGia += " WHERE MaDocGia = '" + input[0] + "';"; DBConnection addDocGia = new DBConnection(); addDocGia.getConnection(); System.out.println(sqlAddDocGia); addDocGia.state.execute(sqlAddDocGia); System.out.println(sqlUpdateDocGia); addDocGia.state.execute(sqlUpdateDocGia); addDocGia.freeConnection(); JOptionPane.showMessageDialog(null, "? thm c gi " + input[1] + " thnh cng"); } } } catch (Exception e) { JOptionPane.showMessageDialog(null, "? c li xy ra +addDocGia", "ERROR", 2); } refreshDocGiaTable(); } private boolean checkInput(int checkType, String[] input) {// false : c li, true : khng c li // checkType = 0 : check ca Tm kim // checkType = 1 : check ca Thm hoc Chnh sa // checkType = 2 : check ly error boolean check = false; for (int i = 0; i < 10; 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 c gi!!"); return false; } if (input[1].equals("") && checkType != 0) { JOptionPane.showMessageDialog(null, "Bn cha nhp tn c gi!!"); return false; } if (!Model.ThuVien.checkNumeric(input[2]) && !input[2].equals("")) { JOptionPane.showMessageDialog(null, "Thng tin MSSV bn nhp khng hp l!!"); return false; } if (isExistMSSV(input[2]) && !input[2].equals("") && checkType == 1) { JOptionPane.showMessageDialog(null, "MSSV bn nhp tn ti. Vui lng nhp MSSV khc!!"); return false; } if (!Model.ThuVien.checkNumeric(input[7]) && !input[7].equals("")) { JOptionPane.showMessageDialog(null, "Thng tin S?T bn nhp khng hp l!!"); return false; } if (!Model.ThuVien.checkValidDate(input[6]) && !input[6].equals("")) { JOptionPane.showMessageDialog(null, "Thng tin Ngy Sinh bn nhp khng hp l!!"); return false; } if (!Model.ThuVien.checkValidDate(input[9]) && !input[9].equals("")) { JOptionPane.showMessageDialog(null, "Thng tin Ngy to bn nhp khng hp l!!"); return false; } return true; // Khng c li } private boolean isExistMSSV(String MSSV) { // true tng ng vi MSSV tn ti // false tng ng vi MSSV cha tn ti String sqlCheckMSSV = "SELECT * FROM DocGia WHERE (Mssv = '" + MSSV + "');"; DBConnection checkMSSV = new DBConnection(); checkMSSV.getConnection(); checkMSSV.excuteQuery(sqlCheckMSSV); boolean rs = true; try { rs = checkMSSV.result.next(); } catch (SQLException ex) { JOptionPane.showMessageDialog(null, "? c li xy ra +isExistMSSV", "ERROR", 2); } checkMSSV.freeConnection(); return (rs); } private String[] getNoiDungTextField() { String[] NoiDungTextField = new String[10]; NoiDungTextField[0] = jTextField_MaDocGia.getText().trim(); NoiDungTextField[1] = jTextField_TenDocGia.getText().trim(); NoiDungTextField[2] = jTextField_Mssv.getText().trim(); NoiDungTextField[3] = jTextField_Lop.getText().trim(); NoiDungTextField[4] = jTextField_Khoa.getText().trim(); if (((String) jComboBox_GioiTinh.getSelectedItem()).equals("null")) { NoiDungTextField[5] = ""; } else { NoiDungTextField[5] = (String) jComboBox_GioiTinh.getSelectedItem(); } NoiDungTextField[6] = jTextField_NgaySinh.getText().trim(); NoiDungTextField[7] = jTextField_Sdt.getText().trim(); NoiDungTextField[8] = jTextField_DiaChi.getText().trim(); NoiDungTextField[9] = jTextField_NgayTao.getText().trim(); return NoiDungTextField; } private boolean isExistMaDocGia(String MaDocGia) { // true tng ng vi M ?c Gi tn ti // false tng ng vi M ?c Gi cha tn ti String sqlCheckMaDocGia = "SELECT * FROM DocGia WHERE (MaDocGia = '" + MaDocGia + "');"; DBConnection checkMaDocGia = new DBConnection(); checkMaDocGia.getConnection(); checkMaDocGia.excuteQuery(sqlCheckMaDocGia); boolean rs = true; try { rs = checkMaDocGia.result.next(); } catch (SQLException ex) { JOptionPane.showMessageDialog(null, "? c li xy ra +isExistMaDocGia", "ERROR", 2); } checkMaDocGia.freeConnection(); return (rs); } private boolean isExistMaDocGiainMuonTra(String MaDocGia) { // true tng ng vi M ?c Gi tn ti // false tng ng vi M ?c Gi cha tn ti String sqlCheckMaDocGia = "SELECT * FROM MuonTra WHERE (MaDocGia = '" + MaDocGia + "');"; DBConnection checkMaDocGia = new DBConnection(); checkMaDocGia.getConnection(); checkMaDocGia.excuteQuery(sqlCheckMaDocGia); boolean rs = true; try { rs = checkMaDocGia.result.next(); } catch (SQLException ex) { JOptionPane.showMessageDialog(null, "? c li xy ra +isExistMaDocGiainMuonTra", "ERROR", 2); } checkMaDocGia.freeConnection(); return (rs); } public String[] setSqlInput(int setType, String[] input) { //setType = 0 : cu lnh sql chnh sa, setType = 1 : cu lnh sql tm kim String[] sqlInput = new String[10]; checkFirstColumn = false; String link = ""; if (setType == 1) { link = " AND "; } if (setType == 0) { link = " , "; } if (input[0].equals("")) { sqlInput[0] = ""; } else { sqlInput[0] = "MaDocGia = '" + input[0] + "'"; checkFirstColumn = true; } if (input[1].equals("")) { sqlInput[1] = ""; } else { if (checkFirstColumn) { sqlInput[1] = link + "TenDocGia = '" + input[1] + "'"; } else { sqlInput[1] = "TenDocGia = '" + input[1] + "'"; checkFirstColumn = true; } } if (input[2].equals("")) { sqlInput[2] = ""; } else { if (checkFirstColumn) { sqlInput[2] = link + "Mssv = " + input[2]; } else { sqlInput[2] = "Mssv = " + input[2]; checkFirstColumn = true; } } if (input[3].equals("")) { sqlInput[3] = ""; } else { if (checkFirstColumn) { sqlInput[3] = link + "Lop = '" + input[3] + "'"; } else { sqlInput[3] = "Lop = '" + input[3] + "'"; checkFirstColumn = true; } } if (input[4].equals("")) { sqlInput[4] = ""; } else { if (checkFirstColumn) { sqlInput[4] = link + "Khoa = '" + input[4] + "'"; } else { sqlInput[4] = "Khoa = '" + input[4] + "'"; checkFirstColumn = true; } } if (input[5].equals("")) { sqlInput[5] = ""; } else { if (checkFirstColumn) { sqlInput[5] = link + "GioiTinh = '" + input[5] + "' "; } else { sqlInput[5] = "GioiTinh = '" + input[5] + "' "; checkFirstColumn = true; } } if (input[6].equals("")) { sqlInput[6] = ""; } else { if (checkFirstColumn) { sqlInput[6] = link + "NgaySinh = '" + input[6] + "' "; } else { sqlInput[6] = "NgaySinh = '" + input[6] + "' "; checkFirstColumn = true; } } if (input[7].equals("")) { sqlInput[7] = ""; } else { if (checkFirstColumn) { sqlInput[7] = link + "Sdt = '" + input[7] + "' "; } else { sqlInput[7] = "Sdt = '" + input[7] + "' "; checkFirstColumn = true; } } if (input[8].equals("")) { sqlInput[8] = ""; } else { if (checkFirstColumn) { sqlInput[8] = link + "DiaChi = '" + input[8] + "' "; } else { sqlInput[8] = "DiaChi = '" + input[8] + "' "; checkFirstColumn = true; } } if (input[9].equals("")) { sqlInput[9] = ""; } else { if (checkFirstColumn) { sqlInput[9] = link + "NgayTao = '" + input[9] + "' "; } else { sqlInput[9] = "NgayTao = '" + input[9] + "' "; } } return sqlInput; } private void xoaDocGia() { int[] docGiaTableSelectedRows = DocGiaTable.getSelectedRows(); if (docGiaTableSelectedRows.length <= 0) { JOptionPane.showMessageDialog(null, "Bn phi ch?n 1 c gi"); } else { TableModel model = (TableModel) DocGiaTable.getModel(); String docGiaDaChon = "", docGiaDangMuonSach = ""; for (int i = 0; i < docGiaTableSelectedRows.length; i++) { docGiaDaChon += model.getValueAt(docGiaTableSelectedRows[i], 1).toString() + "\n"; } if (JOptionPane.showConfirmDialog(null, "Bn c mun xa c gi : \n" + docGiaDaChon + "khng ?") == JOptionPane.YES_OPTION) { try { DBConnection deleteDocGia = new DBConnection(); deleteDocGia.getConnection(); for (int i = 0; i < docGiaTableSelectedRows.length; i++) { if (isExistMaDocGiainMuonTra(model.getValueAt(docGiaTableSelectedRows[i], 0).toString())) { docGiaDangMuonSach += model.getValueAt(docGiaTableSelectedRows[i], 1).toString() + "\n"; } else { String querty = "DELETE FROM DocGia WHERE (MaDocGia = '" + model.getValueAt(docGiaTableSelectedRows[i], 0).toString() + "');"; System.out.println(querty); deleteDocGia.state.execute(querty); } } if (!docGiaDangMuonSach.equals("")) { JOptionPane.showMessageDialog(null, "?c Gi \n" + docGiaDangMuonSach + "ang mn sch, v vy bn khng th xa."); } deleteDocGia.freeConnection(); } catch (SQLException e) { JOptionPane.showMessageDialog(null, "? c li xy ra +xa t DocGiaTable", "ERROR", 2); } } } refreshDocGiaTable(); setNoiDungTextField(new String[] { "", "", "", "", "", "", "", "", "", "" }); } private void chinhSuaDocGia(String[] input) { if (checkInput(1, input)) { if (isExistMaDocGia(input[0]) == true) { String[] sqlInput = new String[10]; sqlInput = setSqlInput(0, input); String sqlUpdateDocGia = "UPDATE DocGia SET "; for (int i = 0; i < 10; i++) { sqlUpdateDocGia += sqlInput[i]; } sqlUpdateDocGia += " WHERE MaDocGia = '" + input[0] + "';"; DBConnection updateDocGia = new DBConnection(); updateDocGia.getConnection(); try { System.out.println(sqlUpdateDocGia); updateDocGia.state.execute(sqlUpdateDocGia); JOptionPane.showMessageDialog(null, "Chnh sa c gi " + input[1] + " thnh cng"); } catch (Exception e) { JOptionPane.showMessageDialog(null, "? c li xy ra +nt chnh sa", "ERROR", 2); } updateDocGia.freeConnection(); } else { if (JOptionPane.showConfirmDialog(null, "Bn nhp 1 M ?c Gi mi. Bn c mun thm ?c Gi ny khng ?") == JOptionPane.YES_OPTION) { addDocGia(input); } } refreshDocGiaTable(); } } private void timKiem(String[] input) { try { if (checkInput(0, input)) { String[] sqlInput = new String[10]; sqlInput = setSqlInput(1, input); String sqlSearch = "SELECT * FROM DocGia WHERE "; for (int i = 0; i < 10; i++) { sqlSearch += sqlInput[i]; } sqlSearch += ";"; DBConnection SearchDocGia = new DBConnection(); SearchDocGia.getConnection(); System.out.println(sqlSearch); SearchDocGia.excuteQuery(sqlSearch); int soDocGia = 0; while (SearchDocGia.result.next()) { soDocGia++; } if (soDocGia == 0) { JOptionPane.showMessageDialog(null, "Khng c ?c Gi no c!!!"); jLabel11.setText(""); } else { jLabel11.setText("C " + soDocGia + " bn ghi c tm thy"); dm = (DefaultTableModel) DocGiaTable.getModel(); resetDocGiaTable(); SearchDocGia.result.beforeFirst(); String[] maDocGia = new String[soDocGia]; while (SearchDocGia.result.next()) { String[] rowData = { SearchDocGia.result.getString("MaDocGia"), SearchDocGia.result.getString("TenDocGia"), SearchDocGia.result.getString("Mssv"), SearchDocGia.result.getString("Lop"), SearchDocGia.result.getString("Khoa"), SearchDocGia.result.getString("GioiTinh"), SearchDocGia.result.getString("NgaySinh"), SearchDocGia.result.getString("Sdt"), SearchDocGia.result.getString("DiaChi"), SearchDocGia.result.getString("NgayTao") }; dm.addRow(rowData); } } } } catch (SQLException ex) { System.out.println(ex); JOptionPane.showMessageDialog(null, "? c li xy ra +tm kim t DocGiaTable", "ERROR", 2); } } void showWindows() { this.setExtendedState(MAXIMIZED_BOTH); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setResizable(false); this.setLocationRelativeTo(null); this.setVisible(true); } private void xuatFile() { String[] tenCot = { "M ?c Gi", "Tn ?c Gi", "MSSV", "Lp", "Kha", "Gii Tnh", "Ngy Sinh", "S?T", "?a Ch", "Ngy To" }; ; String title = " Tm Kim ?c Gi"; String fileName = "TimKiemDocGia"; 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, DocGiaTable, title, PageSize.A2); } }