Java Reference
In-Depth Information
to access the Oracle database. For the Access database, use the JDBC-ODBC bridge driver
included in the JDK. ODBC is a technology developed by Microsoft for accessing databases
on the Windows platform. An ODBC driver is preinstalled on Windows. The JDBC-ODBC
bridge driver allows a Java program to access any ODBC data source.
32.4.1 Developing Database Applications Using JDBC
The JDBC API is a Java application program interface to generic SQL databases that enables
Java developers to develop DBMS-independent Java applications using a uniform interface.
The JDBC API consists of classes and interfaces for establishing connections with data-
bases, sending SQL statements to databases, processing the results of SQL statements, and
obtaining database metadata. Four key interfaces are needed to develop any database appli-
cation using Java:
Driver
,
Connection
,
Statement
, and
ResultSet
. These interfaces
define a framework for generic SQL database access. The JDBC API defines these interfaces,
and the JDBC driver vendors provide the implementation for the interfaces. Programmers use
these interfaces.
The relationship of these interfaces is shown in FigureĀ 32.21. A JDBC application loads an
appropriate driver using the
Driver
interface, connects to the database using the
Connection
interface, creates and executes SQL statements using the
Statement
interface, and processes
the result using the
ResultSet
interface if the statements return results. Note that some state-
ments, such as SQL data definition statements and SQL data modification statements, do not
return results.
Driver
Connection
Connection
Statement
Statement
Statement
Statement
ResultSet
ResultSet
ResultSet
ResultSet
F
IGURE
32.21
JDBC classes enable Java programs to connect to the database, send SQL
statements, and process results.
The JDBC interfaces and classes are the building blocks in the development of Java data-
base programs. A typical Java program takes the following steps to access a database.
1. Loading drivers.
An appropriate driver must be loaded using the statement shown below before connecting to
a database.
Class.forName(
"JDBCDriverClass"
);
A driver is a concrete class that implements the
java.sql.Driver
interface. The drivers for
Access, MySQL, and Oracle are listed in TableĀ 32.3. If your program accesses several differ-
ent databases, all their respective drivers must be loaded.
The JDBC-ODBC driver for Access is bundled in JDK. The most recent platform inde-
pendent version of MySQL JDBC driver is
mysql-connector-java-5.1.26.jar
. This
file is contained in a ZIP file downloadable from
dev.mysql.com/downloads/connector/j/
. The
most recent version of Oracle JDBC driver is
ojdbc6.jar
(downloadable from
www.oracle
.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html
).
To use the MySQL and
mysql-connector-java-5.1.26.jar
Search WWH ::
Custom Search