Learning Resources
Types of Drivers
JDBC Drivers
The connection to the database is handled by the JDBC Driver class. The Java SDK contains only one JDBC driver, a jdbc-odbc
bridge that can communicate with an existing Open DataBase Conectivity (ODBC) driver. Other databases need a JDBC driver specific to that database.
To get a general idea of what the JDBC driver does, you can examine the JDCConnectionDriver.java
file. The JDCConnectionDriver class implements the java.sql.Driver
class and acts as a pass-through driver by forwarding JDBC requests to the real database JDBC Driver. The JDBC driver class is loaded with a call to Class.forName(drivername)
.
These next lines of code show how to load three different JDBC driver classes:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Class.forName("postgresql.Driver"); Class.forName("oracle.jdbc.driver.OracleDriver");
Each JDBC driver is configured to understand a specific URL so multiple JDBC drivers can be loaded at any one time. When you specify a URL at connect time, the first matching JDBC driver is selected.
The jdbc-odbc bridge accepts Uniform Resource Locators (URLs) starting with jdbc:odbc:
and uses the next field in that URL to specify the data source name. The data source name identifies the particular database scheme you wish to access. The URL can also include more details on how to contact the database and enter the account.
//access the ejbdemo tables String url = "jdbc:odbc:ejbdemo";
This next example contains the Oracle SQL*net information on the particular database called ejbdemo on machine dbmachine
String url = "jdbc:oracle:thin:user/password@( description=(address_list=( address=(protocol=tcp) (host=dbmachine)(port=1521)))(source_route=yes) (connect_data=(sid=ejbdemo)))"; |
This next examples uses mysql
to connect to the ejbdemo database on the local machine. The login user name and password details are also included.
String url = "jdbc:mysql://localhost/ejbdemo?user=user; password=pass";
JDBC drivers are divided into four types. Drivers may also be categorized as pure Java or thin drivers to indicate if they are used for client applications (pure Java drivers) or applets (thin drivers). Newer drivers are usually Type 3 or 4. The four types are as follows: Type 1 Drivers
Type 1 JDBC drivers are the bridge drivers such as the jdbc-odbc bridge. These drivers rely on an intermediary such as ODBC to transfer the SQL calls to the database. Bridge drivers often rely on native code, although the jdbc-odbc library native code is part of the Java1 2 virtual machine. Type 2 Drivers
Type 2 Drivers use the existing database API to communicate with the database on the client. Although Type 2 drivers are faster than Type 1 drivers, Type 2 drivers use native code and require additional permissions to work in an applet.
A Type 2 driver might need client-side database code to connect over the network. Type 3 Drivers
Type 3 Drivers call the database API on the server. JDBC requests from the client are first proxied to the JDBC Driver on the server to run. Type 3 and 4 drivers can be used by thin clients as they need no native code. Type 4 Drivers