JDBC | Introduction to Java Database Connectivity

Mohit Lalwani
6 min readMay 10, 2021

Java is one of the powerful languages used in the IT industry to develop a number of software projects. To be a successful Java programmer, a developer needs to understand one of the important concepts of Java i.e. JDBC — Java Database Connectivity or JDBC.

What is JDBC?

JDBC stands for Java Database Connectivity. It is a step by step procedure to communicate with the database from the Java application in order to perform database operation. Basically, JDBC is an Application Programming Interface (API) that makes it possible to standardize and simplify the process of connecting Java applications to a database.

DBC is considered to be a part of Java platform, Standard Edition (Java SE) uses the Structured Query Languages (SQL) for performing different database queries like access, storage, update or delete.

Relational Database Management System (RDBMS) supports Structured Query Language (SQL). Since Java is a platform independent programming language which runs on most platforms, JDBC helps to write a single database application which can run on different platforms and interact with different Database Management Systems.

What are the different types of JDBC drivers?

As we have understood, JDBC is used to connect Java applications with the database. We should also know that JDBC uses different JDBC drivers to perform this task. Basically, a JDBC driver is a software component which enables Java applications to interact with the database.

There are 4 different types of JDBC drivers. These are:

  • JDBC-ODBC Bridge Driver
  • Native-API Driver (partially Java driver)
  • Network Protocol Driver (fully Java driver)
  • Thin Driver (fully Java driver).

Let us discuss each of these drivers independently:

  • JDBC-ODBC Bridge Driver/Type-1 Driver: There are some databases which provide ODBC driver to connect their databases. Here, ODBC stands for Open Database Connectivity.

A Java application which needs to communicate with a database containing ODBC driver will use JDBC API to communicate with JDBC-ODBC Bridge driver. Here, this JDBC-ODBC bridge driver will communicate with the ODBC driver and then further establish a connection with its specific database.

Advantage: 1)It helps to easily connect with the database.

Disadvantage: 1)Performance decreases as JDBC method call needs to be converted into ODBC function call.

  • Native-API driver/Type-2 Driver: Some database vendors provide only Native APIs, written in C/C++ programming languages, in order to access their database.

A Java application which wants to communicate with such databases which provide only Native APIs uses Native API driver to establish a communication. Java application is programmed using JDBC API, makes JDBC calls. Then, these JDBC calls are converted into database specific native calls using Native-API driver. Further, these native calls are passed over to database specific native library which communicates with the database.

Advantage: 1) Performance is upgraded compared to JDBC-OBDC/Type 1 driver.

Disadvantage: 2) Need to install Native API driver on each of the client machine.

  • Network Protocol Driver/Type-3 Driver: The Network Protocol Driver uses middleware to convert JDBC calls directly or indirectly into vendor-specific database protocol.

Advantage: 1) No client libraries required to install on client machines.

Disadvantage: 1) Performance is slower as the number of network calls to an intermediate middleware server.

  • Thin Driver/Type-4 Driver: Thin Driver directly communicates with the database. This driver doesn’t require any native database library or middleware server to communicate with the database. Here, the thin driver converts JDBC calls made by Java application into vendor-specific database protocol.

Advantage: It doesn’t require any software at client side or server side.

Disadvantage: It is a database dependent driver.

How JDBC works

Let’s quickly look at how JDBC works. JDBC establishes a connection with a data source, sending queries, updating statements and processing results. JDBC helps developers with the following aspects:

  • It helps to establish a connection with a data source
  • It allows to send queries and updates statements
  • It helps to fetch the data from the database and process the fetched results.

Here, the Java application calls the JDBC to submit the SQL statements queries and get the results. The JDBC driver which constitutes a set of classes helps to implement the JDBC API. The database stores all the data that is retrieved by the JDBC driver.

Why JDBC is needed?

It is important to understand why we need Java Database connectivity. Java applications are required to connect with database. Java applications are written in Java programming language, but the database only understands Structured Query Language (SQL). In order to establish a connection between Java application and database, JDBC is used. JDBC contains a set of interface and classes which helps to connect Java application to the database.

JDBC components:

Let us discuss 6 main components of JDBC:

  1. Driver Manager: Driver Manager is a class which manages all the database drivers. The Driver Manager is used to load the specific database drivers in an application to establish a connection with the database.
  2. Driver: Driver is the interface which manages the communications happening between the application and the database server.
  3. Connection: Connection is an interface which contains methods for contacting the database.
  4. Statement: Statement is an interface that creates an object to submit SQL queries or statements to the database.
  5. Result Set: Result Set contains the results that are retrieved from the database after the execution of SQL statements or queries.
  6. SQL Exception: SQL Exception class is used to handle any of the errors that occur in a database application.

Which Database is best suited for Java?

As there are number of databases available which help to develop software projects on Java, it is difficult to say which is the best database among all of them. There are various factors involved while choosing a right database to develop a software application. For example, you have to consider whether you are looking for an open source database or a paid version of database, or if you want to use a cloud based database matters.

Some of the databases which can be used for Java are:

  1. Oracle
  2. MySQL
  3. PostgreSQL
  4. IBM-DB2
  5. MS-SQL

Advantages of Java Database Connectivity:

Java Database Connectivity comes up with several advantages, some of them are:

  1. JDBC itself creates XML format of data from the database automatically
  2. JDBC supports modules
  3. JDBC provides better security
  4. JDBC completely supports query and stored procedure
  5. JDBC supports both types of processing i.e. Synchronous and Asynchronous processing
  6. JDBC does not require content conversion.

Conclusion:

JDBC is a very important concept which every Java developer needs to understand to develop projects. We hope this blog helps our reader to understand various concepts related to JDBC.

--

--