Home Java Example Java Swing Add Edit And Delete Employee Information
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Add Edit And Delete Employee Information

Advertisement
In this section, you will learn how to add, edit and delete the Employee's information from the database using java swing.

Add Edit and Delete Employee Information Using Swing

     

In this section, you will learn how to add, edit and delete the Employee's information from the database using java swing. For this purpose, we have used three tabs using JTabbedPane class. Under these tabs, we have created  three forms using JLabel and JTextField class which are enclosed into a panel which is then added to the JTabbedPane.

In the first tab, we have prompted the user to enter the form fields to save the new employee's information into the database. The second tab will edit the Employee's information. For this, again, we have created the same form with two buttons. When the user enters the id and click the edit button, the information of the particular employee will be displayed on the respective textfields and the user can easily make changes in it. Then on clicking the save button, the information will get updated. In the third tab, we have allowed the user to delete the particular employee from the database. On entering the Employee id and click the button, the information of that employee will get removed from the database.

Here is the code of EmployeeInformation.java

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;

class EmployeeInformation {
JFrame f;
JPanel p1,p2,p3;
JTabbedPane tp;
ImageIcon btnimg1,btnimg2;
JLabel l1, l2, l3, l4,l5,l6,l7,l8,l9,l10;
JTextField tf1,tf2,tf3,tf4,tf5,tf6,tf7,tf8,tf9,tf10;
JScrollPane sp1;
JButton savebtn,resetbtn,editbtn1,editbtn2,deletebtn ;

EmployeeInformation(){
f=new JFrame("Form");
p1=new JPanel(new GridLayout(5,2));
p2=new JPanel(new GridLayout(5,2));
p3=new JPanel(new GridLayout(2,2));
tp=new JTabbedPane();
l1=new JLabel("Employee ID:");
l2=new JLabel("Employee Name:");
l3=new JLabel("Employee Address:");
l4=new JLabel("Salary:");
l5=new JLabel("Enter Employee ID to delete record:");

l7=new JLabel("Employee ID:");
l8=new JLabel("Employee Name:");
l9=new JLabel("Employee Address:");
l10=new JLabel("Salary:");
tf1=new JTextField(12);
tf2=new JTextField(12);
tf3=new JTextField(12);
tf4=new JTextField(12);
tf5=new JTextField(12);
tf6=new JTextField(12);
tf7=new JTextField(12);
tf8=new JTextField(12);
tf9=new JTextField(12);
tf10=new JTextField(12);
savebtn=new JButton(" Add ");
resetbtn=new JButton(" Reset");
editbtn1=new JButton(" Edit ");
editbtn2=new JButton(" Save");
deletebtn=new JButton("Delete");
p1.add(l1);
p1.add(tf1);
p1.add(l2);
p1.add(tf2);
p1.add(l3);
p1.add(tf3);
p1.add(l4);
p1.add(tf4);
p1.add(savebtn);
p1.add(resetbtn);

p2.add(l7);
p2.add(tf7);
p2.add(l8);
p2.add(tf8);
p2.add(l9);
p2.add(tf9);
p2.add(l10);
p2.add(tf10);
p2.add(editbtn1);
p2.add(editbtn2);

p3.add(l5);
p3.add(tf5);
p3.add(deletebtn);
resetbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
}
});
savebtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String value1=tf1.getText();
String value2=tf2.getText();
String value3=tf3.getText();
String value4=tf4.getText();
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
System.out.println(value1+value2+value3+value4);
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
PreparedStatement st=con.prepareStatement("insert into employee(emp_id,emp_name,emp_address,
salary) values(?,?,?,?)"
);
st.setString(1,value1);
st.setString(2,value2);
st.setString(3,value3);
st.setString(4,value4);
st.executeUpdate();
JOptionPane.showMessageDialog(p1,"Data is successfully inserted into database.");
con.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(p1,"Error in submitting data!");
}
}
});

deletebtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){

String value1=tf5.getText();
Connection con = null;
 String url = "jdbc:mysql://localhost:3306/";
 String db = "test";
 String driver = "com.mysql.jdbc.Driver";
 String user = "root";
 String pass = "root";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
PreparedStatement st=con.prepareStatement("DELETE FROM employee WHERE emp_id = ?");
st.setString(1,value1);
st.executeUpdate();
JOptionPane.showMessageDialog(p3,"Record is deleted successfully.");
con.close();
}
catch(Exception exp3)
{
JOptionPane.showMessageDialog(p3,"Error in deleting record.");
}
}
});
editbtn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){

String value=tf7.getText();
Connection con = null;
 String url = "jdbc:mysql://localhost:3306/";
 String db = "test";
 String driver = "com.mysql.jdbc.Driver";
 String user = "root";
 String pass = "root";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
PreparedStatement st=con.prepareStatement("select * from employee where emp_id=?");
st.setString(1,value);
ResultSet res=st.executeQuery();
res.next();
tf7.setText(Integer.toString(res.getInt(1)));
tf8.setText(res.getString(2));
tf9.setText(res.getString(3));
tf10.setText(Integer.toString(res.getInt(4)));
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(p2,"Can not edit data");
}
}
});
editbtn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
Connection con = null;
 String url = "jdbc:mysql://localhost:3306/";
 String db = "test";
 String driver = "com.mysql.jdbc.Driver";
 String user = "root";
 String pass = "root";
try
{
int x=JOptionPane.showConfirmDialog(p2,"Confirm edit? All data will be replaced");
if(x==0){
try{
String value1=tf7.getText();
String value2=tf8.getText();
String value3=tf9.getText();
String value4=tf10.getText();

Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);;
Statement st=con.createStatement();
st.executeUpdate("update employee set emp_name='"+value2+"', emp_address='"+value3+"',
 salary='"
+value4+"' where emp_id='"+value1+"'");
JOptionPane.showMessageDialog(p2,"Updated successfully");
con.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(p2,"Error in updating edit fields");
}
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(p2,"Error");
}
}
});
}
void dis()
{
f.getContentPane().add(tp);
tp.addTab("Add Record",p1);
tp.addTab("Edit Record",p2);
tp.addTab("Delete Record",p3);

f.setSize(350,180);
f.setVisible(true);
f.setResizable(true);
}
public static void main(String z[]){
EmployeeInformation pro=new EmployeeInformation();
pro.dis();
}
}

Output will be displayed as:

Download Source Code:

 

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 21, 2009

Ask Questions?    Discuss: Add Edit And Delete Employee Information   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Neeraj Soni
June 20, 2011
database conectivity through netbeansIDE

i want to know that how i connect with oracle database with my window-apps using netbeans ide.
micka
July 1, 2011
very helpful

thanks alot roseindia
jansi
January 14, 2012
there is a error in this code

i had error in this source code while running the program like this->Error in submitting data.how to solve this error?
shobhita
February 28, 2012
comment

its so In this section, you will learn how to add, edit and delete the Employee's information from the database using java swing. For this purpose, we have used three tabs using JTabbedPane class. Under these tabs, we have created three forms using JLabel and JTextField class which are enclosed into a panel which is then added to the JTabbedPane. In the first tab, we have prompted the user to enter the form fields to save the new employee's information into the database. The second tab will edit the Employee's information. For this, again, we have created the same form with two buttons. When the user enters the id and click the edit button, the information of the particular employee will be displayed on the respective textfields and the user can easily make changes in it. Then on clicking the save button, the information will get updated. In the third tab, we have allowed the user to delete the particular employee from the database. On entering the Employee id and click the button, the information of that employee will get removed from the database.
dunnnx
April 15, 2012
error

i just got error when im trying to insert a new data. my db db name : test table : employee attribute : emp_id,emp_name,emp_address,salary and i already change the source code to this : String url = "jdbc:mysql://localhost/test"; String db = "test"; String driver = "com.mysql.jdbc.Driver"; String user = "root"; String pass = "";
Glory
June 21, 2012
Thanks a lot....

