RMI-StoreMarket

download RMI-StoreMarket

of 4

Transcript of RMI-StoreMarket

  • 8/8/2019 RMI-StoreMarket

    1/4

    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:

    Table creation and connection establishment:1.Open MS-Access from program and create a table with the fields like company name,

    stock value and save the table in the name of stock.

    2.Give some values in the table with the company names and their corresponding sharevalues.

    3.Now save the values and go to RUN and give obbead32 and choose the drive in whichyour table resides and add the MS-Access Driver file.

    4.Now give the drive source name as mysource and click on start then select the stock tablethat was created and click ok.

    Implementation:

    1.In the implementation file we receive the input of the companys 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 Queryand select all the fields in the table.4.Now we check each companys 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 theDataInputStream class through our I/O.2.After getting the input we pass it to the implementer and we lookup in the table for this

    companys name.3.If the name is present in the table the client prints the companys name along with its

    stock value.4.If any exception occurs we catch it and just print it.

  • 8/8/2019 RMI-StoreMarket

    2/4

    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)

    {

  • 8/8/2019 RMI-StoreMarket

    3/4

    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);

  • 8/8/2019 RMI-StoreMarket

    4/4

    }}

    }

    OUTPUT:

    Server side commands:

    C:\CORBA>set path=c:\jdk\bin

    C:\CORBA>start rmiregistry

    C:\CORBA>javac rmiserver. java

    C:\CORBA>java rmiserverObject is registered.

    The company to be searched up is: TCS

    1000.0

    Client side commands:

    C:\CORBA>set path=c:\jdk\bin

    C:\CORBA>start rmiregistry

    C:\CORBA>javac rmiclient. java

    C:\CORBA>java rmiclient

    Enter the company Name:TCS

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