Guest User

Untitled

a guest
May 26th, 2017
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.*;
  3.  
  4. public class EmployeeManagement
  5. {
  6.  
  7. public static void main(String[] args)
  8. {
  9. String eid;
  10. String ename;
  11. String edept;
  12. double esal;
  13. boolean flag=true;
  14. Scanner sc = new Scanner(System.in);
  15. String choice;
  16.  
  17. Connection con=null;
  18. PreparedStatement ps=null;
  19. ResultSet rs=null;
  20.  
  21. try
  22. {
  23.  
  24. Class.forName("oracle.jdbc.driver.OracleDriver");
  25. con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr");
  26. }
  27. catch(Exception e)
  28. {
  29. System.out.println(e);
  30. }
  31.  
  32. while(flag)
  33. {
  34. 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");
  35. choice = sc.next();
  36.  
  37. if(choice.equals("1"))
  38. {
  39. try
  40. {
  41. System.out.println("Enter id");
  42. eid = sc.next();
  43. System.out.println("Enter Name");
  44. ename = sc.next();
  45. System.out.println("Enter Department");
  46. edept = sc.next();
  47. System.out.println("Enter salary");
  48. esal = sc.nextDouble();
  49. ps=con.prepareStatement("insert into emp_table values(?,?,?,?)");
  50. ps.setString(1, eid);
  51. ps.setString(2, ename);
  52. ps.setString(3, edept);
  53. ps.setDouble(4, esal);
  54. ps.execute();
  55. System.out.println("Employee added \n");
  56. }
  57. catch(Exception e)
  58. {
  59. System.out.println("Please enter correct values");
  60. }
  61. }
  62. else if(choice.equals("2"))
  63. {
  64. try
  65. {
  66.  
  67. ps=con.prepareStatement("select * from emp_table");
  68. rs=ps.executeQuery();
  69.  
  70. while(rs.next())
  71. {
  72. System.out.println("ID = "+rs.getString(1)+" Name = "+rs.getString(2)+" Department = "+rs.getString(3)+" Salary = "+rs.getDouble(4));
  73. }
  74.  
  75. }
  76. catch(Exception e)
  77. {
  78. System.out.println("Please enter correct values");
  79. }
  80. }
  81.  
  82.  
  83. else if(choice.equals("3"))
  84. {
  85. try
  86. {
  87. System.out.println("Enter id which you want to search");
  88. eid = sc.next();
  89. ps=con.prepareStatement("select * from emp_table where eid=?");
  90. ps.setString(1, eid);
  91. rs=ps.executeQuery();
  92. while(rs.next())
  93. {
  94. System.out.println("ID = "+rs.getString(1)+" Name = "+rs.getString(2)+" Department = "+rs.getString(3)+" Salary = "+rs.getDouble(4));
  95. }
  96.  
  97. }
  98. catch(Exception e)
  99. {
  100. System.out.println("Please enter correct values");
  101. }
  102. }
  103.  
  104.  
  105.  
  106. else if(choice.equals("4"))
  107. {
  108. try
  109. {
  110. System.out.println("Enter id which you want to delete");
  111. eid = sc.next();
  112. ps=con.prepareStatement("delete from emp_table where eid=?");
  113. ps.setString(1, eid);
  114. ps.execute();
  115. System.out.println("Employee deleted \n");
  116.  
  117. }
  118. catch(Exception e)
  119. {
  120. System.out.println("Please enter correct values");
  121. }
  122. }
  123.  
  124.  
  125. else if(choice.equals("5"))
  126. {
  127. try
  128. {
  129. System.out.println("Enter id which you want to update");
  130. String eid1 = sc.next();
  131. System.out.println("Enter new Name");
  132. ename = sc.next();
  133. System.out.println("Enter new Department");
  134. edept = sc.next();
  135. System.out.println("Enter new salary");
  136. esal = sc.nextDouble();
  137. ps=con.prepareStatement("update emp_table set ename=?,edept=?,esal=? where eid=?");
  138. ps.setString(1, ename);
  139. ps.setString(2, edept);
  140. ps.setDouble(3, esal);
  141. ps.setString(4, eid1);
  142. ps.execute();
  143. System.out.println("Employee update \n");
  144. }
  145. catch(Exception e)
  146. {
  147. System.out.println("Please enter correct values");
  148. }
  149. }
  150.  
  151. else
  152. {
  153. System.out.println("Wrong choice please Enter again \n");
  154. continue;
  155. }
  156.  
  157. System.out.println("Enter 1 to continue else enter any other digit");
  158. String ch = sc.next();
  159.  
  160. if(ch.equals("1"))
  161. flag=true;
  162. else
  163. {
  164. flag=false;
  165. System.out.println("Thank you");
  166. }
  167.  
  168. }
  169. }
  170.  
  171. }
Add Comment
Please, Sign In to add comment