Showing posts with label RMI. Show all posts
Showing posts with label RMI. Show all posts

Monday, 24 September 2018

REMOTE METHOD INVOCATION (RMI) | JAVA

Create an RMI based Examination server of Punjab University that will display the status of a student (Pass/ Fail) upon accept the roll number from client. The server will take "rollNo" of type string as an input from client and can will return his/her status. Suppose you have got find (String) method on server side for the searching of student's status. The client should display the status on console. 


RMI Interface:

import java.rmi.*;
public interface ExamInt extends Remote
{
  String find(String roll_n) throws RemoteException ;
}

Interface Implementation:

import java.rmi.server.*;
import java.rmi.*;
import java.sql.*;
public class ExamServerImpl extends UnicastRemoteObject implements ExamInt
{
public ExamServerImpl() throws RemoteException
{
                             }
//implementation of find(String) not required in asked question...
public String find(String roll_no) throws RemoteException
{
String status;
status=Examjdbc(roll_no);
return status;
}
public String Examjdbc(String r_no) throws RemoteException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn=DriverManager.getConnection("Jdbc:Odbc:mobileDSN");
Statement st=cn.createStatement();
ResultSet rs=st.executeQuery("Select *from status");
while(rs.next())
{
if(rs.getString(1).equals(r_no))
return rs.getString(2);
}

}
catch(Exception e)
{
System.out.println(e);
}
return null;
}
}

Server:

import java.net.*;
import java.rmi.*;
public class ExamServer
{
public static void main(String args[])
{
try{
ExamServerImpl obj= new ExamServerImpl();
Naming.rebind("ExamServer",obj);
}

catch(Exception e)
{
System.out.println("Exception "+e);
}
}
}

Client:

import javax.swing.*;
import java.rmi.*;
import java.net.*;
public class ExamClient
{
public static void main(String args[])
{
try{
    String examServerURL = "rmi://" + args[0] + "/ExamServer";
ExamInt obj= (ExamInt)Naming.lookup(examServerURL);
String r_n=JOptionPane.showInputDialog("Enter Roll number:");
String Status=obj.find(r_n);
System.out.println("You are " + Status);
}
catch(Exception e)
{
System.out.print("Exception " + e);
}
}

Output:


Remote Method Invocation (RMI) | Java

You have to build a mobile outlet server based on RMI. Client will generate a call to getPrice(string mobilename) to server class and on server you will get the name of mobile. Create a connection to Database and return the price of that particular mobile to client.
Perform all steps required for complete RMI application.
DB Name: mobile.mdb                              DSN name: mobile


Interface:

import java.rmi.*;
public interface MobileInterface extends Remote
{
double getPrice(String m_name) throws RemoteException;
}

Implementation:

import java.rmi.server.*;
import java.rmi.*;
import java.sql.*;
public class MobileServerImpl extends UnicastRemoteObject implements MobileInterface {
    public MobileServerImpl() throws RemoteException {
    }
    public double getPrice(String m_name) throws RemoteException {
        double price;
        price = Mobilejdbc(m_name);
        return price;
    }
    public double Mobilejdbc(String m_nam) throws RemoteException {
        double p;
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection cn = DriverManager.getConnection("Jdbc:Odbc:mobileDSN");
            String sql = "Select *from Price where name=?";
            PreparedStatement pSt = cn.prepareStatement(sql);
            pSt.setString(1, m_nam);
            ResultSet rs = pSt.executeQuery();         
            if (rs.next()) {             
                    p = Double.parseDouble(rs.getString(2));
                    return p;
                }       
        } catch (Exception e) {
            System.out.println(e);
        }
        return 0.0;
    }
}

Server:

import java.net.*;
import java.rmi.*;
import java.sql.*;
public class MobileServer 
{
public static void main(String args[])
{
try{
MobileServerImpl obj=new MobileServerImpl();
Naming.rebind("MobileServer",obj);
}catch(Exception e)
{
System.out.println(e);
}


}

Client:

import javax.swing.*;
import java.rmi.*;
public class MobileClient 
{
public static void main(String args[])
{
try{
MobileInterface intobj=(MobileInterface)Naming.lookup("rmi://" + args[0] + "/MobileServer");
String  name=JOptionPane.showInputDialog("Enter Mobile Name:");
double p=intobj.getPrice(name);
System.out.println("Price for mobile: "+name+" is: "+p);
}catch(Exception e)
{
System.out.println(e);}
}


}

Output:



Methods in JAVA Language

 In general, a way may be thanks to perform some task. Similarly, the tactic in Java may be a collection of instructions that performs a sel...