JDBC and Java 2
nd
edition
age 15
The Java language employs the idea of the zero-install client. The object code for the entire
application, client and server, resides on the network. Since the JVM provides an application with a
guaranteed runtime environment, no administration is needed for the configuration of that
environment for individual applications. The users simply use a virtual machine interface such as
HotJava to locate the desired application. By clicking on the application icon, a user can run it
without even realizing the application was never stored on their local machine.
The traditional application makes a clear distinction between the locations where processing occurs.
In traditional applications, database access occurs on the server, and GUI processing occurs on the
client; the objects on the client machine talk to the database through a specialized database API. In
other situations, the client might talk to the server through a set of TCP/IP or UDP/IP socket APIs.
Either way, a wall of complex protocols divides the objects found on the client from those on the
server. Java helps tear down this wall between client and server through another piece of its
Enterprise platform, RMI.
RMI allows applications to call methods in objects on remote machines as if those objects were
located on the same machine. Calling a method in another object in Java is of course as simple as
the syntax object.method(arg). If you want to call that method from a remote machine without
RMI, however, you would have to write code that allows you to send an object handle, a method
name, and arguments through a TCP/IP socket, translate it into an object.method(arg) call on the
remote end, perform the method call, pass the return value back across the socket, and then write a
bunch of code to handle network failures. That is a lot of work for a simple method call, and I did
not even get into the issues you would have to deal with, such as passing object references as
arguments and handling garbage collection on each end. Finally, since you have written this
complex protocol to handle method calls across the network, you have serious rewriting to do if you
decide that a given object needs to exist on the client instead of the server (or vice versa).
With RMI, any method call, whether on the same machine or across the network, uses the same
Java method call syntax. This freedom to distribute objects across the network is called a distributed
object architecture. Other languages use much more complex protocols like Common Object
Request Broker Architecture (CORBA) and the Distributed Computing Environment (DCE). RMI,
however, is a Java-specific API for enabling a distributed architecture. As such, it removes many of
the complexities of those two solutions.
For a client/server database application, a distributed architecture allows the various parts of the
application to refer to the same physical objects when referring to particular objects in the data
model. For example, take an airline ticketing system that allows customers on the Internet to book
flights. Current web applications would have a user download a bunch of flight information as an
HTML page. If I book the last seat on a flight that you are viewing at the same time, you will not
see my booking of that last seat. This is because on each client screen you simply see copies of data
from the database.
If you reconstruct this web application so that it uses RMI to retrieve data from a single flight object
on the server, you can allow any number of different customers to view the exact same plane
objects at the same time. In this way, you can be certain that all viewers see any change made to the
plane object simultaneously. When I book the last seat on that flight, the flight object makes an
RMI call to all clients viewing it to let them know another seat was booked.
1.4.1 Putting It All Together
The pieces of the story are now in place. You will be using JDBC for your database access and RMI
to distribute the objects that make up your application. This book covers the JDBC API in complete