RMI Database Access Simple Application


RMI Database Access Application

Before this program, it is advised to read the previous simple application InterestCalculation where the basic notes and guidelines are given. This application adds extra features to the previous application like database access, security features and handling exceptions thrown by each method instead of throwing a generic Exception.

In this Bank application, the client requests the bank to supply name and account balance by supplying account number to the server. The implementation file returns the client data in the form of array list. In the client code, the data is retrieved from the array list and displayed to the user.

Like the previous example, this RMI Database application also includes 4 programs.

1. Remote interface
File Name: Bank.java

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;
public interface Bank extends Remote
{
  public abstract ArrayList getInfo(int accountNumber) throws RemoteException;
}

2. Implementation Program
File Name: BankImpl.java

import java.rmi.*;   
import java.rmi.server.*;
import java.util.*;
import java.sql.*;

public class BankImpl extends UnicastRemoteObject implements Bank
{
  public BankImpl() throws RemoteException {
    super();
  }

  public ArrayList getInfo(int accountNumber) throws RemoteException
  {
    ArrayList al1 = new ArrayList();
    try				      			                                                // your JDBC code here     
    {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");    // for better proformance use Type 4 driver
      Connection con = DriverManager.getConnection("jdbc:odbc:snrao", "scott", "tiger");
      Statement stmt = con.createStatement( );

      ResultSet res = stmt.executeQuery("select *from BankDatabase where accountID="+accountNumber);
       
      res.next();
      int k = res.getInt(1);                                         // for account id
      String name = res.getString(2);                      // for account holder name
      double balance = res.getDouble(3);              // for balance

      al1.add(new Integer(k));         	
      al1.add(name);
      al1.add(new Double(balance));

      al1.add("Thank You");  
      al1.add("\nATTENTION:\n If you find any discrepancy, please call the branch office immediately");

      res.close();
      stmt.close();
      con.close();
    }
    catch(Exception e)
    {
      al1.add("Accopunt number is not found. " + e);
    }
    return al1;
  }
}

3. Server Program
File Name: BankServer.java

import java.rmi.RemoteException;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.net.MalformedURLException;

public class BankServer
{
  public static void main(String args[]) throws Exception
  {
    try
    {
      if(System.getSecurityManager() == null)  
      {
        System.setSecurityManager(new RMISecurityManager());  
      }     

      Bank b1= new BankImpl();
      Naming.rebind("jasmine", b1);
      System.out.println("Server is ready for remote invocations by client");
    }
    catch(RemoteException e)
    {                                         // thrown when unable to invoke
      System.out.println("The problem is "+e);
    }
    catch(MalformedURLException e)
    {                                         // thrown when the client does not find server
      e.printStackTrace();
    }                                        // catch close
  }
}

The server after binding with the RMI registry, prints a confirmation message (to the server side developer) and this indicates the developer that the server is ready to handle client calls. As long as the remote object b1 has any client reference, b1 is not garbage collected and server does not shut down.

RMI Security Manager

  • The RMI security manager protects access to system data from unauthorized and untrusted downloaded code working on JVM.
  • The security manager checks the downloaded files has permission to local files and do oprations without proper privileges.
  • The security manager restricts the operations for downloaded files to security policy set earlier.

Following sets RMISecurityManager

if (System.getSecurityManager() == null) // if no security is provided
{
System.setSecurityManager(new RMISecurityManager()); // provide with RMI Security manager
}

To work with, the Developer is required to set the policy file to grant required permission effects. Developer still is required to go for digital signatures, export/import digital certificates and keystores etc for more security measuers and are required for sensitive banking applications (using different tools available in the markets).

4. Client Program
File Name: InterestClient.java

This program obtains a reference of the remote object of server and invokes remote methods.

import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Iterator;

public class BankClient
{
  public static void main(String args[])
  {
    try
    {
      Bank b2 = (Bank) Naming.lookup("jasmine");

      ArrayList al2 = b2.getInfo(3519);
      Iterator it = al2.iterator();
     
      System.out.println("Account ID: " + it.next());
      System.out.println("Accopunt Holder Name: " + it.next());
      System.out.println("Account Balance: " + it.next());
      System.out.println(it.next());            // for message
      System.out.println(it.next());            // for message
    }
    catch(NotBoundException e)
    {                                                                        // thrown when unable to bind with registry
                                                                             // due to wrong alias name
      e.printStackTrace();
    }
    catch(RemoteException e)
    {                       // thrown when unable to invoke
                            // due to wrong method name
      e.printStackTrace();
    }
    catch(MalformedURLException e)
    {                       // thrown due to wrong server address
      e.printStackTrace();
    }
  }
}    

Leave a Comment

Your email address will not be published.