JDBC and ODBC
The JDBC API can access any kind of tabular data, especially data stored in a Relational Database. It works on top of ODBC which was the driver for database connectivity since age old days but since ODBC was implemented in C so people from the VB background had some problems in understanding the implementation difficulties.
Since JDBC works on top of ODBC we have something called as a JDBC-ODBC bridge to access the database. The details would be seen in the topic to follow.
ODBC is an abbreviation of Open Database Connectivity, a standard database access method developed by Microsoft Corporation. The goal of ODBC is to make it possible to access any data from any application, regardless of which database management system (DBMS) is handling the data.
ODBC manages this by inserted a middle layer, called a driver, between an application and the DBMS. The purpose of this layer is to, translate the queries of the application into commands that the DBMS understands.
For this to work, both the application and the DBMS must be ODBC-complaint-that is, the application must be capable of issuing ODBC commands and the DBMS must be capable of responding to them.
JDBC provides a database-programming interface for java programs. Since the ODBC is written in C Language, a Java program can’t directly communicate with an ODBC driver.
JavaSoft created the JDBC-ODBC Bridge driver that translates the JDBC API to the ODBC API. It is used with ODBC drivers.
JDBC Driver Manager
The JDBC Driver Manager is the backbone of the JDBC architecture. The function of the JDBC driver manager is to connected a Java application to the appropriate driver.
The JDBC-ODBC bridge allows you to use the ODBC driver as JDBC drivers.
JDBC Application Architecture
Connection to a Database
The java.sql package contains classes that helps in connecting to a database, sending SQL statement to the database, and processing query results.
The connection object represents a connection with a database. You may have several connection objects in an application that connects to one or more databases.
Loading the JDBC-ODBC Bridge and Establishing Connection
To establish a connection with a database, you need to register the ODBC-JDBC Driver by calling the forName ( ) method from the Class class and then calling the getConnection ( ) method from the DriverManager class.
The getConnection ( ) method of the DriverManager class attempts to locate the driver that can connect to the database represented by the JDBC URL passed to the getConnection ( ) method.
The JDBC URL is a string that provides a way of identifying a database. A JDBC URL is divided into three parts:
- <protocol> in a JDBC URL is always jdbc.
- <subprotocol> is the name of the database connectivity mechanism. If the mechanism of retrieving the data is ODBC-JDBC Bridge, the subprotocol must be odbc.
- <subname> is used to identify the database.