How JDBC fits in between two and three-tier systems

How JDBC fits in between two and three-tier systems

[28]

Two tier architecture

This Two tier architecture is similar to a basic client-server model. The application at the client end directly communicates with the database at the server side. API’s like ODBC,JDBC are used for this interaction. The server side is responsible for providing query processing and transaction management functionalities.

Three Tier architecture

In this type, there is another layer between the client and the server. Here, the client does not directly communicate with the server. Instead, it interacts with an application server which further communicates with the database system and then the query processing and transaction management takes place. This intermediate layer acts as a medium for exchange of partially processed data between server and client. This type of architecture is used in case of large web applications.

JDBC Architecture

A Java application cannot directly communicate with database to submit and retrieve the result of any query. This is because a database can interpret only SQL statements and not a Java language statement. For this reason we need a mechanism to translate the Java statements into SQL statements. The JDBC Architecture provides a mechanism for this kind of translation.

Classification of JDBC architecture

JDBC Application layer

Signifies a Java application that uses the JDBC API to interact with the JDBC drivers. A JDBC driver is a software that a Java application uses to access a database. The JDBC Driver manager of JDBC API connects the Java application to the driver.

JDBC Driver Layer

It acts as an interface between a Java application and a database. This layer contains a driver, such as a SQL server driver or an Oracle driver, which enables connectivity to database. A driver sends the request from the Java application to the database. After processing the request, the database sends the response back to the driver. The driver translates and sends the response to the JDBC API, and the JDBC API forwards it to the Java application.

JDBC Drivers

When we develop JDBC applications, we need to use JDBC drivers to convert queries into a form that a particular database can interpret. The JDBC Driver also retrieves the result of SQL statements and convert the result into equivalent JDBC API class object that the Java application uses as the JDBC only takes care of interactions with the database, any change made to database does not affect the application.

JDBC supports four types of drivers

  1. JDBC-ODBC Bridge driver

  2. Native API Partly-Java driver

  3. JDBC-Net-Pure-Java driver

  4. Native protocol Pure Java driver

JDBC-ODBC Bridge driver

The JDBC-ODBC Bridge driver is also called type-1 driver. The type-1 driver converts JDBC calls to Open Database Connectivity (ODBC) calls. ODBC is an open standard API to communicate with the databases. It enables a Java application to use any database that supports ODBC Driver. A Java application cannot interact directly with the ODBC driver for this reason, the application uses the type-1 driver that works as an interface between the application and the ODBC driver. To use the type-1 driver we need to have the ODBC driver installed on the client computer. It is usually used in stand-alone applications.

JDBC API

We need to use database drivers and JDBC API while developing a Java application to retrieve or store data in database. The JDBC API classes and interfaces are available in java.sql package or javax.sql package. The classes and interfaces perform a number of tasks, such as establish and close a connection with database, send a request to database, retrieve the data and update data in the database.

Commonly used classes and interfaces in the JDBC API are

  1. DriverManager class: Loads the driver for database.

  2. Driver interface: Represents database driver. All JDBC driver classes must implement the Driver interface.

  3. Connection interface: It enable to established connection between a Java application and a database.

  4. Statement interface: It executes SQL statements.

  5. ResultSet interface: Represents the information retrieved from the database.

  6. SQLException class: Provides information about the exceptions that occur while interacting with databases.