employeepayroll.Searchempsalary.java Source code

Java tutorial

Introduction

Here is the source code for employeepayroll.Searchempsalary.java

Source

/*
 * 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 employeepayroll;

/**
 *
 * @author makh
 */
import com.itextpdf.text.Document;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileOutputStream;
import static java.lang.Integer.parseInt;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class Searchempsalary extends javax.swing.JFrame {
    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement pst = null;

    /**
     * Creates new form searchempsalary
     */
    public Searchempsalary() {
        initComponents();
        conn = db.java_db();
        Toolkit toolkit = getToolkit();
        Dimension size = toolkit.getScreenSize();
        setLocation(size.width / 2 - getWidth() / 2, size.height / 2 - getHeight() / 2);
    }

    /**
     * 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() {

        jLabel1 = new javax.swing.JLabel();
        txt_search = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txt_id = new javax.swing.JTextField();
        txt_fname = new javax.swing.JTextField();
        txt_sname = new javax.swing.JTextField();
        txt_dob = new javax.swing.JTextField();
        txt_dept = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        txt_dest = new javax.swing.JTextField();
        txt_stat = new javax.swing.JTextField();
        txt_doh = new javax.swing.JTextField();
        txt_title = new javax.swing.JTextField();
        txt_sal = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Enter Employee ID:");

        txt_search.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txt_searchKeyReleased(evt);
            }
        });

        jLabel2.setText("Employee ID:");

        jLabel3.setText("First Name: ");

        jLabel4.setText("SurName: ");

        jLabel5.setText("Date Of Birth: ");

        jLabel6.setText("Department: ");

        jLabel7.setText("Designation:");

        jLabel8.setText("Status:");

        jLabel9.setText("Date Hired:");

        jLabel10.setText("Job Title:");

        jLabel11.setText("Basic Salary:");

        jButton1.setText("Generate Slip");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        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(319, Short.MAX_VALUE)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 209,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(296, 296, 296))
                .addGroup(layout.createSequentialGroup().addGap(56, 56, 56).addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup().addComponent(jLabel1).addGap(18, 18, 18)
                                .addComponent(txt_search))
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel5).addComponent(jLabel6).addComponent(jLabel4)
                                        .addComponent(jLabel3).addComponent(jLabel2))
                                .addGap(18, 18, 18)
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(txt_id)
                                                .addComponent(txt_fname, javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(txt_sname)
                                                .addComponent(txt_dept, javax.swing.GroupLayout.DEFAULT_SIZE, 200,
                                                        Short.MAX_VALUE)
                                                .addComponent(txt_dob))
                                .addGap(82, 82, 82)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup().addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel7).addComponent(jLabel8)).addGap(18, 18, 18)
                                                .addGroup(layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(txt_stat,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE, 200,
                                                                Short.MAX_VALUE)
                                                        .addComponent(txt_dest)))
                                        .addGroup(layout.createSequentialGroup().addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel9).addComponent(jLabel10)
                                                .addComponent(jLabel11)).addGap(21, 21, 21)
                                                .addGroup(layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(txt_doh).addComponent(txt_title)
                                                        .addComponent(txt_sal, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                200, Short.MAX_VALUE))))))
                        .addGap(0, 0, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(67, 67, 67).addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1)
                        .addComponent(txt_search, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(36, 36, 36)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2)
                                .addComponent(txt_id, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel7).addComponent(txt_dest,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3)
                                .addComponent(txt_fname, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel8).addComponent(txt_stat,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel4)
                                .addComponent(txt_sname, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel9).addComponent(txt_doh, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel5)
                                .addComponent(txt_dob, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel10).addComponent(txt_title,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel6)
                                .addComponent(txt_dept, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel11)
                                .addComponent(txt_sal, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 88,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(49, 49, 49)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txt_searchKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_searchKeyReleased
        // TODO add your handling code here:
        try {
            String sql = "select * from emp_info where id=? ";
            pst = conn.prepareStatement(sql);
            pst.setString(1, txt_search.getText());
            rs = pst.executeQuery();
            if (rs.next()) {
                String add1 = rs.getString("id");
                txt_id.setText(add1);
                String add2 = rs.getString("fname");
                txt_fname.setText(add2);
                String add3 = rs.getString("sname");
                txt_sname.setText(add3);
                String add4 = rs.getString("dob");
                txt_dob.setText(add4);
                String add5 = rs.getString("dept");
                txt_dept.setText(add5);
                String add7 = rs.getString("salary");
                txt_sal.setText(add7);
                String add8 = rs.getString("status");
                txt_stat.setText(add8);
                String add9 = rs.getString("hiringdate");
                txt_doh.setText(add9);
                String add10 = rs.getString("job_title");
                txt_title.setText(add10);
                String add17 = rs.getString("designation");
                txt_dest.setText(add17);
            } else {
                txt_fname.setText(value1);
                txt_id.setText(value1);
                txt_sname.setText(value1);
                txt_dob.setText(value1);
                txt_dept.setText(value1);
                txt_sal.setText(value1);
                txt_stat.setText(value1);
                txt_doh.setText(value1);
                txt_title.setText(value1);
                txt_dest.setText(value1);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "No Data");
        } finally {
            try {
                rs.close();
                pst.close();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Connec not closed");
            }
        }
    }//GEN-LAST:event_txt_searchKeyReleased

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        String value = txt_fname.getText();
        String value0 = txt_sname.getText();
        int value1 = parseInt(txt_id.getText());
        String value2 = txt_dest.getText();
        String value3 = txt_dept.getText();
        String overtime = " ";
        String medical = " ";
        String bonus = " ";
        String other = " ";
        String earnings = " ";

        JFileChooser dialog = new JFileChooser();
        dialog.setSelectedFile(new File(value + " " + value0 + "-SalarySlip" + ".pdf"));
        int dialogResult = dialog.showSaveDialog(null);
        if (dialogResult == JFileChooser.APPROVE_OPTION) {
            String filePath = dialog.getSelectedFile().getPath();
            try {
                String sql = "select * from deductions where emp_id =" + value1;
                pst = conn.prepareStatement(sql);
                rs = pst.executeQuery();
                while (rs.next()) {
                    val = rs.getString(5);
                    reason = rs.getString(6);
                    v = Integer.parseInt(val);
                }
                rs.close();
                pst.close();

                String sql2 = "select * from allowance where emp_id =" + value1;
                pst = conn.prepareStatement(sql2);
                rs = pst.executeQuery();
                while (rs.next()) {
                    x = Float.valueOf(rs.getString(9));
                    overtime = rs.getString(2);
                    medical = rs.getString(3);
                    bonus = rs.getString(4);
                    other = rs.getString(5);
                    earnings = rs.getString(9);
                }
                //v = Integer.parseInt(val);
                int calcTotal = Integer.parseInt(txt_sal.getText());
                float total = calcTotal + x - v;
                Document myDocument = new Document();
                PdfWriter myWriter = PdfWriter.getInstance(myDocument, new FileOutputStream(filePath));
                myDocument.open();
                myDocument
                        .add(new Paragraph("PAY SLIP", FontFactory.getFont(FontFactory.TIMES_BOLD, 20, Font.BOLD)));
                myDocument.add(new Paragraph(new Date().toString()));
                myDocument.add(new Paragraph(
                        "-------------------------------------------------------------------------------------------"));
                myDocument.add((new Paragraph("EMPLOYEE DETAILS",
                        FontFactory.getFont(FontFactory.TIMES_ROMAN, 15, Font.BOLD))));
                myDocument.add((new Paragraph("Name of Employee: " + value + " " + value0,
                        FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN))));
                myDocument.add((new Paragraph("Designation: " + value2,
                        FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN))));
                myDocument.add((new Paragraph("Department: " + value3,
                        FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN))));
                myDocument.add(new Paragraph(
                        "-------------------------------------------------------------------------------------------"));
                myDocument
                        .add(new Paragraph("SALARY", FontFactory.getFont(FontFactory.TIMES_ROMAN, 15, Font.BOLD)));
                myDocument.add(new Paragraph("Basic Salary: $" + calcTotal,
                        FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN)));
                myDocument.add(new Paragraph("Overtime: " + overtime + " Hours",
                        FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN)));
                myDocument.add(new Paragraph("Medical: $" + medical,
                        FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN)));
                myDocument.add(new Paragraph("Bonus: $" + bonus,
                        FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN)));
                myDocument.add(new Paragraph("Other: $" + other,
                        FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN)));
                myDocument.add(new Paragraph(
                        "-------------------------------------------------------------------------------------------"));
                myDocument.add(
                        new Paragraph("DEDUCTION", FontFactory.getFont(FontFactory.TIMES_ROMAN, 15, Font.BOLD)));
                myDocument.add(new Paragraph("Deduction Details: " + reason,
                        FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN)));
                myDocument.add(new Paragraph("Total Deductions : $" + val,
                        FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN)));
                myDocument.add(new Paragraph(
                        "-------------------------------------------------------------------------------------------"));
                myDocument.add(new Paragraph("TOTAL PAYMENT",
                        FontFactory.getFont(FontFactory.TIMES_ROMAN, 15, Font.BOLD)));
                myDocument.add(new Paragraph("Total Earnings: " + earnings,
                        FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN)));
                myDocument.add(new Paragraph("Net Pay : " + total,
                        FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN)));
                myDocument.add(new Paragraph(
                        "-------------------------------------------------------------------------------------------"));
                myDocument.newPage();
                myDocument.close();
                JOptionPane.showMessageDialog(null, "Report was successfully generated");
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e + "\n creating report problem 00x333");
            } finally {
                try {
                    rs.close();
                    pst.close();
                } catch (SQLException e) {
                    JOptionPane.showMessageDialog(null, e + "/n creating report problem 00x341");
                }
            }
        }
    }//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 ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Searchempsalary.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Searchempsalary.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Searchempsalary.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Searchempsalary.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 Searchempsalary().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    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.JTextField txt_dept;
    private javax.swing.JTextField txt_dest;
    private javax.swing.JTextField txt_dob;
    private javax.swing.JTextField txt_doh;
    private javax.swing.JTextField txt_fname;
    private javax.swing.JTextField txt_id;
    private javax.swing.JTextField txt_sal;
    private javax.swing.JTextField txt_search;
    private javax.swing.JTextField txt_sname;
    private javax.swing.JTextField txt_stat;
    private javax.swing.JTextField txt_title;
    // End of variables declaration//GEN-END:variables
    String value1 = "";
    String val = "0";
    String reason = " ";
    float x = 0;
    int v = 0;
}