JDBC Architecture

JDBC Architecture in Java

In this article, I am going to discuss JDBC Architecture in Java Applications. Please read our previous article where we discussed what is JDBC and why we need JDBC in Java Applications. At the end of this article, you will understand the following pointers in detail.

  1. JDBC Architecture
  2. Components of JDBC
  3. JDBC client
  4. JDBC API
  5. What does API contain?
  6. Understanding java.sql package
  7. Driver Manager
  8. JDBC Driver
  9. Database Server
JDBC Architecture:

In order to understand the JDBC Architecture, please have a look at the following image.

JDBC Architecture

Components of JDBC

JDBC architecture is a client-server architecture. JDBC architecture has 5 elements.

  1. JDBC client: Here JDBC Client is nothing but a java application that wants to communicate with the database.,
  2. JDBC API: To write a java program that communicates with any database without changing the code Sun Microsystem has released JDBC API. By JDBC API implementation we can call JDBC Driver.
  3. JDBC Driver: It is a java application that acts as an interface between java applications and databases. It understands the given java calls and converts them into database calls and vice-versa.
  4. JDBC Driver Manager: It acts as a data structure that collects a group of JDBC Drivers and allows the programmer to select the driver dynamically for the database connection.
  5. Database Server: It is nothing but the Database server like Oracle, MySQL, SQL Server, etc. with which the JDBC client wants to communicate.

Let us discuss each of the above components in detail.

JDBC client:

In the context of client-server architecture, the resource which makes the request is nothing but a client. Any java application i.e. communicating with the database using JDBC is nothing but a JDBC client or we can say JDBC application.

JDBC client

So the java program is the client. JDBC CLIENT has the following responsibility while communicating with the database.

  1. Requesting for the database connection.
  2. Submitting the appropriate SQL statements to the DBMS.
  3. Dealing with the exception
  4. Making transactions whenever required.
  5. Closing the connection.
JDBC API:

API (Application Programming Interface) is nothing but a collection of library methods. Library classes and interfaces contain library methods and these library classes and interfaces are present inside some predefined packages. Packages are just like a folder that contains files.

A collection of library methods of library interfaces and classes of java.sql and javax.sql packages using which database operations are performed in a JDBC application is nothing but a JDBC API.

Java.sql: It is used for advanced operations of the database, i.e., Connection pooling.

Javax.sql: Provides the API for server-side data source access and processing from the Java programming language.

JDBC API

Note: API is a template or model or specification or documentation. Once an API is released then anybody can provide the implementation. API is not a software that cannot be used directly for development. We have to use the implementation of API for the development of any application. This implementation of API is called Software.

We have the following two types of API :

  1. Public API: Public API means anybody can implement it.
  2. Proprietary API: Proprietary API means the particular company that released this API can implement it.
What does API contain?

API contains a collection of classes and interfaces in the case of java related APIs but any C or C++-related APIs contain a collection of library functions. Java people have released JSP API as a public API so that anybody can implement this API and already companies like IBM, BEA, SUN, etc. are implementing this JSE API.

Understanding java.sql package

This package provides the APIs for accessing and processing data that is stored in the database especially relational databases by using the java programming language. It includes a framework where different drivers can be installed dynamically to access different databases, especially relational databases.

This java.sql package contains API for the following:

  1. Making a connection with a database with the help of DriverManager class
  2. Sending SQL parameters to a database
  3. Updating and retrieving the results of a query
  4. Providing Standard mappings for SQL types to classes and interfaces in Java programming language.
  5. Metadata
  6. Exceptions
  7. Custom mapping an SQL user-defined type (UDT) to a class in the java programming language.
Driver Manager:

Driver Manager is the database connection provider to the JDBC client. JDBC client requests a Driver Manager for the database connection. Driver Manager is not the database connection creator. It receives the connection request from the java application(JDBC CLIENT)and passes that connection request to JDBC Driver.

JDBC Driver creates the connection and passes that connection to the JDBC client via Driver Manager. Once the java application connects to the database server, Driver Manager rolls the end that is Driver Manager doesn’t participate in the database connection.

Let’s get into the details of the JDBC Driver Manager.

The JDBC Driver Manager is a very important class that defines objects which connect Java applications to a JDBC driver. Usually, the Driver Manager is the backbone of the JDBC architecture. It’s very simple and small that is used to provide a means of managing the different types of JDBC database drivers running on an application. The main responsibility of the JDBC database is to load all the drivers found in the system properly as well as to select the most appropriate driver from opening to a database. The Driver Manager also helps to select the most appropriate driver from the previously loaded drivers when a new open database is connected.

The Driver Manager class works between the user and the drivers. The task of the Driver Manager class is to keep track of the drivers that are available and handles establishing a connection between a database and the appropriate driver. It even keeps track of the driver login time limits and printing of log and tracing messages. This class is mainly useful for the simple application, the most frequently used method of this class is DriverManager.getConnection(). We can know by the name of the method that this method establishes a connection to a database.

The Driver Manager class maintains the list of the Driver classes. Each driver has to be get registered in the DriverManager class by calling the method DriverManager.registerDriver().

By calling the Class.forName() method the driver class gets automatically loaded. The driver is loaded by calling the Class.forName() method. JDBC drivers are designed to tell the DriverManager about themselves automatically when their driver implementation class gets loads.

JDBC Driver Manager

This class has many methods. Some of the commonly used methods are given below:

  1. deregisterDriver(Driver driver): It drops the driver from the list of drivers registered in the DriverManager class.
  2. registerDriver(Driver driver): It registers the driver with the DriverManager class.
  3. getConnection(String url): It tries to establish the connection to a given database URL.
  4. getConnection(String url, String user, String password): It tries to establish the connection to a given database URL.
  5. getConnection(String url, Properties info): It tries to establish the connection to a given database URL.
  6. getDriver(String url): It attempts to locate the driver by the given string.
  7. getDrivers(): It retrieves the enumeration of the drivers who have been registered with the DriverManager class.
JDBC Driver:

JDBC driver is a translation software written in java language according to JDBC specifications. JDBC drivers perform the following duties

  1. Establishing the database connection for the JDBC client with the database server.
  2. Receiving the JDBC method calls from the JDBC client, translating it into DBMS understandable format, and passing on the same to the database server.
  3. Receiving the response from the DBMS, translating it into java format, and passing on the same to the java client.

JDBC Driver

JDBC compliant drivers are used in java enterprise applications for back-end communication. There is 180+ drivers but all these drivers could be classified into the following four types :

  1. JDBC-ODBC bridge driver (TYPE-1)
  2. Native API, partly java driver (TYPE-2)
  3. Net protocol, All java driver(TYPE-3)
  4. Native protocol, All java driver (TYPE-4)

Note: In the next article, we will discuss each of the above drivers in detail with their own advantages and disadvantages.

Database Server:

In almost all the cases in a java enterprise application RDBMS implementation is used as a database server. For example. Oracle, MySQL, SQL Server, Sybase, DB2, Informix, etc

In the next article, I am going to discuss the JDBC Drivers in detail. Here, in this article, I try to explain JDBC Architecture in detail. I hope you enjoy this JDBC Architecture article.

Leave a Reply

Your email address will not be published. Required fields are marked *