Good Project for learning swing with MYSql connectivity
Mozum
July 12, 2012
inserting data into database through jtextxfield

I found your code highly which I used to create project shown below invaluable. But I can still not insert data into my database as it displayed error in submitting data when I attempt to run it. The code: package staffsalary; import java.awt.*; import java.awt.Color; import javax.swing.*; import java .awt.event.*; import java.sql.*; import java.text.DecimalFormat; class Staffsalary extends JFrame { DecimalFormat naira = new DecimalFormat("###,##0.00"); JTextField sureNameTxt= new JTextField(10); JLabel surel = new JLabel("Sure name"); JTextField nameTxt = new JTextField(10); JLabel naml = new JLabel("First name"); JTextField deptTxt = new JTextField(10); JLabel deptl = new JLabel("Department"); JComboBox state = new JComboBox(); JLabel statel = new JLabel("State"); JTextField fileTxt = new JTextField(5); JLabel filel = new JLabel("File number"); JTextField glTxt = new JTextField(3); JLabel gll = new JLabel("Grade level"); JTextField acctTxt = new JTextField(10); JLabel acctl = new JLabel("Account\n number"); JTextField bankTxt = new JTextField(10); JLabel bankl = new JLabel("Bank"); JTextField emlTxt = new JTextField(10); JLabel emll = new JLabel("Total emolument"); JComboBox sex = new JComboBox(); JLabel sexl = new JLabel("Marital status"); JLabel dedl = new JLabel("Deductions"); JTextField taxTxt = new JTextField(7); JLabel taxl = new JLabel("PAYE"); JTextField duesTxt = new JTextField(7); JLabel duesl = new JLabel("Union dues"); JTextField swaTxt = new JTextField(7); JLabel swal = new JLabel("SWA"); JTextField nhsTxt = new JTextField(7); JLabel nhsl = new JLabel("NHS"); JTextField penTxt = new JTextField(7); JLabel penl = new JLabel("Pension"); JTextField totTxt = new JTextField(10); JLabel totl = new JLabel("Total deduction"); JTextField netTxt = new JTextField(10); JLabel netl = new JLabel("Net salary"); JLabel bit = new JLabel("Bio Data"); JButton salbtn = new JButton("Compute salary"); JButton resetbtn = new JButton("Reset"); public static void main(String[]args) { System.out.printf("%7s%12s%15s%22s%20s%18s%18s\n","File number","Sure name", "Name","Bank", "Gross pay","Total deduction","Net pay"); Staffsalary jf = new Staffsalary(); } public Staffsalary() { setLayout(new FlowLayout()); setSize(370,500); setTitle("NPC STAFF SALARY PAY ROLL"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().setBackground(Color.PINK); JPanel bio = new JPanel(); bio.add(bit); add("North", bio); JPanel top = new JPanel(); top.setLayout(new FlowLayout()); top.add(surel); top.add(sureNameTxt); top.add(naml); top.add(nameTxt); sureNameTxt.setEditable(true); nameTxt.setEditable(true); add("West", top); JPanel side = new JPanel(); side.setLayout(new FlowLayout()); side.add(deptl); side.add(deptTxt); side.add(statel); state.addItem("Abia"); state.addItem("Adamawa"); state.addItem("Akwa Ibom"); state.addItem("Anambra"); state.addItem("Bauchi"); state.addItem("Bayelsa"); state.addItem("Benue"); state.addItem("Borno"); state.addItem("CrossRiver"); state.addItem("Delta"); state.addItem("Ebonyi"); state.addItem("Edo"); state.addItem("Ekiti"); state.addItem("Enugu"); state.addItem("Gombe"); state.addItem("Imo"); state.addItem("Jigawa"); state.addItem("Kaduna"); state.addItem("Kano"); state.addItem("Katsina"); state.addItem("Kebbi"); state.addItem("Kogi"); state.addItem("Kwara"); state.addItem("Lagos"); state.addItem("Nassarawa"); state.addItem("Niger"); state.addItem("Ogun"); state.addItem("Ondo"); state.addItem("Osun"); state.addItem("Oyo"); state.addItem("Rivers"); state.addItem("Sokoto"); state.addItem("Taraba"); state.addItem("Yobe"); state.addItem("Zamfara"); state.addItem("FCT"); side.add(state); deptTxt.setEditable(true); add("West", side); JPanel low = new JPanel(); low.setLayout(new FlowLayout()); low.add(filel); low.add(fileTxt); low.add(gll); low.add(glTxt); fileTxt.setEditable(true); glTxt.setEditable(true); add("West", low); JPanel rigth = new JPanel(); rigth.setLayout(new FlowLayout()); rigth.add(acctl); rigth.add(acctTxt); rigth.add(bankl); rigth.add(bankTxt); add("West", rigth); JPanel left = new JPanel(); left.setLayout(new FlowLayout()); left.add(emll); left.add(emlTxt); add("West", left); JPanel back = new JPanel(); back.setLayout(new FlowLayout()); sex.addItem("Married"); sex.addItem("Single"); sex.addItem("Seperated"); sex.addItem("Divorce"); sex.addItem("Widow"); back.add(sex); add("West", back); JPanel front = new JPanel(); front.setLayout(new FlowLayout()); front.add(dedl); add("Center", front); JPanel centre = new JPanel(); centre.setLayout(new FlowLayout()); centre.add(taxl); centre.add(taxTxt); centre.add(duesl); centre.add(duesTxt); add("West", centre); JPanel act = new JPanel(); act.setLayout(new FlowLayout()); act.add(swal); act.add(swaTxt); act.add(nhsl); act.add(nhsTxt); add("West", act); JPanel sat = new JPanel(); sat.setLayout(new FlowLayout()); sat.add(penl); sat.add(penTxt); add("West", sat); JPanel middle = new JPanel(); middle.setLayout(new FlowLayout()); middle.add(totl); middle.add(totTxt); totTxt.setEditable(false); add("West", middle); JPanel wide = new JPanel(); wide.setLayout(new FlowLayout()); wide.add(netl); wide.add(netTxt); netTxt.setEditable(false); add("West", wide); final JPanel min = new JPanel(); min.setLayout(new FlowLayout()); min.add(salbtn); min.add(resetbtn); add("West", min); resetbtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ sureNameTxt.setText(" "); nameTxt.setText(" "); deptTxt.setText(" "); fileTxt.setText(" "); bankTxt.setText(" "); acctTxt.setText(" "); emlTxt.setText(" "); taxTxt.setText(" "); duesTxt.setText(" "); swaTxt.setText(" "); nhsTxt.setText(" "); penTxt.setText(" "); totTxt.setText(" "); netTxt.setText(" "); glTxt.setText(" "); } }); salbtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent er) { double tamount = calculateSalary(); double ttamt = tamount; taxTxt.setText("" + naira.format(ttamt * 0.026398)); duesTxt.setText("" + naira.format(ttamt * 0.00771730)); double swat = 0; nhsTxt.setText("" + naira.format(ttamt * 0.0115767)); penTxt.setText("" + naira.format(ttamt * 0.05636)); emlTxt.setText("" +naira.format(ttamt)); double totalDeduction = (ttamt * 0.026398 )+ (ttamt * 0.00771730) + (ttamt * 0.0115767) + (ttamt * 0.05636 ) + swat; double netPay = (ttamt - totalDeduction); totTxt.setText(" " + naira.format(totalDeduction)); netTxt.setText(" " + naira.format(netPay)); String due = duesTxt.getText(); String nhs = nhsTxt.getText(); String pen = penTxt.getText(); String eml = emlTxt.getText(); String tot = totTxt.getText(); String net = netTxt.getText(); String tx = taxTxt.getText(); String sureName = sureNameTxt.getText(); String name = nameTxt.getText(); String acc = acctTxt.getText(); String bnk = bankTxt.getText(); String file = fileTxt.getText(); String dep = deptTxt.getText(); String url = "jdbc:mysql://localhost:3306/salary"; String user = "admin"; String pass = ""; System.out.printf( "\n%s" ,file + " " +sureName + " "+ name + " " + dep +" " + bnk + " " + naira.format(ttamt ) + " "+ naira.format(totalDeduction) + " " + " " +naira.format( netPay)+" "+acc) ; try{ Connection con = DriverManager.getConnection(url, user, pass); PreparedStatement st=con.prepareStatement("insert into staffs(`file_name`," + "`sure_name`,`first_name`,`department`,`payee`,`union_dues`,`nhs`,`pension`," + "`total_emolument`,`total_deduction`,`net_salary`,`bank_name`,`accountNumber`" + ") values(?,?,?,?,?,?,?,?,?,?,?,?,?)"); st.setString(1, file); st.setString(2,sureName); st.setString(3, name); st.setString(4, dep); st.setString(5, tx); st.setString(6, due); st.setString(7, nhs); st.setString(8, pen); st.setString(9, eml); st.setString(10, tot); st.setString(11, net); st.setString(12, bnk); st.setString(13, acc); st.executeUpdate(); JOptionPane.showMessageDialog(null, "Data is successfully inserted into database"); con.close(); } catch(Exception e){ JOptionPane.showMessageDialog(null, "error in submitting data"); } } }); setResizable(true); setVisible(true); } public double calculateSalary() { String gdrStr = JOptionPane.showInputDialog("Enter grade level"); String stepStr = JOptionPane.showInputDialog("Enter step"); int gdr = Integer.parseInt(gdrStr); int step = Integer.parseInt(stepStr); double amount; switch (gdr) { case 1: amount = 17073.17 + ((step - 1) * 4469); return amount; case 2: amount = 17350.50 + ((step - 1) * 486.92); return amount; case 3: amount = 17587.33 + ((step - 1) * 598.50); return amount; case 4: amount = 18422.67 + ((step- 1) * 719.08); return amount; case 5: amount = 20874.33 + ((step - 1) * 835.33); return amount; case 6: amount = 25452.42 + ((step - 1) * 950.92); return amount; case 7: amount = 42263.75 + ((step - 1) * 1562.33); return amount; case 8: amount = 54615.33 + ((step - 1) * 1860); return amount; case 9: amount = 64154.83 + ((step - 1) * 2214.50); return amount; case 10: amount = 75309.25 + ((step - 1) * 2435.75); return amount; case 12: amount = 86867.33 + ((step - 1) * 3777.42); return amount; case 13: amount = 96952.75 + ((step - 1) * 3993.50); return amount; case 14: amount = 107084.83 + ((step - 1) * 4299.25); return amount; case 15: amount = 147318 + ((step - 1) ) * 6088.83; return amount; case 16: amount = 182239.75 + ((step - 1) * 7292.67); return amount; case 17: amount = 347733.33 + ((step - 1) * 1313.92); return amount; default : amount = 0; return amount; } } }
sunny
July 15, 2012
java

it was very helpfull
Mohamed Hafees
July 16, 2012
About Sampol Inventry System

HI All I am Hafees from Srilanka. I am a software engineering student. Now i want to create a INVENTORY SYSTEM for my end of my course but i try to create system but result error, error. So please if you can do any help for me please inform through my mail id . Mohamed Hafees
krishan mishra
October 23, 2012
Thanks

Hi, Iam krishan mishra.iwould like to you that, this code is very usefull for those person who is bignner in java . kindly provide some more java code and programm with concept. Thanks & Regards Krishan Mishra
HIREN MODI
November 7, 2012
Instruction about the above program

It shows the error * Error in Data Write/Edit/Delete but you set String pass =""; // then it works perfectly
Rumiana
October 6, 2013
ERROR

Hi! I have a error when I insert data intro table. I create a database employee with following fields - emp_id, emp_name, emp_address, salary in test, but still failing. What is wrong?
tarun
December 3, 2011
database

how can i replace mysql with sqlite database...plz guid me..i want to make it standalone application.
DMCA.com