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 ReportGeneration; import Registration.DataBase; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import java.sql.ResultSet; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.logging.Level; import java.util.logging.Logger; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Vector; import javax.swing.table.DefaultTableModel; /** * * @author Wilma */ public class GenerateReport extends javax.swing.JPanel { private DataBase genReport; private ResultSet records; /** * Creates new form GenerateReport */ public GenerateReport() { 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() { jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jXDatePicker2 = new org.jdesktop.swingx.JXDatePicker(); jXDatePicker3 = new org.jdesktop.swingx.JXDatePicker(); jLabel4 = new javax.swing.JLabel(); jLabel1.setFont(new java.awt.Font("Vani", 1, 14)); // NOI18N jLabel1.setText("Attendance Report"); jButton1.setText("Print"); jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "SN", "NAME", "MATRICULE NUMBER", "DATE", "TIME" }) { Class[] types = new Class[] { java.lang.Integer.class, java.lang.String.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class }; boolean[] canEdit = new boolean[] { false, false, false, false, false }; public Class getColumnClass(int columnIndex) { return types[columnIndex]; } public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit[columnIndex]; } }); jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS); jScrollPane1.setViewportView(jTable1); jButton2.setText("Save"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setText("Generate"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jLabel2.setFont(new java.awt.Font("Vani", 1, 12)); // NOI18N jLabel2.setText("From"); jLabel3.setFont(new java.awt.Font("Vani", 1, 12)); // NOI18N jLabel3.setText("To"); jLabel4.setFont(new java.awt.Font("Vani", 1, 12)); // NOI18N jLabel4.setForeground(new java.awt.Color(255, 0, 0)); 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().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 582, Short.MAX_VALUE) .addContainerGap()) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup().addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jXDatePicker2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(31, 31, 31).addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jXDatePicker3, 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(jButton3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton1).addGap(24, 24, 24)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup().addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(jLabel4)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1).addComponent(jButton2).addComponent(jButton3) .addComponent(jLabel3).addComponent(jXDatePicker3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(jXDatePicker2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(18, 18, 18) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.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())); }// </editor-fold>//GEN-END:initComponents private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed report(); }//GEN-LAST:event_jButton3ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed print(); // TODO add your handling code here: }//GEN-LAST:event_jButton2ActionPerformed String studName; public void report() { if (jXDatePicker2.getDate() == null || jXDatePicker3.getDate() == null) { System.out.println("Error,choose a date"); jLabel4.setText("Please, you must select a date"); } else { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); String date2 = df.format(jXDatePicker2.getDate()).toString(); String date3 = df.format(jXDatePicker3.getDate()).toString(); System.out.println(date2); genReport = DataBase.getInstance(); genReport.connect(); String query = "select `registered personel`.Name, `registered personel`.Matricule_Number, `Attendance Record`.Date, `Attendance Record`.Time from `Registered Personel` join `Attendance Record` on `registered personel`.Tag_ID =`Attendance Record`.`Registered Personel_Tag_ID` where Date>='" + date2 + "' AND Date<='" + date3 + "' order by Date"; genReport.query(query); System.out.println(query); records = genReport.getResultSet(); try { DefaultTableModel table_mod = (DefaultTableModel) jTable1.getModel(); Vector RowData; table_mod.setRowCount(0); int i = 0; while (records.next()) { RowData = new Vector(); studName = records.getString(1); RowData.add(i + 1); RowData.add(records.getString(1)); RowData.add(records.getString(2)); RowData.add(records.getString(3)); RowData.add(records.getString(4)); table_mod.addRow(RowData); i++; } } catch (Exception ex) { Logger.getLogger(RecordAttendance.class.getName()).log(Level.SEVERE, null, ex); } } } public void print() { FileOutputStream fileOut = null; try { Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50); //Document document = new Document(); //Create PDF cocument fileOut = new FileOutputStream(new File("report.pdf")); PdfWriter writer = PdfWriter.getInstance(document, fileOut); document.open(); //Open the document for writting PdfPTable table = new PdfPTable(5); //Create a table with 7 columns. 6 the first column //is for labeling the day, and the rest are for displaying actual time table entries. document.add(new Paragraph(" RFID ATTENDANCE SYSTEM ")); table.setWidthPercentage(100); PdfPCell cell; cell = new PdfPCell(); cell.setColspan(1); cell.addElement(new Phrase("S/N")); table.addCell(cell); cell = new PdfPCell(); cell.addElement(new Phrase("Name")); table.addCell(cell); cell = new PdfPCell(); cell.addElement(new Phrase("Matricule Number")); table.addCell(cell); cell = new PdfPCell(); cell.addElement(new Phrase("Date")); table.addCell(cell); cell = new PdfPCell(); cell.addElement(new Phrase("Time")); table.addCell(cell); int i = 0; DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); String date2 = df.format(jXDatePicker2.getDate()).toString(); String date3 = df.format(jXDatePicker3.getDate()).toString(); genReport = DataBase.getInstance(); genReport.connect(); String query = "select `registered personel`.Name, `registered personel`.Matricule_Number, `Attendance Record`.Date, `Attendance Record`.Time from `Registered Personel` join `Attendance Record` on `registered personel`.Tag_ID =`Attendance Record`.`Registered Personel_Tag_ID` where Date>='" + date2 + "' AND Date<='" + date3 + "'"; genReport.query(query); records = genReport.getResultSet(); while (records.next()) { studName = records.getString(1); cell = new PdfPCell(); cell.addElement(new Phrase("" + i)); table.addCell(cell); cell = new PdfPCell(); cell.addElement(new Phrase(records.getString(1))); table.addCell(cell); cell = new PdfPCell(); cell.addElement(new Phrase(records.getString(2))); table.addCell(cell); cell = new PdfPCell(); cell.addElement(new Phrase(records.getString(3))); table.addCell(cell); cell = new PdfPCell(); cell.addElement(new Phrase(records.getString(4))); table.addCell(cell); i++; } document.add(table); document.close(); } catch (Exception ex) { Logger.getLogger(GenerateReport.class.getName()).log(Level.SEVERE, null, ex); } finally { try { fileOut.close(); } catch (IOException ex) { Logger.getLogger(GenerateReport.class.getName()).log(Level.SEVERE, null, ex); } } } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable jTable1; private org.jdesktop.swingx.JXDatePicker jXDatePicker2; private org.jdesktop.swingx.JXDatePicker jXDatePicker3; // End of variables declaration//GEN-END:variables }