Rmi - Stock Market
Rmi - Stock Market
Aim: To write a java program to use the RMI concept to get stock value of shares of
companies that exits in the databases in an MSAccess table.
Algorithm:
Implementation:
1. In the implementation file we receive the input of the company’s name given by the user.
2. We set the path to the address where the mysource drive is present.
3. We create objects for the Connection and Statement classes and we execute the Query
and select all the fields in the table.
4. Now we check each company’s name with the input given, If it matches we return the
value of that particular companies share.
Server:
1. In the server side we jus create an object for the StockImpl and we bind it with the
implementer.
2. We then just print a message saying that the object has been registered.
3. If any exception occurs we catch it and just print it.
Client:
1. In the client side we first get the input from the user using the readLine() of the
DataInputStream class through our I/O.
2. After getting the input we pass it to the implementer and we lookup in the table for this
company’s name.
3. If the name is present in the table the client prints the company’s name along with its
stock value.
4. If any exception occurs we catch it and just print it.
Program:
Module:
import java.rmi.*;
interface stockWatch extends Remote
{
public int get_stockValue(String name) throws Exception;
}
Implementation:
import java.rmi.*;
import java.rmi.server.*;
import java.sql.*;
import java.io.*;
public class stockImpl extends UnicastRemoteObject implements stockWatch{
int value;
public stockImpl()throws RemoteException{
}
public int get_stockValue(String symbol) throws RemoteException{
try{
System.out.println("The company to be searched up is:"+symbol);
float price=0;
String url="jdbc:odbc:mysource2";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection myConnection=DriverManager.getConnection(url);
Statement myStatement=myConnection.createStatement();
ResultSet rs=myStatement.executeQuery("Select * from stock where
Comp_Name='"+symbol+"'");
while(rs.next())
{
value=rs.getInt(2);
System.out.print(value);
System.out.println();
}
myStatement.close();
myConnection.close();
return value;
}
catch(Exception e)
{
System.out.println("Exception caught"+e);
return 0;
}}}
Server:
import java.rmi.*;
import java.net.*;
import java.io.*;
public class rmiserver
{ public static void main(String args[])
{
try
{
stockImpl stock=new stockImpl();
Naming.bind("stockmarket",stock);
System.out.println("OBJECT IS REGISTERED");
}
catch(Exception e)
{
System.out.println("EXCEPTION :"+e);
}
}}
Client:
import java.io.*;
import java.rmi.*;
import java.net.*;
public class rmiclient
{
public static void main(String args[]) throws Exception
{
try
{
DataInputStream din=new DataInputStream(System.in);
String s1="rmi://localhost/stockmarket";
stockWatch stock=(stockWatch)Naming.lookup(s1);
System.out.println("Enter the company name:");
String name=din.readLine();
System.out.println(name+"'s one stock costs Rs"+stock.get_stockValue(name));
}
catch(Exception e)
{
System.out.println("EXCEPTION :"+e);
}
}
}
OUTPUT:
C:\CORBA>set path=c:\jdk\bin
C:\CORBA>start rmiregistry
C:\CORBA>java rmiserver
Object is registered.
C:\CORBA>set path=c:\jdk\bin
C:\CORBA>start rmiregistry
C:\CORBA>java rmiclient
Enter the company Name:
TCS
TCS’s one stock costs 1000.0
Result: Thus the java program to use the RMI concept to get stock value of shares of
companies that exits in the databases in MSAccess table was completed successfully.