Guest User

2,3

a guest
May 10th, 2017
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. ques 2----------------------------------
  2. package newP;
  3. public class Employee implements java.io.Serializable
  4. {
  5. private int employeeId;
  6. private String name;
  7. float salary;
  8. public Employee(int employeeId,String name,float salary)
  9. {
  10. this.employeeId=employeeId;
  11. this.name=name;
  12. this.salary=salary;
  13. }
  14. public String toString()
  15. {
  16. return employeeId+" "+name+" "+salary;
  17. }
  18.  
  19. }
  20. --------------------------------------
  21. package newP;
  22.  
  23. import java.rmi.*;
  24. import java.util.*;
  25. interface Employee1 extends Remote
  26. {
  27. public List<Employee> getEmployee() throws Exception ;
  28. public void setEmployee(Employee e) throws Exception ;
  29. }
  30. ------------------------------------------
  31. package newP;
  32.  
  33. import java.rmi.registry.*;
  34. import java.rmi.*;
  35. import java.rmi.server.*;
  36. import java.util.*;
  37.  
  38.  
  39. class EmployeeClient
  40. {
  41. public static void main(String args[]) throws Exception
  42. {
  43. int ch;
  44. // ArrayList<Employee> list=new ArrayList<>();
  45. Employee1 m = (Employee1)Naming.lookup("rmi://localhost:3333/service");
  46. Scanner sc = new Scanner(System.in);
  47. EmployeeImpl e =new EmployeeImpl();
  48. while(true)
  49. {
  50. System.out.println("1.add \n 2.show");
  51. ch=sc.nextInt();
  52. if(ch==1)
  53. {
  54. System.out.println("Enter id , name ,salary");
  55. int id=sc.nextInt();
  56. String name=sc.next();
  57. float salary=sc.nextFloat();
  58. Employee e1=new Employee(id,name,salary);
  59. e.setEmployee(e1);
  60. }
  61.  
  62. if(ch==2)
  63. {
  64. System.out.println(e.getEmployee());
  65. }
  66. System.out.println("enter 1 to continue");
  67. int ch1=sc.nextInt();
  68. if(ch1!=1)
  69. System.exit(0);
  70. }
  71. }
  72. }
  73. --------------------------------------
  74. package newP;
  75. import java.util.*;
  76. import java.rmi.*;
  77. import java.rmi.server.*;
  78. import java.util.*;
  79. public class EmployeeImpl extends UnicastRemoteObject implements Employee1
  80. {
  81. static ArrayList<Employee> list=new ArrayList<>();
  82. static
  83. {
  84. Employee e1 = new Employee(101,"jack",12000);
  85. Employee e2 = new Employee(102,"ram",15000);
  86. list.add(e1);
  87. list.add(e2);
  88. }
  89. protected EmployeeImpl() throws RemoteException {
  90. super();
  91. // TODO Auto-generated constructor stub
  92. }
  93.  
  94. public void setEmployee(Employee e)
  95. {
  96. list.add(e);
  97. }
  98. public List<Employee> getEmployee()
  99. {
  100. return list;
  101. }
  102.  
  103. }
  104. -----------------------------------------
  105. package newP;
  106.  
  107. import java.rmi.registry.*;
  108. import java.rmi.*;
  109. import java.rmi.server.*;
  110. import java.util.*;
  111. public class EmployeeServer
  112. {
  113. public static void main(String args[])throws Exception
  114. {
  115. Employee1 m=new EmployeeImpl();
  116. Registry reg = LocateRegistry.createRegistry(3333);
  117. reg.rebind("service",m);
  118. System.out.println("server is ready");
  119. }
  120. }
  121.  
  122.  
  123. ques3-------------------------------------
  124.  
  125. package Question3;
  126.  
  127. public class Employee implements java.io.Serializable
  128. {
  129. private int employeeId;
  130. private String name;
  131. float salary;
  132. public Employee(int employeeId,String name,float salary)
  133. {
  134. this.employeeId=employeeId;
  135. this.name=name;
  136. this.salary=salary;
  137. }
  138. public String toString()
  139. {
  140. return employeeId+" "+name+" "+salary;
  141. }
  142.  
  143. }
  144. ---------------------------------------------
  145. package Question3;
  146.  
  147. import java.rmi.*;
  148. import java.util.*;
  149. interface Employee1 extends Remote
  150. {
  151. public List<Employee> getEmployee() throws Exception ;
  152. public void setEmployee(int id , String name,float salary) throws Exception ;
  153. }
  154. ------------------------------------------------
  155. package Question3;
  156.  
  157.  
  158. import java.rmi.registry.*;
  159. import java.rmi.*;
  160. import java.rmi.server.*;
  161. import java.util.*;
  162.  
  163.  
  164. class EmployeeClient
  165. {
  166. public static void main(String args[]) throws Exception
  167. {
  168. int ch;
  169. // ArrayList<Employee> list=new ArrayList<>();
  170. Employee1 m = (Employee1)Naming.lookup("rmi://localhost:2222/service");
  171. Scanner sc = new Scanner(System.in);
  172. EmployeeImpl e =new EmployeeImpl();
  173. while(true)
  174. {
  175. System.out.println("1.add \n 2.show");
  176. ch=sc.nextInt();
  177. if(ch==1)
  178. {
  179. System.out.println("Enter id , name ,salary");
  180. int id=sc.nextInt();
  181. String name=sc.next();
  182. float salary=sc.nextFloat();
  183. e.setEmployee(id,name,salary);
  184. }
  185.  
  186. if(ch==2)
  187. {
  188. System.out.println(e.getEmployee());
  189. }
  190. System.out.println("enter 1 to continue");
  191. int ch1=sc.nextInt();
  192. if(ch1!=1)
  193. System.exit(0);
  194. }
  195. }
  196. }
  197. ---------------------------------------------------
  198. package Question3;
  199.  
  200.  
  201. import java.util.*;
  202. import java.rmi.*;
  203. import java.rmi.server.*;
  204. import java.sql.Connection;
  205. import java.sql.DriverManager;
  206. import java.sql.ResultSet;
  207. import java.sql.Statement;
  208. import java.util.*;
  209. public class EmployeeImpl extends UnicastRemoteObject implements Employee1
  210. {
  211. static ArrayList<Employee> list=new ArrayList<>();
  212. static Connection con=null;
  213. static Statement st=null;
  214. static ResultSet rs=null;
  215. static
  216. {
  217. Employee e1 = new Employee(101,"jack",12000);
  218. Employee e2 = new Employee(102,"ram",15000);
  219. list.add(e1);
  220. list.add(e2);
  221. }
  222. protected EmployeeImpl() throws RemoteException {
  223. super();
  224. // TODO Auto-generated constructor stub
  225. }
  226.  
  227. public void setEmployee(int id,String name,float salary)
  228. {
  229. try
  230. {
  231. Class.forName("oracle.jdbc.driver.OracleDriver");
  232. con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","hr","hr");
  233. st=con.createStatement();
  234. st.execute("insert into employee values("+id+",'"+name+"',"+salary+")");
  235. System.out.println("inserted");
  236. }catch(Exception e){System.out.println(e);}
  237. }
  238. public List<Employee> getEmployee()
  239. {
  240. try
  241. {
  242. Class.forName("oracle.jdbc.driver.OracleDriver");
  243. con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","hr","hr");
  244. st=con.createStatement();
  245. rs=st.executeQuery("select * from employee");
  246. while(rs.next())
  247. {
  248. Employee e2 =new Employee(rs.getInt(1),rs.getString(2),rs.getFloat(3));
  249. list.add(e2);
  250. }
  251. }catch(Exception e){ System.out.println(e);}
  252. return list;
  253. }
  254.  
  255. }
  256. -----------------------------------------------------------
  257. package Question3;
  258.  
  259. import java.rmi.registry.*;
  260. import java.rmi.*;
  261. import java.rmi.server.*;
  262. import java.util.*;
  263. public class EmployeeServer
  264. {
  265. public static void main(String args[])throws Exception
  266. {
  267. Employee1 m=new EmployeeImpl();
  268. Registry reg = LocateRegistry.createRegistry(2222);
  269. reg.rebind("service",m);
  270. System.out.println("server is ready");
  271. }
  272. }
Add Comment
Please, Sign In to add comment