Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Chapter 3
Database Integration With JDBC

In This Chapter

This chapter discusses how to integrate databases with Java using Java Database Connectivity (JDBC) and other techniques. This chapter includes:

  The role of JDBC
  The components of JDBC and their characteristics
  JDBC alternatives

Java Database Connectivity classes are Java classes that allow an application to send SQL statements to a database management system (DBMS) and retrieve the results. JDBC functions the same as Open Database Connectivity (ODBC). One of JDBC’s strengths is interoperability — a developer can create JDBC applications without targeting a specific DBMS. Users can use specific JDBC drivers to target a specific database. Interoperability on the client side is also provided when using all Java solutions. Figure 3-1 shows Java clients running on different platforms.


Figure 3-1:  Java clients running on different platforms.

The Role of JDBC

The JDBC interface provides the application with a set of methods that enable database connections, queries, and result retrievals. It is the interface between specific database drivers and the Java user application, applet, or servlet.

The functions a user application can call are methods of connection, statements, or results object classes. Java is an object-oriented programming language, and the problems of impedance mismatch between Structured Query Language (SQL) and object-oriented programming (OOP) language have been minimized.

JDBC Characteristics

JDBC’s characteristics are:

  JDBC is a “call-level” SQL interface for Java. This interface is totally independent of the available database management systems. It is a low-level application programming interface (API) that allows a Java program to issue SQL statements and retrieve their results. It also provides methods for error and warning messages management. As shown in Figure 3-2, JDBC is located at the client side.


Figure 3-2:  JDBC is located on the client side.

  SQL conformance: JDBC does not restrict the type of queries passed to an underlying DBMS driver. An application may use as much SQL functionality as desired. The underlying drivers are authorized to claim JDBC compliance on the condition they fully support ANSI SQL-92 Entry Level. SQL-2 Entry Level conformance is widely supported today and guarantees a wide level of portability.
  JDBC may be implemented on top of common SQL level APIs, in particular on top of ODBC.
  JDBC provides a Java interface that stays consistent with the rest of the Java system. There are no conflicts because of opposed philosophies expressed by the impedance mismatch between the object-oriented world (Java) and the tabular world (SQL).
  The JDBC mechanisms are simple to understand and use. This simplicity does not mean that functionality suffers.
  JDBC uses strong, static typing whenever possible. This approach allows for performing more error checking at compile time. It should not be a limitation to JDBC’s usage, however.
  One functionality, one method: This concept has been adopted, as opposed to many other DBMS SQL level APIs, to keep it simple yet powerful for the beginner as well as the experienced developer.

JDBC Components

The following are JDBC components:

Application: The user application invokes JDBC methods to send SQL statements to the database and retrieve results. It performs these tasks:

  Requests a connection with a data source
  Sends SQL statements to the data source
  Defines storage areas and data types for the result sets
  Requests results
  Processes errors
  Controls transactions: requests commit or rollback operations
  Closes the connection

Driver Manager: Its primary purpose is to load specific drivers for the user application. It may also perform the following:

  Locate a driver for a particular database
  Process JDBC initialization calls
  Provide entry points to JDBC functions for each specific driver
  Perform parameter and sequence validation for JDBC calls

Driver: The driver processes JDBC methods invocations, sends SQL statements to a specific data source, and returns results back to the application. When necessary, the driver translates and/or optimizes requests so the request conforms to the syntax supported by the specific DBMS. It will:

  Establish a connection to a data source
  Send requests to the data source
  Perform translations when requested by the user application
  Return results to the user application
  Format errors in standard JDBC error codes
  Manipulate cursors if necessary
  Initiate transactions, if explicitly required

There are three types of JDBC drivers.

Proprietary Database Drivers: They process JDBC calls and send SQL statements to the data source. They may be “native-API partly-Java” or “native-protocol all-Java.” A native-API driver forwards the calls to a locally installed library, usually developed in C and provided by the database vendor. It may be a Dynamic Link Library (DLL) or a .so shared library. Figure 3-3 shows native-API partly-Java drivers.


Figure 3-3:  Native-API partly-Java drivers.

A native-protocol all-Java driver implements in Java all the layers necessary to communicate with the database. They are fully portable because they do not use local libraries or other native code. Figure 3-4 shows native-protocol all Java-drivers.


Figure 3-4:  Native-protocol all Java-drivers.

Bridge Drivers: This driver creates a bridge between JDBC and another Call Level Interface (CLI). For example, the JDBC-ODBC Bridge is a bridge driver. It processes JDBC calls and, in turn, calls ODBC functions that will send SQL statements to the ODBC data source. Figure 3-5 shows the JDBC-ODBC Bridge driver.


Figure 3-5:  The JDBC-ODBC bridge driver.

DBMS-independent all-Java Net drivers: These drivers use a DBMS-independent published network protocol. They are very portable because they are 100 percent Java. Figure 3-6 shows the Net Driver.


Figure 3-6:  The Net driver.

Data Source: The data source consists of the data the user application wants to access and its associated parameters — that is, the type of DBMS and network layer (if any) used to access the DBMS.

The JDBC interface defines the possible interactions between the user application and the driver manager. Figure 3-7 shows the relationship between the four JDBC components.


Figure 3-7:  The complete JDBC architecture.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.