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 com.thelinh.views; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import com.thelinh.controller.LoadTable; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.sql.ResultSet; import java.text.SimpleDateFormat; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.JTextArea; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; /** * * @author Admin */ public class Statistics extends javax.swing.JFrame { /** * Creates new form NewJFrame */ public Statistics() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { buttonGroup1 = new javax.swing.ButtonGroup(); jLabel1 = new javax.swing.JLabel(); radBook = new javax.swing.JRadioButton(); radClient = new javax.swing.JRadioButton(); radCheckOut = new javax.swing.JRadioButton(); btnStatistics = new javax.swing.JButton(); btnPrint = new javax.swing.JButton(); jLabel5 = new javax.swing.JLabel(); jScrollPane2 = new javax.swing.JScrollPane(); taReport = new javax.swing.JTextArea(); jLabel2 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setText("Statistics"); buttonGroup1.add(radBook); radBook.setText("Statistics Book"); buttonGroup1.add(radClient); radClient.setText("Statistics Client"); buttonGroup1.add(radCheckOut); radCheckOut.setText("Statistics CheckOut"); btnStatistics.setText("Statistics"); btnStatistics.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnStatisticsActionPerformed(evt); } }); btnPrint.setText("Print Report"); btnPrint.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnPrintActionPerformed(evt); } }); jLabel5.setText("Report"); taReport.setColumns(20); taReport.setRows(5); jScrollPane2.setViewportView(taReport); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(89, 89, 89) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(radBook).addComponent(radClient).addComponent(radCheckOut) .addComponent(btnStatistics) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(btnPrint).addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(47, 47, 47).addComponent(jLabel2)))) .addGroup(layout.createSequentialGroup().addGap(288, 288, 288) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1).addComponent(jLabel5)))) .addContainerGap(163, Short.MAX_VALUE))); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] { radBook, radCheckOut, radClient }); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(31, 31, 31).addComponent(jLabel1).addGap(14, 14, 14) .addComponent(radBook).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(radClient).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(radCheckOut) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnStatistics) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE) .addComponent(jLabel2).addGap(453, 453, 453)) .addGroup(layout.createSequentialGroup().addGap(6, 6, 6).addComponent(jLabel5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(btnPrint) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))); pack(); }// </editor-fold>//GEN-END:initComponents private void btnStatisticsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStatisticsActionPerformed if (radBook.isSelected()) { try { String sqlBook1 = "SELECT Count(BookId) AS bookAll FROM Book"; String sqlBook2 = "SELECT Count(BookId) AS bookBorrow FROM Detail"; ResultSet rs1 = LoadTable.Display(sqlBook1); ResultSet rs2 = LoadTable.Display(sqlBook2); this.taReport.setText(""); this.taReport.append("Statistic by Book"); if (rs1.next()) { this.taReport.append("A total is " + rs1.getInt("bookAll") + " books"); } if (rs2.next()) { this.taReport.append("A total of " + rs2.getInt("bookBorrow") + " were borrowed"); this.taReport.append( "A total of " + (rs1.getInt("bookAll") - rs2.getInt("bookBorrow")) + " in stock"); } } catch (Exception ex) { ex.printStackTrace(); } } else if (radClient.isSelected()) { try { String sqlClient1 = "SELECT Count(ClientId) AS clientAll FROM Client"; String sqlClient2 = "SELECT Count(ClientId) AS clientBorrow FROM CheckOut"; ResultSet rs1 = LoadTable.Display(sqlClient1); ResultSet rs2 = LoadTable.Display(sqlClient2); this.taReport.setText(""); this.taReport.append("Statistic by Client"); if (rs1.next()) { this.taReport.append("A total is " + rs1.getInt("clientAll") + " clients"); } if (rs2.next()) { this.taReport.append("A total of " + rs2.getInt("clientBorrow") + " clients borrowed"); this.taReport.append("A total of " + (rs1.getInt("clientAll") - rs2.getInt("clientBorrow")) + " clients did not borrow"); } } catch (Exception e) { e.printStackTrace(); } } else if (radCheckOut.isSelected()) { try { String sqlCheckOut1 = "SELECT Count(CheckOutId) AS checkOutAll FROM CheckOut"; String sqlCheckOut2 = "SELECT Count(DISTINCT CheckOut.CheckOutId) AS checkOutDeadLine FROM CheckOut,Detail WHERE CheckOut.CheckOutId = Detail.CheckOutId AND DeadLine < (SELECT CURRENT_DATE)"; ResultSet rs1 = LoadTable.Display(sqlCheckOut1); ResultSet rs2 = LoadTable.Display(sqlCheckOut2); int checkOutAll = 0; int checkOutDeadLine = 0; if (rs1.next()) { this.taReport.append("A total is " + rs1.getInt("checkOutAll") + " checkouts"); } if (rs2.next()) { this.taReport.append("A total of " + rs2.getInt("checkOutDeadLine") + "checkouts time out"); } } catch (Exception e) { e.printStackTrace(); } } }//GEN-LAST:event_btnStatisticsActionPerformed /* public static boolean saveFile(JTextArea ta, String path){ try { FileOutputStream fos = new FileOutputStream(path); OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8"); BufferedWriter bw = new BufferedWriter(osw); bw.write(ta.getText()); bw.close(); osw.close(); fos.close(); return true; } catch (Exception e) { e.printStackTrace(); } return false; }*/ private void btnPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrintActionPerformed Document document = new Document() { }; try { JFileChooser jfc = new JFileChooser("Save File"); if (jfc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) { String content = this.taReport.getText(); jfc.setDialogTitle("Save File"); FileOutputStream fos = new FileOutputStream(jfc.getSelectedFile()); PdfWriter.getInstance(document, fos); document.open(); document.add(new Paragraph(content)); document.close(); JOptionPane.showMessageDialog(null, "Save success"); } } catch (FileNotFoundException ex) { Logger.getLogger(Statistics.class.getName()).log(Level.SEVERE, null, ex); } catch (DocumentException ex) { Logger.getLogger(Statistics.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_btnPrintActionPerformed /** * @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(Statistics.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Statistics.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Statistics.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Statistics.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 Statistics().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnPrint; private javax.swing.JButton btnStatistics; private javax.swing.ButtonGroup buttonGroup1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel5; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JRadioButton radBook; private javax.swing.JRadioButton radCheckOut; private javax.swing.JRadioButton radClient; private javax.swing.JTextArea taReport; // End of variables declaration//GEN-END:variables }