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.DBConnection; import com.itextpdf.text.PageSize; import java.sql.SQLException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; /** * * @author hctha_000 */ public class ThongKe extends javax.swing.JFrame { DefaultTableModel dm; public ThongKe() { initComponents(); setTitle("?ng Nhp - Phn m?m qun l th vin"); setExtendedState(MAXIMIZED_BOTH); this.setVisible(true); } public void showWindows() { //this.setSize(600,600); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setResizable(false); this.setLocationRelativeTo(null); this.setVisible(true); } /** * 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() { jTabbedPane1 = new javax.swing.JTabbedPane(); ThongKeSach = new javax.swing.JPanel(); jComboBox1 = new javax.swing.JComboBox<>(); jButton10 = new javax.swing.JButton(); jScrollPane4 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); jButton11 = new javax.swing.JButton(); jButton12 = new javax.swing.JButton(); ThongKeDocGia = new javax.swing.JPanel(); jComboBox2 = new javax.swing.JComboBox<>(); jButton3 = new javax.swing.JButton(); jScrollPane2 = new javax.swing.JScrollPane(); jTable2 = new javax.swing.JTable(); jButton2 = new javax.swing.JButton(); jButton6 = new javax.swing.JButton(); ThongKeMuonTra = new javax.swing.JPanel(); jComboBox3 = new javax.swing.JComboBox<>(); jButton7 = new javax.swing.JButton(); jScrollPane3 = new javax.swing.JScrollPane(); jTable3 = new javax.swing.JTable(); jButton8 = new javax.swing.JButton(); jButton9 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>( new String[] { "Th Loi", "Nh Xut Bn", "Nm Xut Bn", "Tc Gi" })); jButton10.setText("Thng K"); jButton10.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton10ActionPerformed(evt); } }); jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "Tiu Ch", "S Lng" })); jScrollPane4.setViewportView(jTable1); jButton11.setText("Xut file"); jButton11.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton11ActionPerformed(evt); } }); jButton12.setText("Back"); jButton12.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton12ActionPerformed(evt); } }); javax.swing.GroupLayout ThongKeSachLayout = new javax.swing.GroupLayout(ThongKeSach); ThongKeSach.setLayout(ThongKeSachLayout); ThongKeSachLayout.setHorizontalGroup(ThongKeSachLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(ThongKeSachLayout.createSequentialGroup().addGroup(ThongKeSachLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(ThongKeSachLayout.createSequentialGroup().addGap(185, 185, 185) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(111, 111, 111).addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(ThongKeSachLayout.createSequentialGroup().addGap(270, 270, 270) .addGroup(ThongKeSachLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton11, javax.swing.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE)))) .addGap(69, 69, 69).addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 641, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(186, Short.MAX_VALUE))); ThongKeSachLayout.setVerticalGroup(ThongKeSachLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(ThongKeSachLayout.createSequentialGroup().addGap(43, 43, 43).addGroup(ThongKeSachLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(ThongKeSachLayout.createSequentialGroup().addGap(133, 133, 133) .addGroup(ThongKeSachLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(124, 124, 124).addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(34, 34, 34).addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(162, Short.MAX_VALUE))); jTabbedPane1.addTab("Thng K Sch", ThongKeSach); jComboBox2.setModel(new javax.swing.DefaultComboBoxModel<>( new String[] { "Lp", "Kha", "Gii Tnh", "?a Ch" })); jButton3.setText("Thng K"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jTable2.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "Tiu Ch", "S Lng" })); jScrollPane2.setViewportView(jTable2); jButton2.setText("Xut file"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton6.setText("Back"); jButton6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton6ActionPerformed(evt); } }); javax.swing.GroupLayout ThongKeDocGiaLayout = new javax.swing.GroupLayout(ThongKeDocGia); ThongKeDocGia.setLayout(ThongKeDocGiaLayout); ThongKeDocGiaLayout.setHorizontalGroup(ThongKeDocGiaLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(ThongKeDocGiaLayout.createSequentialGroup().addGroup(ThongKeDocGiaLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(ThongKeDocGiaLayout.createSequentialGroup().addGap(185, 185, 185) .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(111, 111, 111).addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(ThongKeDocGiaLayout.createSequentialGroup().addGap(270, 270, 270) .addGroup(ThongKeDocGiaLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE)))) .addGap(69, 69, 69).addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 641, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(186, Short.MAX_VALUE))); ThongKeDocGiaLayout.setVerticalGroup(ThongKeDocGiaLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(ThongKeDocGiaLayout.createSequentialGroup().addGap(43, 43, 43) .addGroup(ThongKeDocGiaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(ThongKeDocGiaLayout.createSequentialGroup().addGap(133, 133, 133) .addGroup(ThongKeDocGiaLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(124, 124, 124).addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(34, 34, 34).addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(162, Short.MAX_VALUE))); jTabbedPane1.addTab("Thng K ?c Gi", ThongKeDocGia); jComboBox3.setModel(new javax.swing.DefaultComboBoxModel<>( new String[] { "?c Gi", "Sch", "Ngy Mn", "Thng Mn", "Nm Mn" })); jButton7.setText("Thng K"); jButton7.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton7ActionPerformed(evt); } }); jTable3.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "Tiu Ch", "S Lng" })); jScrollPane3.setViewportView(jTable3); jButton8.setText("Xut file"); jButton8.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton8ActionPerformed(evt); } }); jButton9.setText("Back"); jButton9.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton9ActionPerformed(evt); } }); javax.swing.GroupLayout ThongKeMuonTraLayout = new javax.swing.GroupLayout(ThongKeMuonTra); ThongKeMuonTra.setLayout(ThongKeMuonTraLayout); ThongKeMuonTraLayout.setHorizontalGroup(ThongKeMuonTraLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(ThongKeMuonTraLayout.createSequentialGroup().addGroup(ThongKeMuonTraLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(ThongKeMuonTraLayout.createSequentialGroup().addGap(185, 185, 185) .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(111, 111, 111).addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(ThongKeMuonTraLayout.createSequentialGroup().addGap(270, 270, 270) .addGroup(ThongKeMuonTraLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jButton9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton8, javax.swing.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE)))) .addGap(69, 69, 69).addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 641, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(186, Short.MAX_VALUE))); ThongKeMuonTraLayout.setVerticalGroup(ThongKeMuonTraLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(ThongKeMuonTraLayout.createSequentialGroup().addGap(43, 43, 43) .addGroup(ThongKeMuonTraLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(ThongKeMuonTraLayout.createSequentialGroup().addGap(133, 133, 133) .addGroup(ThongKeMuonTraLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(124, 124, 124).addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(34, 34, 34).addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(162, Short.MAX_VALUE))); jTabbedPane1.addTab("Thng K Mn Tr", ThongKeMuonTra); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1540, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap().addComponent(jTabbedPane1).addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed resetTable(jTable2); thongke(jComboBox2.getSelectedIndex(), jTable2); }//GEN-LAST:event_jButton3ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed xuatFile(jTable2); }//GEN-LAST:event_jButton2ActionPerformed private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed this.dispose(); LibrarianForm newLibrarianForm = new LibrarianForm(); newLibrarianForm.showWindows(); }//GEN-LAST:event_jButton6ActionPerformed private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jButton7ActionPerformed private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jButton8ActionPerformed private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed this.dispose(); LibrarianForm newLibrarianForm = new LibrarianForm(); newLibrarianForm.showWindows(); }//GEN-LAST:event_jButton9ActionPerformed private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton12ActionPerformed this.dispose(); LibrarianForm newLibrarianForm = new LibrarianForm(); newLibrarianForm.showWindows(); }//GEN-LAST:event_jButton12ActionPerformed private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed xuatFile(jTable1); }//GEN-LAST:event_jButton11ActionPerformed private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed resetTable(jTable1); thongke(jComboBox1.getSelectedIndex(), jTable1); }//GEN-LAST:event_jButton10ActionPerformed /** * @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(ThongKe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(ThongKe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(ThongKe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(ThongKe.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 ThongKe().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel ThongKeDocGia; private javax.swing.JPanel ThongKeMuonTra; private javax.swing.JPanel ThongKeSach; private javax.swing.JButton jButton10; private javax.swing.JButton jButton11; private javax.swing.JButton jButton12; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton6; private javax.swing.JButton jButton7; private javax.swing.JButton jButton8; private javax.swing.JButton jButton9; private javax.swing.JComboBox<String> jComboBox1; private javax.swing.JComboBox<String> jComboBox2; private javax.swing.JComboBox<String> jComboBox3; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JScrollPane jScrollPane4; private javax.swing.JTabbedPane jTabbedPane1; private javax.swing.JTable jTable1; private javax.swing.JTable jTable2; private javax.swing.JTable jTable3; // End of variables declaration//GEN-END:variables private void thongke(int loai, JTable table) { try { Model.ThuVien.resetTable(table); String loaiThongKe = "", bang = ""; int soTheLoai = 0; if (table == jTable1) { bang = "Sach"; if (loai == 0) loaiThongKe = "TheLoai"; if (loai == 1) loaiThongKe = "Nxb"; if (loai == 2) loaiThongKe = "NamXB"; if (loai == 3) loaiThongKe = "TacGia"; } else if (table == jTable2) { bang = "DocGia"; if (loai == 0) loaiThongKe = "Lop"; if (loai == 1) loaiThongKe = "Khoa"; if (loai == 2) loaiThongKe = "GioiTinh"; if (loai == 3) loaiThongKe = "DiaChi"; } String sql = "SELECT " + loaiThongKe + ",count(" + loaiThongKe + ") FROM " + bang + " GROUP BY " + loaiThongKe + " ASC;"; System.out.println(sql); DBConnection db = new DBConnection(); db.getConnection(); db.excuteQuery(sql); while (db.result.next()) { soTheLoai++; } System.out.println(soTheLoai); if (soTheLoai > 0) { db.result.beforeFirst(); dm = (DefaultTableModel) table.getModel(); while (db.result.next()) { String[] rowData = { db.result.getString(loaiThongKe), db.result.getString("count(" + loaiThongKe + ")") }; System.out.println(loaiThongKe); System.out.println("count(" + loaiThongKe + ")"); dm.addRow(rowData); } } } catch (SQLException ex) { JOptionPane.showMessageDialog(null, "? c li xy ra +thongke", "ERROR", 2); } } private void resetTable(JTable table) { dm = (DefaultTableModel) table.getModel(); dm.setColumnCount(0); if (table == jTable1) dm.addColumn((String) jComboBox1.getSelectedItem()); if (table == jTable2) dm.addColumn((String) jComboBox2.getSelectedItem()); dm.addColumn("S Lng"); } private void xuatFile(JTable table) { String url = "", title = " Thng K ?c Gi", loaiThongKe = (String) jComboBox2.getSelectedItem(); ; String[] tenCot = { loaiThongKe, "S Lng" }; ; if (table == jTable1) { url = "ThongKeSach"; title = " Thng K Sch"; loaiThongKe = (String) jComboBox1.getSelectedItem(); tenCot[0] = loaiThongKe; } if (table == jTable2) url = "ThongKeDocGia"; String bf = "H? v tn : L Ng?c Long\n" + "MSSV : 20142659\n" + "?? ti : Xy dng chng trnh qun l th vin"; bf += "\n\nTr?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() + " THEO "; bf += loaiThongKe.toUpperCase(); String af = "H Ni, Vit Nam Ngy " + Model.ThuVien.getTodayNgayThangNam() + "\n" + "Ng?i to bng : L Ng?c Long"; //Model.ThuVien.xuatFilePDF(url,table,title,PageSize.A3); Model.ThuVien.xuatFilePDF2(url, bf, tenCot, af, table, title, PageSize.A4); } }