Guest User

Untitled

a guest
May 10th, 2017
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. package com.question3;
  2.  
  3. import java.rmi.Remote;
  4. import java.rmi.RemoteException;
  5. import java.util.List;
  6.  
  7. public interface Company extends Remote {
  8.  
  9. public List<Employee> getEmployees() throws RemoteException;
  10.  
  11. public void addEmployee(Employee e) throws RemoteException;
  12. }
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31. package com.question3;
  32.  
  33. import java.io.Serializable;
  34. import java.rmi.RemoteException;
  35.  
  36. public class Employee implements Serializable{
  37. int eId;
  38. String name;
  39. float sal;
  40.  
  41. public Employee() {
  42. // TODO Auto-generated constructor stub
  43. }
  44. public Employee(int eId, String name, float sal) {
  45. super();
  46. this.eId = eId;
  47. this.name = name;
  48. this.sal = sal;
  49. }
  50.  
  51. public int geteId() {
  52. return eId;
  53. }
  54. public void seteId(int eId) {
  55. this.eId = eId;
  56. }
  57. public String getName() {
  58. return name;
  59. }
  60. public void setName(String name) {
  61. this.name = name;
  62. }
  63. public float getSal() {
  64. return sal;
  65. }
  66. public void setSal(float sal) {
  67. this.sal = sal;
  68. }
  69. @Override
  70. public String toString() {
  71. return "Employee [eId=" + eId + ", name=" + name + ", sal=" + sal + "]";
  72. }
  73.  
  74.  
  75.  
  76.  
  77.  
  78. }
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88. package com.question3;
  89.  
  90. import java.rmi.RemoteException;
  91. import java.rmi.server.UnicastRemoteObject;
  92. import java.sql.Connection;
  93. import java.sql.PreparedStatement;
  94. import java.sql.ResultSet;
  95. import java.sql.Statement;
  96. import java.util.ArrayList;
  97. import java.util.List;
  98.  
  99. public class EmployeeImpl extends UnicastRemoteObject implements Company{
  100.  
  101. public EmployeeImpl() throws RemoteException {
  102.  
  103. }
  104. @Override
  105. public List<Employee> getEmployees() throws RemoteException {
  106. Connection connection = null;
  107. PreparedStatement statement = null;
  108. ResultSet resultSet = null;
  109. List<Employee> employee = new ArrayList<>();
  110. try {
  111. connection = Utility.getMyCon();
  112. statement = connection.prepareStatement("select * from employeermi");
  113. resultSet= statement.executeQuery();
  114.  
  115. while(resultSet.next()){
  116. Employee employeeobj = new Employee(resultSet.getInt(1),resultSet.getString(2), resultSet.getFloat(3));
  117. employee.add(employeeobj);
  118. }
  119. } catch (Exception e) {
  120. System.out.println(e);
  121. }
  122.  
  123. return employee;
  124.  
  125.  
  126. }
  127.  
  128. @Override
  129. public void addEmployee(Employee e) throws RemoteException {
  130. Connection connection = null;
  131. PreparedStatement statement = null;
  132. String query = "insert into employeermi values(?,?,?)";
  133.  
  134. try {
  135. connection = Utility.getMyCon();
  136. statement = connection.prepareStatement(query);
  137. statement.setInt(1, e.eId);
  138. statement.setString(2, e.name);
  139. statement.setFloat(3, e.sal);
  140. int c = statement.executeUpdate();
  141. System.out.println(c+" row inserted !");
  142. } catch (Exception e2) {
  143. System.out.println(e2);
  144. }
  145.  
  146. }
  147.  
  148. }
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157. package com.question3;
  158.  
  159. import java.rmi.Naming;
  160. import java.util.List;
  161. import java.util.Scanner;
  162.  
  163. public class MyClient {
  164.  
  165. public static void main(String[] args) throws Exception{
  166. Scanner out = new Scanner(System.in);
  167. Company c = (Company)Naming.lookup("rmi://localhost:2222/employee");
  168. boolean condition = true;
  169.  
  170. while(condition){
  171. System.out.println("1.getEmployee()\n2.addEmployee()");
  172. int check = out.nextInt();
  173.  
  174. switch (check) {
  175. case 1:
  176. List<Employee> e = c.getEmployees();
  177. for (Employee employee : e) {
  178. System.out.println(employee);
  179. }
  180. break;
  181. case 2:
  182. System.out.println("Enter Employee ID :");
  183. int eId = out.nextInt();
  184. System.out.println("Enter Employee Name :");
  185. String name = out.next();
  186. System.out.println("Enter Employee Salary :");
  187. float sal = out.nextFloat();
  188. c.addEmployee(new Employee(eId, name, sal));
  189. System.err.println("Employee Inserted!");
  190. break;
  191. default:
  192. System.err.println("Invalid Selection");
  193. break;
  194. }
  195. System.out.println("Do you want to continue(yes/no)");
  196. String s = out.next();
  197. if(s.equals("no")){
  198. System.exit(0);
  199. }
  200. }
  201. }
  202.  
  203. }
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212. package com.question3;
  213.  
  214. import java.rmi.registry.LocateRegistry;
  215. import java.rmi.registry.Registry;
  216.  
  217. public class MyServer {
  218.  
  219. public static void main(String[] args) throws Exception{
  220. Company c = new EmployeeImpl();
  221. Registry reg = LocateRegistry.createRegistry(2222);
  222. reg.rebind("employee", c);
  223. System.out.println("Service is running on port 2222");
  224.  
  225. }
  226.  
  227. }
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238. package com.question3;
  239.  
  240. import java.sql.Connection;
  241. import java.sql.DriverManager;
  242. import java.sql.ResultSet;
  243. import java.sql.SQLException;
  244. import java.sql.Statement;
  245.  
  246. public class Utility {
  247. static Connection con = null;
  248.  
  249. public static Connection getMyCon(){
  250. try {
  251. Class.forName("oracle.jdbc.driver.OracleDriver");
  252. con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr");
  253. } catch (Exception e) {
  254. // TODO: handle exception
  255. }
  256. return con;
  257. }
  258. }
Add Comment
Please, Sign In to add comment