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 banking.modules; import java.sql.*; import javax.swing.*; import net.proteanit.sql.DbUtils; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PiePlot; import org.jfree.data.general.DefaultPieDataset; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; /** * * @author benson */ public class salary_comp extends javax.swing.JFrame { Connection conn = null; PreparedStatement pst = null; ResultSet rs = null; /** * Creates new form salary_comp */ public salary_comp() { initComponents(); conn = MysqlConnect.ConnectDB(); Sal_table(); } private void Sal_table() { try { String sql = "select * from salary_components"; pst = conn.prepareStatement(sql); rs = pst.executeQuery(); table_salary_comp.setModel(DbUtils.resultSetToTableModel(rs)); } catch (Exception e) { JOptionPane.showMessageDialog(null, e); } { } } /** * 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(); jScrollPane1 = new javax.swing.JScrollPane(); table_salary_comp = new javax.swing.JTable(); txtBasic = new javax.swing.JTextField(); txtHouse = new javax.swing.JTextField(); txtDearness = new javax.swing.JTextField(); txtTransport = new javax.swing.JTextField(); txtEntertainment = new javax.swing.JTextField(); txtMedical = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); 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(); jButton1 = new javax.swing.JButton(); Cmd_Save = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); table_salary_comp .setModel(new javax.swing.table.DefaultTableModel( new Object[][] { { null, null, null, null }, { null, null, null, null }, { null, null, null, null }, { null, null, null, null } }, new String[] { "Title 1", "Title 2", "Title 3", "Title 4" })); table_salary_comp.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { table_salary_compMouseClicked(evt); } }); jScrollPane1.setViewportView(table_salary_comp); jLabel1.setText("Basic Salary"); jLabel2.setText("House Allowance"); jLabel3.setText("Dearness Allowance"); jLabel4.setText("Transport Allowance"); jLabel5.setText("Entertainment Allowance"); jLabel6.setText("Medical Allowance"); jButton1.setText("GRAPHS"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); Cmd_Save.setText("UPDATE NEW RULES"); Cmd_Save.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Cmd_SaveActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3).addComponent(jLabel2)) .addGap(85, 85, 85) .addGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(txtBasic, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtHouse, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtDearness, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE) .addGroup(jPanel1Layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4).addComponent(jLabel6) .addComponent(jLabel5)) .addGap(45, 45, 45)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(Cmd_Save, javax.swing.GroupLayout.PREFERRED_SIZE, 225, 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) .addComponent(txtMedical, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(txtTransport, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE) .addComponent(txtEntertainment))) .addGap(98, 98, 98)) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jButton1).addGap(0, 0, Short.MAX_VALUE))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(57, 57, 57) .addComponent(jLabel1).addGap(18, 18, 18)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup().addContainerGap() .addComponent(txtTransport, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(26, 26, 26))) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtHouse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel5).addComponent(txtEntertainment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jLabel2)) .addGap(13, 13, 13) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtDearness, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtMedical, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup().addGap(13, 13, 13) .addComponent(jLabel3))) .addGap(24, 24, 24).addComponent(Cmd_Save)) .addGroup(jPanel1Layout.createSequentialGroup().addGap(49, 49, 49) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtBasic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel4)))) .addGap(18, 33, Short.MAX_VALUE) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jButton1).addGap(38, 38, 38))); 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(21, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 26, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: String Basic_salary = txtBasic.getText(); String House_all = txtHouse.getText(); String Dear_all = txtDearness.getText(); String Trans_all = txtTransport.getText(); String Ent_all = txtEntertainment.getText(); String Med_all = txtMedical.getText(); DefaultPieDataset pieDataset = new DefaultPieDataset(); pieDataset.setValue("Basic_salary", new Double(Basic_salary)); pieDataset.setValue("House_all", new Double(House_all)); pieDataset.setValue("Dear_all", new Double(Dear_all)); pieDataset.setValue("Trans_all", new Double(Trans_all)); pieDataset.setValue("Ent_all", new Double(Ent_all)); pieDataset.setValue("Med_all", new Double(Med_all)); JFreeChart chart = ChartFactory.createPieChart("Renumaration pie chart", pieDataset, true, true, true); PiePlot P = (PiePlot) chart.getPlot(); // P.setForegroundAlpha(TOP_ALIGNMENT); ChartFrame frame = new ChartFrame("SALARY DISTRIBUTION", chart); frame.setVisible(true); frame.setSize(450, 500); }//GEN-LAST:event_jButton1ActionPerformed private void Cmd_SaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Cmd_SaveActionPerformed // TODO add your handling code here: conn = MysqlConnect.ConnectDB(); String sql = "update salary_components set Basic_salary = ? , house_allowance = ? , dearness_allowance= ? , transport_llowance= ? , entertainment_allowance=? , medical_allowance=? WHERE id =1"; try { pst = conn.prepareStatement(sql); pst.setString(1, txtBasic.getText()); pst.setString(2, txtHouse.getText()); pst.setString(3, txtDearness.getText()); pst.setString(4, txtTransport.getText()); pst.setString(5, txtEntertainment.getText()); pst.setString(6, txtMedical.getText()); pst.execute(); JOptionPane.showMessageDialog(null, "Updated"); } catch (Exception e) { JOptionPane.showMessageDialog(null, e); } }//GEN-LAST:event_Cmd_SaveActionPerformed private void table_salary_compMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_table_salary_compMouseClicked // TODO add your handling code here: try { String sql = "Select * from salary_components where id=1"; int row = table_salary_comp.getSelectedRow(); String Table_click = (table_salary_comp.getModel().getValueAt(row, 0).toString()); pst = conn.prepareStatement(sql); rs = pst.executeQuery(); if (rs.next()) { String add1 = rs.getString("Basic_salary"); txtBasic.setText(add1); String add2 = rs.getString("house_allowance"); txtHouse.setText(add1); String add3 = rs.getString("dearness_allowance"); txtDearness.setText(add1); String add4 = rs.getString("transport_llowance"); txtTransport.setText(add4); String add5 = rs.getString("entertainment_allowance"); txtEntertainment.setText(add5); String add6 = rs.getString("medical_allowance"); txtMedical.setText(add6); } } catch (Exception e) { JOptionPane.showMessageDialog(null, e); } }//GEN-LAST:event_table_salary_compMouseClicked /** * @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(salary_comp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(salary_comp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(salary_comp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(salary_comp.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 salary_comp().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton Cmd_Save; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; 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.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable table_salary_comp; private javax.swing.JTextField txtBasic; private javax.swing.JTextField txtDearness; private javax.swing.JTextField txtEntertainment; private javax.swing.JTextField txtHouse; private javax.swing.JTextField txtMedical; private javax.swing.JTextField txtTransport; // End of variables declaration//GEN-END:variables }