Unit-V Java
Unit-V Java
Unit: V
RMI- JDBC- Developing Java Program for RMI and JDBC.
RMI stands for Remote Method Invocation. It is a mechanism that allows an object residing in
one system (JVM) to access/invoke an object running on another JVM. RMI is used to build
distributed applications; it provides remote communication between Java programs. It is provided in the
package java.rmi.
The following diagram (Fig 5.1) shows the architecture of an RMI application.
Transport Layer − This layer connects the client and the server. It manages the existing connection and
also sets up new connections.
Stub − A stub is a representation (proxy) of the remote object at client. It resides in the client system;
it acts as a gateway for the client program.
Skeleton − This is the object which resides on the server side. stub communicates with this skeleton to
pass request to the remote object.
RRL(Remote Reference Layer) − It is the layer which manages the references made by the client to
the remote object.
When the client-side RRL receives the request, it invokes a method called invoke() of the
object remoteRef. It passes the request to the RRL on the server side.
The RRL on the server side passes the request to the Skeleton (proxy on the server) which finally
invokes the required object on the server.
At the server side, the packed parameters are unbundled and then the required method is
invoked. This process is known as unmarshalling.
lookup( )- To invoke a remote object, the client needs a reference of that object. At that time, the client
fetches the object from the registry using its bind name (using lookup() method).
Adder.java
import java.rmi.*;
public interface Adder extends Remote
{
public int add(int x,int y)throws RemoteException;
public int sub(int x,int y)throws RemoteException;
}
5.2.2 Provide the implementation of the remote interface
For providing the implementation of the Remote interface, we need to Either extend the
UnicastRemoteObject class,or use the exportObject() method of the UnicastRemoteObject class.In
case, you extend the UnicastRemoteObject class, you must define a constructor that declares
RemoteException.
AdderRemote.java
import java.rmi.*;
import java.rmi.server.*;
public class AdderRemote extends UnicastRemoteObject implements Adder
{
AdderRemote()throws RemoteException
{
super( );
}
public int add(int x,int y)
{
return x+y;
}
public int add(int x,int y)
{
return x-y;
}
}
5.2.3 Create the stub and skeleton objects using the rmic tool.
The rmic tool invokes the RMI compiler and creates stub and skeleton objects.
>rmic AdderRemote
MyClient.java
import java.rmi.*;
public class MyClient
{
public static void main(String args[])
{
try
{
Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/addition");
System.out.println(“The sum is “+stub.add(34,4));
System.out.println (“The Difference is”+stub.sub(20,10));
}
catch(Exception e)
{
System.out.println(e);
}
}
}
For running this rmi example,
javac *.java
rmic AdderRemote
start rmiregistry
java MyServer
java MyClient
Fundamentally, JDBC is a specification that provides a complete set of interfaces that allows for
portable access to an underlying database. Java can be used to write different types of executables, such
as :
Java Applications
Java Applets
Java Servlets
Java Server Pages (JSPs)
Enterprise JavaBeans (EJBs).
All of these different executables are able to use a JDBC driver to access a database, and take
advantage of the stored data.
The JDBC API supports both two-tier and three-tier processing models for database access but in
general, JDBC Architecture consists of two layers :
The JDBC API uses a driver manager and database-specific drivers to provide transparent connectivity
to heterogeneous databases. Following is the architectural diagram, which shows the location of the
driver manager with respect to the JDBC drivers and the Java application.
JDBC drivers are client-side adapters (installed on the client machine, not on the server) that convert
requests from Java programs to a protocol that the DBMS can understand. There are 4 types of JDBC
drivers:
Type-1 driver
Type-1 driver or JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-
ODBC bridge driver converts JDBC method calls into the ODBC function calls. Type-1 driver is also
called Universal driver because it can be used to connect to any of the databases.
As a common driver is used in order to interact with different databases, the data transferred
through this driver is not so secured.
The ODBC bridge driver is needed to be installed in individual client machines.
Type-1 driver isn’t written in java, that’s why it isn’t a portable driver.
Type-2 driver
The Native API driver uses the client -side libraries of the database. This driver converts JDBC method
calls into native calls of the database API. In order to interact with different database, this driver needs
their local API, that’s why data transfer is much more secure as compared to type-1 driver.
Type-3 driver
The Network Protocol driver uses middleware (application server) that converts JDBC calls directly or
indirectly into the vendor-specific database protocol. Here all the database connectivity drivers are
present in a single server, hence no need of individual client-side installation.
Type-3 drivers are fully written in Java, hence they are portable drivers.
No client side library is required because of application server that can perform many tasks like
auditing, load balancing, logging etc.
Network support is required on client machine.
Maintenance of Network Protocol driver becomes costly because it requires database-specific
coding to be done in the middle tier.
Type-4 driver
Type-4 driver is also called native protocol driver. This driver interact directly with database. It does
not require any native database library, that is why it is also known as Thin Driver.
Does not require any native library and Middleware server, so no client-side or server-side
installation.
It is fully written in Java language, hence they are portable drivers.
The following 5 steps are the basic steps involve in connecting a Java application with Database using
JDBC
1. Register the Driver- Class.forName() is used to load the driver class explicitly.
2. Create a Connection- getConnection() method of DriverManager class is used to create a
connection.
Syntax
getConnection(String url)
getConnection(String url, String username, String password)
getConnection(String url, Properties info)
Syntax:
public Statement createStatement() throws SQLException
5. Closing the connection-After executing SQL statement you need to close the connection and
release the session. The close() method of Connection interface is used to close the connection.
Syntax:
public void close() throws SQLException
The following SQL statement creates a table named student with three columns −
The following SQL INSERT statement inserts a new row in the student database created earlier SQL>
The JDBC program which access the data inserted into the student table and displays the data from the
database
OracleCon.Java
import java.sql.*;
class OracleCon
{
public static void main(String args[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
"system", "root");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from student");
while(rs.next( ))
{
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3) );
}
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
Two Marks
1. Write in brief about the working principle of the RMI.
RMI stands for Remote Method Invocation. It is a mechanism that allows an object
residing in one system (JVM) to access/invoke an object running on another JVM. RMI is
used to build distributed applications; it provides remote communication between Java programs.
It is provided in the package java.rmi.
At the server side, the packed parameters are unbundled and then the required method is
invoked. This process is known as unmarshalling.
Statement.
PreparedStatement.
CallableStatement.
ResultSet.
Connection
SQLException
12. List out the different types of Drivers in JDBC
There are 4 types of JDBC drivers:
Type-1 driver or JDBC-ODBC bridge driver
Type-2 driver or Native-API driver
Type-3 driver or Network Protocol driver
Type-4 driver or Thin driver