Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.*;
- public class EmployeeManagement
- {
- public static void main(String[] args)
- {
- String eid;
- String ename;
- String edept;
- double esal;
- boolean flag=true;
- Scanner sc = new Scanner(System.in);
- String choice;
- Connection con=null;
- PreparedStatement ps=null;
- ResultSet rs=null;
- try
- {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr");
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- while(flag)
- {
- System.out.println("Enter 1 for add Employee \nEnter 2 for get All Employee \nEnter 3 for get Employee By employee ID \nEnter 4 for delete Employee\nEnter 5 for update Employee");
- choice = sc.next();
- if(choice.equals("1"))
- {
- try
- {
- System.out.println("Enter id");
- eid = sc.next();
- System.out.println("Enter Name");
- ename = sc.next();
- System.out.println("Enter Department");
- edept = sc.next();
- System.out.println("Enter salary");
- esal = sc.nextDouble();
- ps=con.prepareStatement("insert into emp_table values(?,?,?,?)");
- ps.setString(1, eid);
- ps.setString(2, ename);
- ps.setString(3, edept);
- ps.setDouble(4, esal);
- ps.execute();
- System.out.println("Employee added \n");
- }
- catch(Exception e)
- {
- System.out.println("Please enter correct values");
- }
- }
- else if(choice.equals("2"))
- {
- try
- {
- ps=con.prepareStatement("select * from emp_table");
- rs=ps.executeQuery();
- while(rs.next())
- {
- System.out.println("ID = "+rs.getString(1)+" Name = "+rs.getString(2)+" Department = "+rs.getString(3)+" Salary = "+rs.getDouble(4));
- }
- }
- catch(Exception e)
- {
- System.out.println("Please enter correct values");
- }
- }
- else if(choice.equals("3"))
- {
- try
- {
- System.out.println("Enter id which you want to search");
- eid = sc.next();
- ps=con.prepareStatement("select * from emp_table where eid=?");
- ps.setString(1, eid);
- rs=ps.executeQuery();
- while(rs.next())
- {
- System.out.println("ID = "+rs.getString(1)+" Name = "+rs.getString(2)+" Department = "+rs.getString(3)+" Salary = "+rs.getDouble(4));
- }
- }
- catch(Exception e)
- {
- System.out.println("Please enter correct values");
- }
- }
- else if(choice.equals("4"))
- {
- try
- {
- System.out.println("Enter id which you want to delete");
- eid = sc.next();
- ps=con.prepareStatement("delete from emp_table where eid=?");
- ps.setString(1, eid);
- ps.execute();
- System.out.println("Employee deleted \n");
- }
- catch(Exception e)
- {
- System.out.println("Please enter correct values");
- }
- }
- else if(choice.equals("5"))
- {
- try
- {
- System.out.println("Enter id which you want to update");
- String eid1 = sc.next();
- System.out.println("Enter new Name");
- ename = sc.next();
- System.out.println("Enter new Department");
- edept = sc.next();
- System.out.println("Enter new salary");
- esal = sc.nextDouble();
- ps=con.prepareStatement("update emp_table set ename=?,edept=?,esal=? where eid=?");
- ps.setString(1, ename);
- ps.setString(2, edept);
- ps.setDouble(3, esal);
- ps.setString(4, eid1);
- ps.execute();
- System.out.println("Employee update \n");
- }
- catch(Exception e)
- {
- System.out.println("Please enter correct values");
- }
- }
- else
- {
- System.out.println("Wrong choice please Enter again \n");
- continue;
- }
- System.out.println("Enter 1 to continue else enter any other digit");
- String ch = sc.next();
- if(ch.equals("1"))
- flag=true;
- else
- {
- flag=false;
- System.out.println("Thank you");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment