Java JDBC Tutorial: What Is JDBC (Java Database Connectivity)

Starting with the basics, this JDBC tutorial explains components, architecture and types of drivers in Java Database Connectivity (JDBC):

This tutorial explains what is JDBC, its versions released till now, what are the pre-requisites needed before proceeding with JDBC, types of Drivers, etc. At the end of this tutorial, you will know basics of Java Database Connectivity or JDBC and where can we use it.

Let’s start this tutorial with basics of JDBC.

 JDBC Tutorial

List Of Java JDBC Tutorials

Tutorial #1: What Is JDBC (Java Database Connectivity) (This Tutorial)
Tutorial #2: Java JDBC Connection Tutorial With Programming Example 
Tutorial #3: JDBC DriverManager, JDBC PreparedStatement And Statement
Tutorial #4: JDBC ResultSet: How To Use Java ResultSet To Retrieve Data
Tutorial #5: Java JDBC Transaction Management With Example 
Tutorial #6: JDBC Exception Handling – How To Handle SQL Exceptions
Tutorial #7: JDBC Batch Processing And Stored Procedure Tutorial 
Tutorial #8: Top 25+ JDBC Interview Questions And Answers


What Is JDBC

In most of the Java applications, there is always a need to interact with databases to retrieve, manipulate, and process the data. For this purpose, Java JDBC has been introduced.

JDBC is the commonly used short form for Java Database Connectivity. By using JDBC, we can interact with different types of Relational Databases such as Oracle, MySQL, MS Access, etc.

Before JDBC, ODBC API was introduced to connect and perform operations with the database. ODBC uses an ODBC driver which is platform-dependent because it was written in C programming language. JDBC API is written in Java language, is platform-independent and makes Java platform-independent itself.

Below diagram shows the basic flow of JDBC:

Basic Flow of JDBC

Versions Of JDBC

Initially, Sun Microsystems had released JDBC in JDK 1.1 on Feb 19, 1997. After that, it has been part of the Java Platform.

The following table contains JDBC versions and implementations:

JDBC VersionJDK ImplementationYear
JDBC 1.2JDK 1.11997
JDBC 2.1JDK 1.21999
JDBC 3.0JDK 1.42001
JDBC 4.0Java SE 62006
JDBC 4.1Java SE 72011
JDBC 4.2Java SE 82014
JDBC 4.3Java SE 92017

Prerequisite

Before going ahead with the JDBC tutorial, you need to have good knowledge about Java so you can use JDBC in Java programs.

=>Recommended Reading: Java Tutorials

Prerequisites to implement JDBC in Java Applications are as follows:

1. Java should be installed in the system where you want to create Java application and use JDBC.

Refer to this link for Java installation. 

2. You should have proper JDBC driver jar files to connect with DBMS. Each database has different JDBC driver jar files.

You can download these files for the specific database using the links in the following table:

Database with Download LinkJDBC Driver Provider NameJAR File Name
MySQLOracle Corporationmysql-connector-java-VERSION.jar
OracleOracle Corporationojdbc8.jar
SQL ServerMicrosoft Corporationsqljdbc41.jar, sqljdbc42.jar
Postgre SQLPostgreSQL Global Development Grouppostgresql-VERSION.jar
SQLiteXerial.orgsqlite-jdbc-VERSION.jar
MS AccessUCanAccess.comucanaccess-VERSION.jar

Driver Types In JDBC

Now, Let’s discuss the types of the driver in JDBC to help you choose the one suitable for your program.

All RDMS (Relational Database Management System) need a driver if they are to be accessed from outside of their system. So, JDBC Driver is required to execute SQL queries and get the result from the database.

There are 4 different types of Drivers available in JDBC. They are classified based on the technique which is used to access a Database.

They are as follows:

  1. Type I : JDBC- ODBC Bridge
  2. Type II: Native APT- Partly Java Driver
  3. Type III: Network Protocol- Fully Java Driver
  4. Type IV: Thin Driver- Fully Java Driver

Type I: JDBC- ODBC Bridge

In this type of Driver, JDBC – ODBC Bridge act as an interface between client and DB server. When a user uses Java application to send requests to the database using JDBC – ODBC Bridge, it first converts the JDBC API to ODBC API and then sends it to the database. When the result is received from DB, it is sent to ODBC API and then to JDBC API.

This Driver is platform-dependent because it uses ODBC which depends on the native library of the OS. In this Type, JDBC – ODBC driver should be installed in each client system and the database must support the ODBC driver.

When it is no matter about the installation in the client machine, we can use this driver. It is easy to use but it gives low performance due to the conversion of the JDBC method calls to the ODBC method calls.

Note: It is available in JDK 1.2

Java 8 doesn’t support this type of driver. Oracle recommends that the user makes use of the JDBC drivers provided by their Database vendors.

Type I: JDBC – ODBC Bridge Driver
Figure: Type I: JDBC – ODBC Bridge Driver

Type II: Native API – Partially Java Driver

It is similar to Type I Driver. Here, the ODBC part is replaced with native code in Type II Driver. This native code part is targeted at a specific database product. It uses the libraries of the client-side of the database. This Driver converts the JDBC method calls to native calls of the database native API.

When the database gets the requests from the user, the requests are processed and sent back with the results in the native format which are to be converted to JDBC format and give it to the Java application.

It was instantly adopted by the DB vendors because it was quick and inexpensive to implement. It is also in the native format of the DB.

This type of driver gives faster response and performance than the Type I driver.

Type II: Native APT - Partly Java Driver
Figure: Type II: Native APT – Partly Java Driver

Type III: Network Protocol

The type III driver is fully written in Java. It is like a 3-tier approach to access the database. It sends the JDBC method calls to an intermediate server. On behalf of the JDBC, the intermediate server communicates with the database. The Application server (intermediate or middle – tier) converts the JDBC calls either directly or indirectly to the vendor-specific Database protocol.

This approach does not increase the architecture efficiency and it is also costly, due to this most of the database vendors don’t prefer this driver. Since the application server is used, you need to have good knowledge about the application server to use this approach efficiently.

Type III : Network Protocol – Fully Java Driver
Figure: Type III: Network Protocol – Fully Java Driver

Type IV: Thin Driver

Type IV driver is directly implemented that converts JDBC calls directly into vendor-specific Database protocol. Today, most of the JDBC Drivers are type IV drivers. It is written fully in Java and thus it is platform-independent. It is installed inside the JVM (Java Virtual Machine) of the client, so you don’t have to install any software on the client or server-side. This driver architecture has all the logic to communicate directly with the DB in a single driver.

It provides better performance than the other type of drivers. It allows for easy deployment. Nowadays, this type of driver is developed by the database vendor itself so that programmers can use it directly without dependence on other sources.

Type IV : Thin Driver - Fully Java Driver
Figure: Type IV: Thin Driver – Fully Java Driver

Applications Of JDBC

JDBC can be used in Java applications, Applets, Servlets, or any other Java programs where you want to connect with DB.

For Example, Standalone Applications, Websites, Banking Applications, etc.,

Architecture And Components Of JDBC

JDBC Architecture: It supports two types of processing models to access the DB.

These are:

  1. Two-tier Architecture
  2. Three-tier Architecture

#1) Two-tier Architecture:

It helps Java application to directly connect with the database. It needs a JDBC driver for the communication with a particular DB. The user sends the requests to DB and receives the response directly without any mediator except JDBC Driver. The database, either in the same machine or on a remote machine is connected via a network. It can be called as a client-server architecture.

Two-tier Architecture

#2) Three-tier Architecture:

It is the opposite of two-tier architecture. There is no direct communication between the user and the database. The user sends the request to the middle tier (Application Server) from which the request is again sent to Database. Then the database processes the request and sends the result to the middle-tier from which the user receives the result/ response.

It simplifies deployment and management. Management Information System (MIS) directors use this architecture as it makes it simple to maintain access control and updates to corporate data.

Three-tier Architecture

Three-tier Architecture

Components of JDBC

There are 4 main components available in JDBC. They are:

  1. JDBC API
  2. JDBC Driver Manager
  3. JDBC Test Suite
  4. JDBC – ODBC Bridge

#1) JDBC API: It provides access to relational databases from any Java program. The JDBC API has a set of classes and interfaces which are written in Java that gives a standard tool/ API for developers. Using JDBC API, you can create and execute SQL queries, receive the result, and perform changes to the data and save the results back to the database.

It can interact with multiple databases like Oracle, MySQL, MS Access from a single Java program. With JDBC API, it is not necessary to write one program to access Oracle DB, another program to access MySQL, another program to access MS Access, and so on.

JDBC API is a part of Java Platform which has both Java Standard Edition (Java SE) and Java Enterprise Edition (Java EE).

The JDBC 4.0 API has 2 packages.

  • Java.sql
  • Javax.sql

Both packages are available in Java SE and Java EE.

#2) JDBC Driver Manager: The traditional management layer of JDBC is Driver Manager and it acts as an interface between the user and the drivers. It keeps the tracking details of the drivers that are available and establish a connection between the database and the appropriate driver. It defines the objects which can connect Java app to JDBC Driver. Thus, it is the backbone of the JDBC API.

We will discuss the Driver Manager in the next tutorial.

#3) JDBC Test Suite: It helps to find whether the JDBC Drivers will run the program or not. It provides the confidence and conformance that the program will run by JDBC Drivers.

#4) JDBC- ODBC Bridge: This makes use of ODBC drivers as JDBC Drivers. It is similar to the TYPE I driver which is already covered in the driver types section in this tutorial.

Key points to be noted:

  • JDBC is used to interact with the database from any Java program such as Java application, Applets, Servlets.
  • The latest version of JDBC is JDBC 4.3 It is stable release since 21st Sept 2017.
  • Type I driver: JDBC ODBC Bridge – easy to use but ODBC is platform dependent. It is also a low-performance driver.
  • Type II driver: Native API – Native code part replaced the ODBC part in Type I driver and targeted at a specific database product. It is faster than Type I Driver.
  • Type III driver: Middle Tier or Application Server is used as an interface between JDBC Driver and database. Knowledge of Application Server is needed to use it effectively.
  • Type IV driver: JDBC Driver immediately communicates with the database. Database vendors are using this type of driver and providing it to customers or developers.
  • JDBC can be used in any program which is written in Java language.

Conclusion

In this JDBC tutorial, we have learned about the basics of JDBC and the pre-requisites needed before proceeding with JDBC. We have seen its versions and 4 types of drivers which help the developers decide the type of driver to use in programs.

We also discussed the architecture of JDBC and its 4 core components.

NEXT Tutorial>>