Database Reference
In-Depth Information
Java-oriented products—JDBC was not an acronym; it just stood for JDBC. At this point in time,
however, we can even find the name Java Database Connectivity ( JBDC) on Oracle's Web site
(Oracle Corporation purchased Sun Microsystems in January of 2010—see http://www.oracle
.com/technetwork/java/javase/tech/index-jsp-136101.html ) ! Still, because we use acronyms in
this topic after introducing the full term, we will use JDBC .
A JDBC driver is available for almost every conceivable DBMS product. Oracle maintains
a directory of them available through http://www.oracle.com/technetwork/java/javase/jdbc/
index.html . Some of the drivers are free, and almost all of them have an evaluation edition that
can be used for free for a limited period of time. The JDBC driver for MySQL is the MySQL
Connector/J, which is available at http://dev.mysql.com/downloads/connector/j/ .
Driver Types
As summarized in Figure 11-20, there are four defined JDBC driver types. Type 1 drivers are
JDBC-ODBC bridge drivers, which provide an interface between Java and regular ODBC driv-
ers. Most ODBC drivers are written in C or C++. For reasons unimportant to us here, there are
incompatibilities between Java and C/C++. Bridge drivers resolve these incompatibilities and
allow access to ODBC data sources from Java. Because we use ODBC in this chapter, if you are
using MySQL, you will want to download the MySQL Connector/ODBC driver. The MySQL
Connector/ODBC is available from http://dev.mysql.com/downloads/connector/odbc/ . Note
that the MySQL ODBC connector for Windows operating systems is included in the MySQL
Installer for Windows discussed in Chapter 10C.
Drivers of Types 2 through 4 are written entirely in Java; they differ only in how they con-
nect to the DBMS. Type 2 drivers connect to the native API of the DBMS. For example, they call
Oracle Database using the standard (non-ODBC) programming interface to Oracle Database.
Drivers of Types 3 and 4 are intended for use over communications networks. A Type 3 driver
translates JDBC calls into a DBMS-independent network protocol. This protocol is then trans-
lated into the network protocol used by a particular DBMS. Finally, Type 4 drivers translate
JDBC calls into DBMS-specific network protocols.
To understand how drivers Types 2 through 4 differ, you must first understand the differ-
ence between a servlet and an applet . As you probably know, Java was designed to be portable.
To accomplish portability, Java programs are not compiled into a particular machine language,
but instead are compiled into machine-independent bytecode. Oracle, Microsoft, and others
have written bytecode interpreters for each machine environment (Intel 386, Alpha, and so
on). These interpreters are referred to as Java virtual machines .
To run a compiled Java program, the machine-independent bytecode is interpreted by
the virtual machine at run time. The cost of this, of course, is that bytecode interpretation
constitutes an extra step, so such programs can never be as fast as programs that are compiled
directly into machine code. This may or may not be a problem, depending on the application's
workload.
An applet is a Java bytecode program that runs on the application user's computer. Applet
bytecode is sent to the user via HTTP and is invoked using the HTTP protocol on the user's
computer. The bytecode is interpreted by a virtual machine, which is usually part of the browser.
Because of portability, the same bytecode can be sent to a Windows, a UNIX, or an Apple computer.
Figure 11-20
Summary of JDBC Driver
types
Driver Type
1
Characteristics
JDBC-ODBC bridge. Provides a Java APl that interfaces to
an ODBC driver. Enables processing of ODBC data sources
from Java.
A Java APl that connects to the native-library of a DBMS
product. The Java program and the DBMS must reside on the
same machine, or the DBMS must handle the intermachine
communication, if not.
A Java APl that connects to a DBMS-independent network
protocol. Can be used for servlets and applets.
A Java APl that connects to a DBMS-dependent network
protocol. Can be used for servlets and applets.
2
3
4
 
Search WWH ::




Custom Search