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 the basics of Java Database Connectivity or JDBC and where can we use it.
Let’s start this tutorial with the basics of JDBC.
Table of Contents:
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 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.
Also read =>> How to use MySQL connector (with examples)
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 the 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:
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 Version | JDK Implementation | Year |
---|---|---|
JDBC 1.2 | JDK 1.1 | 1997 |
JDBC 2.1 | JDK 1.2 | 1999 |
JDBC 3.0 | JDK 1.4 | 2001 |
JDBC 4.0 | Java SE 6 | 2006 |
JDBC 4.1 | Java SE 7 | 2011 |
JDBC 4.2 | Java SE 8 | 2014 |
JDBC 4.3 | Java SE 9 | 2017 |
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 Link | JDBC Driver Provider Name | JAR File Name |
---|---|---|
MySQL | Oracle Corporation | mysql-connector-java-VERSION.jar |
Oracle | Oracle Corporation | ojdbc8.jar |
SQL Server | Microsoft Corporation | sqljdbc41.jar, sqljdbc42.jar |
Postgre SQL | PostgreSQL Global Development Group | postgresql-VERSION.jar |
SQLite | Xerial.org | sqlite-jdbc-VERSION.jar |
MS Access | UCanAccess.com | ucanaccess-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:
- Type I : JDBC- ODBC Bridge
- Type II: Native APT- Partly Java Driver
- Type III: Network Protocol- Fully Java Driver
- 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.
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.
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.
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.
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:
- Two-tier Architecture
- 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.
#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
Components of JDBC
There are 4 main components available in JDBC. They are:
- JDBC API
- JDBC Driver Manager
- JDBC Test Suite
- 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.