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.
- JDBC Architecture
- Components of JDBC
- JDBC client
- JDBC API
- What does API contain?
- Understanding java.sql package
- Driver Manager
- JDBC Driver
- Database Server
In order to understand the JDBC Architecture, please have a look at the following image.
Components of JDBC
JDBC architecture is a client-server architecture. JDBC architecture has 5 elements.
- JDBC client: Here JDBC Client is nothing but a java application that wants to communicate with the database.,
- 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.
- 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.
- 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.
- 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.
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.
So the java program is the client. JDBC CLIENT has the following responsibility while communicating with the database.
- Requesting for the database connection.
- Submitting the appropriate SQL statements to the DBMS.
- Dealing with the exception
- Making transactions whenever required.
- Closing the connection.
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.
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 :
- Public API: Public API means anybody can implement it.
- 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:
- Making a connection with a database with the help of DriverManager class
- Sending SQL parameters to a database
- Updating and retrieving the results of a query
- Providing Standard mappings for SQL types to classes and interfaces in Java programming language.
- Custom mapping an SQL user-defined type (UDT) to a class in the java programming language.
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.
This class has many methods. Some of the commonly used methods are given below:
- deregisterDriver(Driver driver): It drops the driver from the list of drivers registered in the DriverManager class.
- registerDriver(Driver driver): It registers the driver with the DriverManager class.
- getConnection(String url): It tries to establish the connection to a given database URL.
- getConnection(String url, String user, String password): It tries to establish the connection to a given database URL.
- getConnection(String url, Properties info): It tries to establish the connection to a given database URL.
- getDriver(String url): It attempts to locate the driver by the given string.
- getDrivers(): It retrieves the enumeration of the drivers who have been registered with the DriverManager class.
JDBC driver is a translation software written in java language according to JDBC specifications. JDBC drivers perform the following duties
- Establishing the database connection for the JDBC client with the database server.
- Receiving the JDBC method calls from the JDBC client, translating it into DBMS understandable format, and passing on the same to the database server.
- Receiving the response from the DBMS, translating it into java format, and passing on the same to the java client.
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 :
- JDBC-ODBC bridge driver (TYPE-1)
- Native API, partly java driver (TYPE-2)
- Net protocol, All java driver(TYPE-3)
- 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.
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.
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.