At the Microsoft Professional Developer's Conference in March 1996,
Microsoft hailed a new era of software development that will concentrate on
intranets, the Web, ActiveX, HTML, applets, browsers, and Java. Microsoft
emphasized the integration of tool sets for the various versions of Windows and
Internet/intranet application development. It also revealed a unified shell and
browser that may become a universal client (Internet Explorer--IE--4.0).
If you don't think Microsoft's new direction is relevant to your situation,
you need to know that surveys show most organizations will build intranets. As a
result, developers face connecting databases to supply content to thousands of
Web browser users, even where those databases are not accessible from the
Internet. To meet this need, more and more developers are relying on the Java
programming language to access SQL databases (for information about the benefits
of Java, see the sidebar, "Programming with Java," page 111).
In this article, I describe several aspects of programming SQL databases
with Java, including the Open Database Connectivity (ODBC) and Java Database
Connectivity (JDBC) APIs. A simple Java program illustrates how to insert data
into an ODBC data source. You'll learn how to add interoperability to your Java
database programs with adaptive programming, and how to serve many concurrent
database users by writing multitier applications in Java.
The Basics of Java
Java is well known for creating Web applets that you download with HTML
pages, but you can also use it to program applications, including SQL
applications. Java programs are of two types. Java applets are
components embedded in Web pages that a browser executes. Java applications
are main programs that the Java interpreter, the Java virtual machine, executes.
Java supports a distributed, client/server computing model, and today's
Web-orientation exemplifies this model. Applets run as clients and conform to
strict client security rules. Applications usually execute at a server, but they
can run anywhere a Java virtual machine exists. In a typical scenario, a Windows
NT server can support PC, Macintosh, OS/2, and other clients. To embed an applet
in an HTML document, you specify an <APPLET> tag, but other scenarios will
soon appear. Microsoft applications will soon use applets in a way that is
similar to how they use Visual Basic custom controls (VBXs), Object Linking and
Embedding custom controls (OCXs), and ActiveX controls.
As an Object-Oriented Programming (OOP) language, Java lets you write
programs that use inheritance, encapsulation, and data hiding. Java includes I/O
streams, exception handling, and sockets for client/server communication.
The Java architecture includes Unicode strings and characters and a
security model that makes developing secure clients easier. Java includes
packages that are analogous to application frameworks, which you use with C++
compilers. Java's built-in support for threads also simplifies developing
multithreaded clients that can exploit ODBC asynchronous processing mode and
high-performance JDBC drivers. Java 1.1, released in December 1996, includes
Java Beans (interfaces for interoperable components), Remote Method Invocation
(RMI), and other interfaces.
ODBC and JDBC
Multidatabase APIs have gained favor with developers working with SQL
databases. Microsoft's ODBC and JavaSoft's JDBC let you use one API to write
programs that operate on various SQL databases. Java developers can also use
Microsoft data access APIs where the target execution environment is 32-bit
Windows. Presently, ODBC or JDBC is the best choice for developing multiplatform
programs. Let's examine ODBC and JDBC to see what that choice involves.
ODBC is the most widely used call-level interface for accessing SQL
databases. JDBC is a new API that provides an object layer that works with ODBC
drivers to abstract SQL programming for Java developers.
The ODBC and JDBC architectures include loadable database drivers and a
driver manager. Database drivers are conceptually similar to printer drivers
because database drivers let you expand a program's functionality without
rewriting its source code. The database drivers are libraries that the driver
manager invokes when your program connects to an ODBC or JDBC data source. Your
program can use drivers that work with a specific database management system
(DBMS), such as Oracle, or drivers that connect to ODBC and JDBC servers.
Although these servers can connect to multiple data sources, clients use only
one driver to connect to the server. This server-based architecture produces a
thinner client by replacing multiple drivers, client libraries, and protocol
stacks with one driver and network transport. When your code uses ODBC or JDBC,
a driver manager validates the arguments in your call and loads the appropriate
driver for the data source to which you are connecting. The UNIX and Macintosh
versions use shared libraries for the driver manager and drivers, whereas NT,
other Windows versions, and OS/2 use DLLs.
Dozens of ODBC drivers already exist, so INTERSOLV developed bridge
software for JavaSoft to let JDBC applications use ODBC drivers. Figure 1
illustrates the JDBC architecture that includes drivers, a driver manager, and a
bridge for accessing ODBC data sources.
All ODBC and JDBC data access uses SQL queries to define and manipulate
database tables. ODBC and JDBC drivers understand the same SQL (ANSI SQL with
X/Open escape clauses) and models for query preparation and execution.