Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.question3;
- import java.rmi.Remote;
- import java.rmi.RemoteException;
- import java.util.List;
- public interface Company extends Remote {
- public List<Employee> getEmployees() throws RemoteException;
- public void addEmployee(Employee e) throws RemoteException;
- }
- package com.question3;
- import java.io.Serializable;
- import java.rmi.RemoteException;
- public class Employee implements Serializable{
- int eId;
- String name;
- float sal;
- public Employee() {
- // TODO Auto-generated constructor stub
- }
- public Employee(int eId, String name, float sal) {
- super();
- this.eId = eId;
- this.name = name;
- this.sal = sal;
- }
- public int geteId() {
- return eId;
- }
- public void seteId(int eId) {
- this.eId = eId;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public float getSal() {
- return sal;
- }
- public void setSal(float sal) {
- this.sal = sal;
- }
- @Override
- public String toString() {
- return "Employee [eId=" + eId + ", name=" + name + ", sal=" + sal + "]";
- }
- }
- package com.question3;
- import java.rmi.RemoteException;
- import java.rmi.server.UnicastRemoteObject;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- public class EmployeeImpl extends UnicastRemoteObject implements Company{
- public EmployeeImpl() throws RemoteException {
- }
- @Override
- public List<Employee> getEmployees() throws RemoteException {
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- List<Employee> employee = new ArrayList<>();
- try {
- connection = Utility.getMyCon();
- statement = connection.prepareStatement("select * from employeermi");
- resultSet= statement.executeQuery();
- while(resultSet.next()){
- Employee employeeobj = new Employee(resultSet.getInt(1),resultSet.getString(2), resultSet.getFloat(3));
- employee.add(employeeobj);
- }
- } catch (Exception e) {
- System.out.println(e);
- }
- return employee;
- }
- @Override
- public void addEmployee(Employee e) throws RemoteException {
- Connection connection = null;
- PreparedStatement statement = null;
- String query = "insert into employeermi values(?,?,?)";
- try {
- connection = Utility.getMyCon();
- statement = connection.prepareStatement(query);
- statement.setInt(1, e.eId);
- statement.setString(2, e.name);
- statement.setFloat(3, e.sal);
- int c = statement.executeUpdate();
- System.out.println(c+" row inserted !");
- } catch (Exception e2) {
- System.out.println(e2);
- }
- }
- }
- package com.question3;
- import java.rmi.Naming;
- import java.util.List;
- import java.util.Scanner;
- public class MyClient {
- public static void main(String[] args) throws Exception{
- Scanner out = new Scanner(System.in);
- Company c = (Company)Naming.lookup("rmi://localhost:2222/employee");
- boolean condition = true;
- while(condition){
- System.out.println("1.getEmployee()\n2.addEmployee()");
- int check = out.nextInt();
- switch (check) {
- case 1:
- List<Employee> e = c.getEmployees();
- for (Employee employee : e) {
- System.out.println(employee);
- }
- break;
- case 2:
- System.out.println("Enter Employee ID :");
- int eId = out.nextInt();
- System.out.println("Enter Employee Name :");
- String name = out.next();
- System.out.println("Enter Employee Salary :");
- float sal = out.nextFloat();
- c.addEmployee(new Employee(eId, name, sal));
- System.err.println("Employee Inserted!");
- break;
- default:
- System.err.println("Invalid Selection");
- break;
- }
- System.out.println("Do you want to continue(yes/no)");
- String s = out.next();
- if(s.equals("no")){
- System.exit(0);
- }
- }
- }
- }
- package com.question3;
- import java.rmi.registry.LocateRegistry;
- import java.rmi.registry.Registry;
- public class MyServer {
- public static void main(String[] args) throws Exception{
- Company c = new EmployeeImpl();
- Registry reg = LocateRegistry.createRegistry(2222);
- reg.rebind("employee", c);
- System.out.println("Service is running on port 2222");
- }
- }
- package com.question3;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class Utility {
- static Connection con = null;
- public static Connection getMyCon(){
- try {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr");
- } catch (Exception e) {
- // TODO: handle exception
- }
- return con;
- }
- }
Add Comment
Please, Sign In to add comment