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 gui; import com.lowagie.text.Document; import com.lowagie.text.PageSize; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfWriter; import core.ExcelExporter; import core.SendMailWithAttachment; import db.DBConnection; import java.awt.BorderLayout; import java.awt.Graphics2D; import java.awt.Shape; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Vector; import javax.swing.JFrame; import javax.swing.table.DefaultTableModel; /** * * @author HOME */ public class TransHistory extends javax.swing.JFrame { /** * Creates new form TransHistory */ Transactions t; String fDate; String toDate; String accNumber; String query; File f; Connection con; ResultSet rs; ResultSetMetaData rsmd; PreparedStatement psmt; public TransHistory() { initComponents(); } public TransHistory(Transactions trans) { initComponents(); t = trans; accnoLbl.setText(t.accNo); todateLbl.setText(t.toDate3); fdateLbl.setText(t.fDate3); fDate = t.fDate3; toDate = t.toDate3; try { con = DBConnection.getConnection(); query = "select account_type,name from user_account,profile " + "where account_no=? and username=? and user_id=?"; psmt = con.prepareStatement(query); psmt.setString(1, t.accNo); psmt.setString(2, t.user); psmt.setString(3, t.user); rs = psmt.executeQuery(); if (rs.next()) { nameLbl.setText(rs.getString("name")); typeLbl.setText(rs.getString("account_type")); System.out.println(rs.getString("name")); } display(); } catch (Exception ex) { ex.printStackTrace(); } } public TransHistory(Transactions trans, String s) throws Exception { initComponents(); t = trans; accnoLbl.setText(t.accNo); todateLbl.setText(t.toDate3); fdateLbl.setText(t.fDate3); fDate = t.fDate3; toDate = t.toDate3; try { con = DBConnection.getConnection(); query = "select account_type,name from user_account,profile " + "where account_no=? and username=? and user_id=?"; psmt = con.prepareStatement(query); psmt.setString(1, t.accNo); psmt.setString(2, t.user); psmt.setString(3, t.user); rs = psmt.executeQuery(); if (rs.next()) { nameLbl.setText(rs.getString("name")); typeLbl.setText(rs.getString("account_type")); System.out.println(rs.getString("name")); } if (s.compareToIgnoreCase("text") == 0) { ConvertText(); } else if (s.compareToIgnoreCase("excel") == 0) { ConvertExcel(); } else { Convertpdf(); } } catch (Exception ex) { ex.printStackTrace(); } } /** * 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(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); accnoLbl = new javax.swing.JLabel(); typeLbl = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); nameLbl = new javax.swing.JLabel(); jLabel8 = new javax.swing.JLabel(); jLabel9 = new javax.swing.JLabel(); fdateLbl = new javax.swing.JLabel(); todateLbl = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); printTHBtn = new javax.swing.JButton(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N jLabel1.setForeground(new java.awt.Color(0, 204, 153)); jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel1.setText("Transactions History"); jLabel2.setText("Account Number :"); jLabel3.setText("Type :"); jLabel6.setText("Name :"); jLabel8.setText("From Date :"); jLabel9.setText("To Date :"); jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "Account Number", "Post Date", "Value Date", "Branch", "Cheque Number", "Description", "Debit", "Credit", "Balance", "User ID" })); jScrollPane1.setViewportView(jTable1); printTHBtn.setText("Print"); printTHBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { printTHBtnActionPerformed(evt); } }); jButton1.setText("Cancel"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(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(169, 169, 169) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING).addGroup( jPanel1Layout.createSequentialGroup() .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(31, 31, 31).addComponent(accnoLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(18, 18, 18) .addComponent(nameLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 61, 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(jPanel1Layout.createSequentialGroup() .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(31, 31, 31).addComponent(typeLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(fdateLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(todateLbl))) .addGap(16, 16, 16)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 576, Short.MAX_VALUE) .addContainerGap()))) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(printTHBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton1).addGap(27, 27, 27))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(jLabel1) .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(typeLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(jLabel3).addComponent(accnoLbl))) .addGap(28, 28, 28) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel8).addComponent(jLabel9).addComponent(fdateLbl) .addComponent(todateLbl).addComponent(jLabel6)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(nameLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(printTHBtn).addComponent(jButton1)) .addContainerGap())); 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.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(19, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents public void display() throws Exception { System.out.println(fDate); System.out.println(toDate); psmt = con.prepareStatement("select * from user_transactions where account_no = '" + t.accNo + "' and post_date between ? and ?"); psmt.setDate(1, Date.valueOf(fDate)); psmt.setDate(2, Date.valueOf(toDate)); rs = psmt.executeQuery(); rsmd = rs.getMetaData(); DefaultTableModel dtm = new DefaultTableModel(); Vector column = new Vector(); int count = rsmd.getColumnCount(); for (int i = 2; i <= count; i++) { column.addElement(rsmd.getColumnName(i)); } dtm.setColumnIdentifiers(column); while (rs.next()) { Vector row = new Vector(); for (int i = 2; i <= count; i++) { String k = rs.getString(i); row.addElement(k); System.out.println(k); } dtm.addRow(row); } jTable1.setModel(dtm); } public void ConvertText() throws Exception { display(); //create text file FileWriter f = new FileWriter("text.txt"); BufferedWriter bfw = new BufferedWriter(f); for (int j = 0; j < jTable1.getColumnCount(); j++) { bfw.write((String) (jTable1.getColumnName(j))); bfw.write("\t\t"); } bfw.newLine(); for (int i = 0; i < jTable1.getRowCount(); i++) { for (int j = 0; j < jTable1.getColumnCount(); j++) { if (j >= 4) { bfw.write("\t"); } try { bfw.write((String) (jTable1.getValueAt(i, j))); } catch (NullPointerException e) { bfw.write("-\t"); } bfw.write("\t"); } bfw.newLine(); } bfw.close(); f.close(); //send mail query = "select email from profile_id where user_id = ?"; psmt = con.prepareStatement(query); psmt.setString(1, t.user); rs = psmt.executeQuery(); rs.next(); SendMailWithAttachment smail = new SendMailWithAttachment(); String message = "hereby is the requested transction report of account " + "no. = " + t.accNo + " from date " + fDate + " to " + toDate; smail.send(rs.getString(1), "text.txt", message); } public void ConvertExcel() throws Exception { display(); //create excel file ExcelExporter exp = new ExcelExporter(); f = new File("excel.xls"); exp.exportTable(jTable1, f); //send mail query = "select email from profile_id where user_id = ?"; psmt = con.prepareStatement(query); psmt.setString(1, t.user); rs = psmt.executeQuery(); rs.next(); SendMailWithAttachment smail = new SendMailWithAttachment(); String message = "hereby is the requested transction report of account " + "no. = " + t.accNo + " from date " + fDate + " to " + toDate; String k = rs.getString(1); System.out.println(k); smail.send(k, f.getName(), message); } public void Convertpdf() throws Exception { display(); Document document = new Document(PageSize.A4.rotate()); try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("Table.pdf")); document.open(); PdfContentByte cb = writer.getDirectContent(); cb.saveState(); Graphics2D g2 = cb.createGraphics(500, 500); Shape oldClip = g2.getClip(); g2.clipRect(20, 20, 500, 500); jTable1.print(g2); jTable1.getTableHeader().paint(g2); g2.setClip(oldClip); g2.dispose(); cb.restoreState(); cb.saveState(); } catch (Exception e) { System.err.println(e.getMessage()); } document.close(); //send mail query = "select email from profile_id where user_id = ?"; psmt = con.prepareStatement(query); psmt.setString(1, t.user); rs = psmt.executeQuery(); rs.next(); SendMailWithAttachment smail = new SendMailWithAttachment(); String message = "hereby is the requested transction report of account " + "no. = " + t.accNo + " from date " + fDate + " to " + toDate; smail.send(rs.getString(1), "Table.pdf", message); } private void printTHBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_printTHBtnActionPerformed try { jTable1.print(); } catch (Exception e) { e.printStackTrace(); } }//GEN-LAST:event_printTHBtnActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed this.dispose(); }//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 ("Windows".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(TransHistory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(TransHistory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(TransHistory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(TransHistory.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 TransHistory().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel accnoLbl; private javax.swing.JLabel fdateLbl; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable jTable1; private javax.swing.JLabel nameLbl; private javax.swing.JButton printTHBtn; private javax.swing.JLabel todateLbl; private javax.swing.JLabel typeLbl; // End of variables declaration//GEN-END:variables }