Java Database Connectivity

 

1

 

Java Database Connectivity (abbreviated JDBC) allows you to easily create Java applets and applications that can access data from many different types of database systems such as Microsoft SQL Server, Oracle, Sybase and other popular database servers.

Understanding JDBC

Like the Microsoft ODBC (Open Database Connectivity) API on which it is based, the JDBC API is a standard application programming interface that simplifies the process of connecting Java client applications with back-end database servers. The JDBC API consists of a set of Java classes that use predefined methods to handle various data access functions such as selecting the appropriate database driver to access a particular database, connecting to the database, submitting SQL statements to the DBMS, and processing the results returned by the DBMS.

The four main classes in the JDBC API hierarchy are briefly summarized below:

Driver Manager Class. The JDBC DriverManager class loads the JDBC driver needed to access a particular data source, locates and logs on to the database and returns a Connection object.

Connection Class. The JDBC Connection class manages the communications between a Java client application and a specific database, including passing SQL statements to the DBMS and managing transactions.

Statement Class. The JDBC Statement class contains SQL strings that are submitted to the DBMS. A SQL Select statement returns a ResultSet object.

ResultSet Class. The JDBC ResultSet class provides predefined methods to access, analyze, and convert data values returned by an executed SQL Select statement.

When a Form is converted into an Java applet, the data access code is also automatically converted to use the Java ado library which is build on top of these JDBC classes to access and manipulate the data displayed in the applet.



How JDBC Works

To provide a database-independent interface for Java client applications, JDBC is divided into two API layers: the JDBC API and the JDBC Driver API. The JDBC API handles all communication between the Java applet and the JDBC Driver Manager. The JDBC Driver API handles all communication between the JDBC Driver Manager and the driver for a specific DBMS.

Figure 1: JDBC Architecture

The required JDBC driver must be registered with the JDBC Driver Manager before a connection between the Java applet and the JDBC data source can be made. Once a driver is registered and loaded, the Driver Manager can use that driver to connect the Java applet to its associated data source. Once the connection is established, all database calls go directly to the JDBC driver itself, bypassing the Driver Manager.

Because JDBC presents a standard programming interface to various data sources, you can easily switch drivers if, for example, a new JDBC driver becomes available for your database. JDBC's two-tiered design is also valuable in distributed database environments where the same Java applet may connect to more than one type of database.

Because JDBC is a new standard and there are relatively few native JDBC drivers available, you can use the JDBC-ODBC Bridge driver provided with the Java SDK to let your Java applet access an existing ODBC data source. Unlike native JDBC drivers that can be dynamically downloaded at run time, the JDBC-ODBC Bridge driver must be installed on each client machine, along with the required ODBC driver. See your ODBC driver documentation for information on how to install ODBC drivers and define ODBC data sources.



Using JDBC Data Sources and Database URLS

As in ODBC, JDBC uses a predefined data source to store the location of a particular database and the driver that is used to connect to that database. When you create a JDBC data source, the database location is specified by a URL (Uniform Resource Locator) string, which uses a similar format to the syntax used to specify the location of an HTML file (see below).

HTML Page URL Syntax

http://domain:port/directory/filename

For example, when you enter the following URL in your Web browser:

http://www.diamondedge.com:80/contact.html

It specifies the connection protocol (http), domain (www.diamondedge.com), server port (80) and filename (contact.html) required to access the Contact Information page on Diamond Edge's World Wide Web site.

In a JDBC data source definition, the database location is specified using the following URL format:

<protocolname>:<subprotocol>:<subname>

For a JDBC database URL, the protocol name is always JDBC. The subprotocol identifies the type of driver JDBC uses to access the database. The subname can be the JDBC data source name or the name of the database file. The subname format varies, depending on which JDBC driver is being used. Check your JDBC driver documentation for information on how to specify the URL subname.

For example, in the JDBC URL:

jdbc:odbc:dmv

the jdbc protocol is used to initiate the database connection, the subprotocol (odbc) specifies that the connection should be made using the JDBC-ODBC Bridge driver, and the data source to connect to is named dmv which was defined in the Windows Control Panel (ODBC).