Java Program to implement employee information system using JAVA and JDBC ODBC
Employee Information System
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.awt.*;
public class EmpInformation extends JFrame implements ActionListener
{
JLabel lbl_title,lbl_id,lbl_name,lbl_quali,lbl_design,lbl_salary;
JButton btn_save,btn_update,btn_delete,btn_search,btn_clear,btn_exit;
JTextField txt_id,txt_name,txt_quali,txt_design,txt_salary;
JPanel p1,p2,p3;
Connection con;
public EmpInformation()
{
lbl_title=new JLabel("EMPLOYEE INFORMATION SYSTEM");
lbl_id=new JLabel("ID");
lbl_name=new JLabel("Name");
lbl_quali=new JLabel("Qualification");
lbl_design=new JLabel("Designation");
lbl_salary=new JLabel("Salary");
btn_save=new JButton("Save");
btn_update=new JButton("Update");
btn_delete=new JButton("Delete");
btn_search=new JButton("Search");
btn_clear=new JButton("Clear");
btn_exit=new JButton("Exit");
txt_id=new JTextField(20);
txt_name=new JTextField(10);
txt_quali=new JTextField(10);
txt_design=new JTextField(10);
txt_salary=new JTextField(10);
btn_save.addActionListener(this);
btn_update.addActionListener(this);
btn_delete.addActionListener(this);
btn_search.addActionListener(this);
btn_clear.addActionListener(this);
btn_exit.addActionListener(this);
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p1.setLayout(new FlowLayout());
p1.add(lbl_title);
p2.setLayout(new FlowLayout());
p2.add(btn_save);
p2.add(btn_update);
p2.add(btn_delete);
p2.add(btn_search);
p2.add(btn_clear);
p2.add(btn_exit);
p3.setLayout(new GridLayout(5,2));
p3.add(lbl_id);
p3.add(txt_id);
p3.add(lbl_name);
p3.add(txt_name);
p3.add(lbl_quali);
p3.add(txt_quali);
p3.add(lbl_design);
p3.add(txt_design);
p3.add(lbl_salary);
p3.add(txt_salary);
Container c=getContentPane();
c.setLayout(null);
p1.setBounds(30,20,400,30);
p3.setBounds(10,90,200,200);
p2.setBounds(10,300,300,90);
c.add(p1);
c.add(p2);
c.add(p3);
}
public void actionPerformed(ActionEvent ae)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:emp");
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
String st=ae.getActionCommand();
if(st.equals("Clear"))
{
txt_id.setText("");
txt_name.setText("");
txt_quali.setText("");
txt_design.setText("");
txt_salary.setText("");
}
if(st.equals("Exit"))
{
System.exit(0);
}
if(st.equals("Search"))
{
String se=txt_id.getText();
try{
Statement st1=con.createStatement();
String query="Select *from employee where id='"+ se +"'";
ResultSet rs=st1.executeQuery(query);
while(rs.next())
{
txt_id.setText(rs.getString("id"));
txt_name.setText(rs.getString("name"));
txt_quali.setText(rs.getString("qualification"));
txt_design.setText(rs.getString("designation"));
txt_salary.setText(rs.getString("salary"));
}
}
catch(Exception ex)
{
}
}
if(st.equals("Delete"))
{
try{
Statement st2=con.createStatement();
String id1=txt_id.getText();
String query2="Delete * from employee where id='"+id1+"'";
st2.executeUpdate(query2);
}
catch(Exception ex)
{
}
JOptionPane.showMessageDialog(null,"Successfully deleted");
txt_id.setText("");
txt_name.setText("");
txt_quali.setText("");
txt_design.setText("");
txt_salary.setText("");
}
if(st.equals("Update"))
{
String s1=txt_id.getText();
String s2=txt_name.getText();
String s3=txt_quali.getText();
String s4=txt_design.getText();
String s5=txt_salary.getText();
try{
PreparedStatement ps=con.prepareStatement("Update employee set id=?,name=?,qualification=?,designation=?,salary=? where id=?");
ps.setString(1,s1);
ps.setString(2,s2);
ps.setString(3,s3);
ps.setString(4,s4);
ps.setString(5,s5);
ps.setString(6,s1);
ps.executeUpdate();
txt_id.setText("");
txt_name.setText("");
txt_quali.setText("");
txt_design.setText("");
txt_salary.setText("");
JOptionPane.showMessageDialog(null,"Successfully updated");
}
catch(Exception ex)
{
}
}
if(st.equals("Save"))
{
String s1=txt_id.getText();
String s2=txt_name.getText();
String s3=txt_quali.getText();
String s4=txt_design.getText();
String s5=txt_salary.getText();
try{
PreparedStatement ps=con.prepareStatement("insert into employee values(?,?,?,?,?)");
ps.setString(1,s1);
ps.setString(2,s2);
ps.setString(3,s3);
ps.setString(4,s4);
ps.setString(5,s5);
ps.executeUpdate();
txt_id.setText("");
txt_name.setText("");
txt_quali.setText("");
txt_design.setText("");
txt_salary.setText("");
JOptionPane.showMessageDialog(null,"Successfully inserted");
}
catch(Exception ex)
{
}
}
}
public static void main(String args[])
{
EmpInformation emp=new EmpInformation();
emp.setVisible(true);
emp.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
emp.setSize(new Dimension(600,500));
}
}
Output
Employee Information System using JAVA and JDBC ODBC |
No comments:
Post a Comment