Monday 24 September 2018

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:



No comments:

Post a Comment

If you have any doubt please let me know

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...