Dec 20, 2009

Introduction to JDBC

JDBC stands for Java Database Connectivity. Imagine that you have given the task of developing an application for a store to enable the store owner to maintain the daily transactions. Java application needs to communicate with the Database to perform these tasks. Sun Microsystems has included JDBC API as a part of JDK to enable java applications to communicate with a database.

Features of JDBC

  1. JDBC contains a set of classes and interfaces that are used to connect to a database built using any DBMS / RDBMS submit SQL queries to a database, retrieves and process the results of SQL queries.
  2. JDBC is a low level interface in which select and update statements are called directly from with in java programs.
  3. JDBC can be used with both two tier and three architectures. In two tier architecture a java program invokes the methods of JDBC API, which in turn communicates with the database server. In three tier architecture, a java applet or HTML form submits the SQL queries to the middle tier server, the middle tier server in turn communicates with the database server.

JDBC Drivers

The following two issues have to be considered while using JDBC.

  1. Java applications cannot communicate directly with database to retrieve and store data. This is because a database server cannot understand Java statements. Hence you need a mechanism that translates Java statements to SQL statements.
  2. There are different kinds of DBMS/RDBMS products that are available in today. A Java application should be DBMS independent to communicate with all these products without any changes.

JDBC API uses a driver to address these issues. JDBC API takes care of converting Java commands to SQL statements. However to address specific database issues, each database vendor provides a driver along with their database. A java application invokes the methods of JDBC API to communicate with the database. JDBC API in turn uses the driver to communicate with the database.

JDBC API submits the queries to the JDBC driver. The JDBC driver in turn converts the queries to a form that a DBMS/RDBMS can understand. The JDBC also retrieves the results of SQL queries and converts it into JDBC API classes and objects that can be used by the application. Since the JDBC driver takes care of the interaction with the database, any changes made in the database do not affect the application.


 

  
 

There are several categories of JDBC drivers provided by different database vendors. They are,

  1. JDBC-ODBC bridge driver
  2. Native API partly Java driver and
  3. Native protocol pure Java driver / JDBC-Net pure Java driver

JDBC-ODBC bridge driver

DBMS / RDBMS such as MS Access and SQL server contain the ODBC driver embedded in to them. A java program cannot communicate directly with ODBC API, since the ODBC API is written in C language and make use of pointers and other constructs that java does not supports. Sun Microsystems provides a driver to communicate with ODBC data sources from JDBC. The JDBC-ODBC bridge driver translates the JDBC API to ODBC API.




Native API partly Java driver

Some DBMS/RDBMSs such as DB2 and Informix contains a JDBC driver supplied by he vendor. These JDBC drivers consist of classes that can be invoked directly by the JDBC API.

Native protocol pure Java driver / JDBC-Net pure Java driver

These drivers are used to connect a client application or Applet to a database over a TCP / IP connection.

JDBC Driver Manager

The JDBC Driver Manager is the back bone of the JDBC architecture. It maintains a list of drivers created by different Database vendors. It connects the Java application to the appropriate specified in the Java program.



 
 

 
 

 
 

 
 

 
 

 
 


  

No comments:

Post a Comment