Purpose
Consider a situation where you are working on a java application and you continuously need to read and write data from a file or more precisely a database. One way to do this is to write the code every time we wish to perform the task. This will be a very inefficient method and there will be high chances of error. The second option is that we make use of the already existing RDBMS softwares like MySQL, Oracle, SQL server, etc. We leave the responsibility of dealing with the data to such databases.
Since we are working on a java application, our code is in java. The database software may not be a java application. So how are we going to read and write the data from the Java application? The solution is JDBC. Actually the JDBC contains various classes and interfaces. In simple words it contains programs that help us communicate with databases. Once the connection is made, we can manipulate the data in the database using java.
Example: You must have seen that the bank employees are able to get all the transactions you have made by simply clicking on the transactions button on the software they use. Your list of transactions are stored in a database. However the bank employee does not write a SQL query to fetch the information. Whenever he clicks on the transaction button, with the help of JDBC, a query is generated that fetches the result from the database.
Components Of JDBC
There are four main components of JDBC that are used for interacting with the Database.
JDBC API
It is one of the most important components of the Java Database Connectivity (JDBC). It can be thought of as a collection of Java classes and interfaces. It contains usable code that makes the task of connecting with the database much easier. It contains two popular packages.
JDBC DriverManager
The JDBC Driver Manager manages a list of database drivers.
- It matches connection requests from the java application with the proper database driver using communication sub-protocol.
- The first driver that recognises a certain subprotocol under the JDBC will be used to establish a database connection.
JDBC Test Suite
The main use of the JDBC test suite is to test the operations such as insertion, deletion, updating being performed by JDBC Drivers.
JDBC ODBC Bridge Driver
The JDBC-ODBC Bridge facilitates applications written in the Java programming language to use the JDBC API with many existing ODBC drivers. The main function of this driver is to connect the database drivers to the database. As per Oracle, it has been removed since JDK 8.
JDBC Architecture
The entire JDBC architecture is best depicted by the picture below:

The architecture of JDBC consists of:
Java Applications
It can be either a Java applet or a servlet that has to perform certain data-related tasks. For doing so, it must be able to communicate with the data present in the database.
JDBC API
It contains various classes and interfaces that help us to execute SQL queries. Some of the highly popular JDBC API classes are:
- DriverManager class
- Clob class
- Blob class
- Types class
Similarly, some popular JDBC API interfaces are:
- Driver interface
- Connection interface
- Statement interface
- PreparedStatement
- CallableStatement interface
- ResultSet interface
- ResultSetMetaData interface
- DatabaseMetaData interface
- RowSet interface
JDBC DriverManager
It is a very crucial component of the JDBC. The main work of the DriverManager is to load a specific driver for a database as various databases may have a different driver. The driver manager ensures that the correct driver is chosen for the database we are interacting with and is returned to the application.
JDBC Drivers
It is one of the most crucial parts of the entire JDBC. A JDBC Driver is a software component that enables a Java application to interact with a database.
The important functionalities of the JDBC driver are:
- It handles the communications with the database server.
- Instead of directly creating a Driver object, we use the DriverManager object to deal with the drivers.
- It also abstracts the details associated with working with the Driver objects.
Working Of JDBC
The Java Database Connectivity(JDBC) helps in making the connection between the database and the java application. After that, we are able to perform SQL-related tasks. Let us analyse each and every process involved in the working of the JDBC.
Loading a Driver
A program can load the JDBC Drivers at any time explicitly. Whenever the connection is to be made, the driver is loaded.
Example: loading the Oracle Driver.
Class.forName(“oracle.jdbc.driver.OracleDriver”);
The above function is used to load the driver class. The name of the driver class has to be passed as a String.
Establishing Connection
- When getConnection() is called, the DriverManager will attempt to locate a suitable driver from amongst those loaded at initialisation and those loaded explicitly.
- Syntax: DriverManager.getConnection(url, user, password)
- The getConnection() method of the DriverManager class is used to make a connection with the database url.
- url: unique database url.
- user: database user name.
- password: database password.
- The getConnection() method returns Connection Object if the connection is successful, otherwise null.
Creating Statement Object
Whenever a connection is established, then we can interact with the database. The connection interface defines methods for interacting with the database. It instantiates a Statement by using the createStatement() method.
Example:
Statement st = con.createStatement();
You can also use online java compiler for compile and run the code for good practice.
Executing Statements
To execute the statement query, there are three popular methods:
- execute(): returns boolean value.
- executeQuery(): returns the result of the query in the form of the ResultSet object.
- executeUpdate(): returns the number of rows affected by the execution of the query.
Getting Results
ResultSet object can be obtained as a returned object by executeQuery() method of the Prepared Statement. This object can be used to extract the row's values. It's next() method sets the pointer to the first row, and getX() methods can be used to get different types of data from the result set.
Example:
while( rs.next() ) {
String data = rs.getString(1);
System.out.println( data );
}
Closing Database Connection
The connection object has a method called close() to close the connection.
Example:
con_obj.close()
JDBC Example
import java.sql.*;
public class JDBCExample {
public static void main(String a[]) {
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "CodingNinjas";
String driver = "com.mysql.jdbc.Driver";
String userName = "Ankit";
String password = "no";
try {
Class.forName(driver).newInstance();
// establish connection
Connection conn_obj = DriverManager.getConnection(url + dbName, userName, password);
// create a statement
Statement st = conn_obj.createStatement();
//result is returned as ResultSet
ResultSet rs = st.executeQuery("SELECT * from Ninjas");
System.out.println("Results");
// iterate and print data
while (rs.next()) {
String data = rs.getString(1);
System.out.println(data);
}
st.close();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}

You can also try this code with Online Java Compiler
Run Code
Also see, Hashcode Method in Java
Frequently Asked Questions
Why do we need JDBC?
The JDBC helps us to establish a connection with the data source (database) and perform various SQL-related tasks.
Name a few interfaces in JDBC.
Some popular interfaces are the Driver interface, Statement interface, Connection interface, ResultSet interface, etc.
Which classes are available in JDBC API?
DriverManager class, Blob class, Clob class, Types class.
What is the importance of JDBC DriverManager?
The main work of the DriverManager is to load a specific driver for a database as various databases may have a different driver.
Conclusion
The Java Database Connectivity (JDBC) is a Java API (Application Programming Interface) that is responsible for connecting the Java applications with the database. We can perform many SQL operations in the java application with the help of JDBC. There are four main components of JDBC that are used for interacting with the database, viz. JDBC API, JDBC DriverManager, Test suite, ODBC Bridge Driver. The DriverManager ensures that the correct driver is chosen for the database we are interacting with and is returned to the application. When getConnection() is called, the DriverManager will attempt to locate a suitable driver from amongst those loaded at initialisation and those loaded explicitly.
You can also check out our other blogs on Code360.