The Wayback Machine - https://web.archive.org/web/20241001083109/https://www.geeksforgeeks.org/introduction-to-jdbc/
Open In App

Introduction to JDBC (Java Database Connectivity)

Last Updated : 10 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and execute the query with the database. It is a specification from Sun Microsystems that provides a standard abstraction(API or Protocol) for Java applications to communicate with various databases. It provides the language with Java database connectivity standards. It is used to write programs required to access databases. JDBC, along with the database driver, can access databases and spreadsheets. The enterprise data stored in a relational database(RDB) can be accessed with the help of JDBC APIs.

Definition of JDBC(Java Database Connectivity) 

JDBC is an API(Application programming interface) used in Java programming to interact with databases. The classes and interfaces of JDBC allow the application to send requests made by users to the specified database. The current version of JDBC is JDBC 4.3, released on 21st September 2017.

Purpose of JDBC 

Enterprise applications created using the JAVA EE technology need to interact with databases to store application-specific information. So, interacting with a database requires efficient database connectivity, which can be achieved by using the ODBC(Open database connectivity) driver. This driver is used with JDBC to interact or communicate with various kinds of databases such as Oracle, MS Access, Mysql, and SQL server database.

Components of JDBC 

There are generally four main components of JDBC through which it can interact with a database. They are as mentioned below: 

1. JDBC API: It provides various methods and interfaces for easy communication with the database. It provides two packages as follows, which contain the java SE and Java EE platforms to exhibit WORA(write once run anywhere) capabilities. The java.sql package contains interfaces and classes of JDBC API.

java.sql: This package provides APIs for data access and data process in a relational database, included in 
Java Standard Edition (java SE)
javax.sql: This package extends the functionality of java package by providing datasource interface for
establishing connection pooling, statement pooling with a data source, included in
Java Enterprise Edition (java EE)

It also provides a standard to connect a database to a client application.

2. JDBC Driver manager: It loads a database-specific driver in an application to establish a connection with a database. It is used to make a database-specific call to the database to process the user request.

3. JDBC Test suite: It is used to test the operation(such as insertion, deletion, updation) being performed by JDBC Drivers.

4. JDBC-ODBC Bridge Drivers: It connects database drivers to the database. This bridge translates the JDBC method call to the ODBC function call. It makes use of the sun.jdbc.odbc package which includes a native library to access ODBC characteristics.

Architecture of JDBC

Architecture of JDBC

Description: 

  1. Application: It is a java applet or a servlet that communicates with a data source.
  2. The JDBC API: The JDBC API allows Java programs to execute SQL statements and retrieve results. Some of the important interfaces defined in JDBC API are as follows: Driver interface , ResultSet Interface , RowSet Interface , PreparedStatement interface, Connection inteface, and cClasses defined in JDBC API are as follows: DriverManager class, Types class, Blob class, clob class.
  3. DriverManager: It plays an important role in the JDBC architecture. It uses some database-specific drivers to effectively connect enterprise applications to databases.
  4. JDBC drivers: To communicate with a data source through JDBC, you need a JDBC driver that intelligently communicates with the respective data source.

Types of JDBC Architecture(2-tier and 3-tier) 

The JDBC architecture consists of two-tier and three-tier processing models to access a database. They are as described below:  

  1. Two-tier model: A java application communicates directly to the data source. The JDBC driver enables the communication between the application and the data source. When a user sends a query to the data source, the answers for those queries are sent back to the user in the form of results. 
    The data source can be located on a different machine on a network to which a user is connected. This is known as a client/server configuration, where the user’s machine acts as a client, and the machine has the data source running acts as the server.
     
  2. Three-tier model: In this, the user’s queries are sent to middle-tier services, from which the commands are again sent to the data source. The results are sent back to the middle tier, and from there to the user. 
    This type of model is found very useful by management information system directors.

What is API?

Before jumping into JDBC Drivers, let us know more about API.

API stands for Application Programming Interface. It is essentially a set of rules and protocols which transfers data between different software applications and allow different software applications to communicate with each other. Through an API one application can request information or perform a function from another application without having direct access to it’s underlying code or the application data.

JDBC API uses JDBC Drivers to connect with the database.

JDBC Drivers

JDBC drivers are client-side adapters (installed on the client machine, not on the server) that convert requests from Java programs to a protocol that the DBMS can understand. There are 4 types of JDBC drivers:

  1. Type-1 driver or JDBC-ODBC bridge driver
  2. Type-2 driver or Native-API driver (partially java driver)
  3. Type-3 driver or Network Protocol driver (fully java driver)
  4. Type-4 driver or Thin driver (fully java driver)

Interfaces of JDBC API

A list of popular interfaces of JDBC API is given below:

  • Driver interface
  • Connection interface
  • Statement interface
  • PreparedStatement interface
  • CallableStatement interface
  • ResultSet interface
  • ResultSetMetaData interface
  • DatabaseMetaData interface
  • RowSet interface

Classes of JDBC API

A list of popular classes of JDBC API is given below:

  • DriverManager class
  • Blob class
  • Clob class
  • Types class

Working of JDBC 

Java application that needs to communicate with the database has to be programmed using JDBC API. JDBC Driver supporting data sources such as Oracle and SQL server has to be added in java application for JDBC support which can be done dynamically at run time. This JDBC driver intelligently communicates the respective data source. 

Creating a simple JDBC application:  

Java
//Java program to implement a simple JDBC application
package com.vinayak.jdbc;

import java.sql.*;

public class JDBCDemo {
  
    public static void main(String args[])
        throws SQLException, ClassNotFoundException
    {
        String driverClassName
            = "sun.jdbc.odbc.JdbcOdbcDriver";
        String url = "jdbc:odbc:XE";
        String username = "scott";
        String password = "tiger";
        String query
            = "insert into students values(109, 'bhatt')";

        // Load driver class
        Class.forName(driverClassName);

        // Obtain a connection
        Connection con = DriverManager.getConnection(
            url, username, password);

        // Obtain a statement
        Statement st = con.createStatement();

        // Execute the query
        int count = st.executeUpdate(query);
        System.out.println(
            "number of rows affected by this query= "
            + count);

        // Closing the connection as per the
        // requirement with connection is completed
        con.close();
    }
} // class

The above example demonstrates the basic steps to access a database using JDBC. The application uses the JDBC-ODBC bridge driver to connect to the database. You must import java.sql package to provide basic SQL functionality and use the classes of the package. 

What is the need of JDBC?

JDBC is a Java database API used for making connection between java applications with various databases. Basically, JDBC used for establishing stable database connection with the application API. To execute and process relational database queries (SQL or Oracle queries), multiple application can connect to different types of databases which supports both standard (SE) and enterprise (EE) edition of java.



Previous Article
Next Article

Similar Reads

Spring Boot - Spring JDBC vs Spring Data JDBC
Spring JDBC Spring can perform JDBC operations by having connectivity with any one of jars of RDBMS like MySQL, Oracle, or SQL Server, etc., For example, if we are connecting with MySQL, then we need to connect "mysql-connector-java". Let us see how a pom.xml file of a maven project looks like. C/C++ Code <?xml version="1.0" encoding=
4 min read
Java Database Connectivity with MySQL
In Java, we can connect our Java application with the MySQL database through the Java code. JDBC ( Java Database Connectivity) is one of the standard APIs for database connectivity, using it we can easily run our query, statement, and also fetch data from the database. Prerequisite to understand Java Database Connectivity with MySQL 1. You should h
3 min read
How to Execute Multiple SQL Commands on a Database Simultaneously in JDBC?
Java Database Connectivity also is known as JDBC is an application programming interface in Java that is used to establish connectivity between a Java application and database. JDBC commands can be used to perform SQL operations from the Java application. Demonstrating execution of multiple SQL commands on a database simultaneously using the addBat
6 min read
Checking Internet Connectivity using Java
Checking Internet connectivity using Java can be done using 2 methods: 1) by using getRuntime() method of java Runtime class. 2) by using methods of java URL and URLConnection classes. #Java Runtime Class:This class is used to interact with java runtime environment (Java virtual machine) in which the application is running. It provides methods/func
2 min read
Difference between JDBC and Hibernate in Java
Java is one of the most powerful and popular server-side languages in the current scenario. One of the main features of a server-side language is the ability to communicate with the databases. In this article, let's understand the difference between two ways of connecting to the database (i.e.) JDBC and Hibernate. Before getting into the difference
2 min read
Java Program to Retrieve Contents of a Table Using JDBC connection
It can be of two types namely structural and non-structural database. The structural database is the one which can be stored in row and columns. A nonstructural database can not be stored in form of rows and columns for which new concepts are introduced which would not be discussing here. Most of the real-world data is non-structural like photos, v
5 min read
Java JDBC - Difference Between Row Set and Result Set
ResultSet characteristics are as follows: It maintains a connection to a database and because of that, it can’t be serialized.it can not pass the Result set object from one class to another class across the network.ResultSet object maintains a cursor pointing to its current row of data. Initially, the cursor is positioned before the first row. The
2 min read
Difference Between Connected vs Disconnected RowSet in Java JDBC
A RowSet is a wrapper around a ResultSet object. It can be connected, disconnected from the database, and can be serialized. It maintains a JavaBean component by setting the properties. You can pass a RowSet object over the network. By default, the RowSet object is scrollable and updatable. This diagram will give you more idea about ResultSet and R
5 min read
Working with Large Objects Using JDBC in Java
Sometimes as part of programming requirements, we have to insert and retrieve large files like images, video files, audio files, resumes, etc with respect to the database. Example: Uploading images on the matrimonial websiteUpload resume on job-related websites To store and retrieve large information we should go for Large Objects(LOBs). There are
5 min read
Establishing JDBC Connection in Java
Before Establishing JDBC Connection in Java (the front end i.e your Java Program and the back end i.e the database) we should learn what precisely a JDBC is and why it came into existence. Now let us discuss what exactly JDBC stands for and will ease out with the help of real-life illustration to get it working. What is JDBC? JDBC is an acronym for
6 min read
What is RowSet in Java JDBC?
RowSet is an interface in java that is present in the javax.sql package. Geek do note not to confuse RowSet with ResultSet. Note: RowSet is present in package javax.sql while ResultSet is present in package java.sql. The instance of RowSet is the java bean component because it has properties and a java bean notification mechanism. It is introduced
3 min read
How to pre populate database in Android using SQLite Database
Introduction : Often, there is a need to initiate an Android app with an already existing database. This is called prepopulating a database. In this article, we will see how to pre-populate database in Android using SQLite Database. The database used in this example can be downloaded as Demo Database. To prepopulate a SQLite database in an Android
7 min read
How to Commit a Query in JDBC?
COMMIT command is used to permanently save any transaction into the database. It is used to end your current transaction and make permanent all changes performed in the transaction. A transaction is a sequence of SQL statements that Oracle Database treats as a single unit. This statement also erases all save points in the transaction and releases t
5 min read
Simplifying CRUD Operation with JDBC
Creating, reading, updating, and deleting data in a database is a common task in many applications, and JDBC (Java Database Connectivity) is a Java API that allows you to connect to a database and perform these operations. In this blog post, we will walk through the steps of setting up a simple CRUD (create, read, update, delete) operation using JD
3 min read
How to Insert Records to a Table using JDBC Connection?
Before inserting contents in a table we need to connect our java application to our database. Java has its own API which JDBC API which uses JDBC drivers for database connections. Before JDBC, ODBC API was used but it was written in C which means it was platform-dependent. JDBC API provides the applications-to-JDBC connection and JDBC driver provid
4 min read
Delete Contents From Table Using JDBC
JDBC(Java Database Connectivity) is a standard API(application interface) between the java programming language and various databases like Oracle, SQL, PostgreSQL, etc. It connects the front end(for interacting with the users) with the backend(for storing data). Approach: 1. CREATE DATABASE: Create a database using sqlyog and create some tables in
2 min read
JDBC - Type 4 Driver
Let us do get a cover overview of the JDBC and the ODBC prior in order to better understand what exactly is type 4 driver. A JDBC driver enables Java application to interact with a database from where we can fetch or store data. JDBC drivers are analogous to ODBC drivers. The JDBC classes are contained in the Java Package java.sql and javax.sql.JDB
3 min read
JDBC - Type 2 Driver
A JDBC driver enables Java application to interact with a database from where we can fetch or store data. JDBC drivers are analogous to ODBC drivers. The JDBC classes are contained in the Java Package java.sql and javax.sql.JDBC helps to Connect to a data source, like a database.Send queries and update statements to the databaseRetrieve and process
2 min read
JDBC - Type 1 Driver
A JDBC driver enables Java application to interact with a database from where we can fetch or store data. JDBC drivers are analogous to ODBC drivers. The JDBC classes are contained in the Java Package java.sql and javax.sql.JDBC helps to Connect to a data source, like a database.Send queries and update statements to the databaseRetrieve and process
3 min read
JDBC - Type 3 Driver
A JDBC driver enables Java application to interact with a database from where we can fetch or store data. JDBC drivers are analogous to ODBC drivers. The JDBC classes are contained in the Java Package java.sql and javax.sql.JDBC helps to Connect to a data source, like a database.Send queries and update statements to the databaseRetrieve and process
2 min read
Inserting Records in Batch Using JDBC
It is carried out using the functions namely addBatch() and executeBatch() methods. For that lets us do have a prior understanding of JDBC drivers. So, in order to connect to your database in Java, you need a JDBC driver. Every database (MySQL, Oracle, etc.) comes with its own JDBC driver, usually built by the database vendor and found on the datab
2 min read
Spring - Prepared Statement JDBC Template
In Enterprise applications, data access/stored in relational databases is a common requirement. As an essential part of Java SE, Java Database Connectivity (JDBC) defines a set of standard APIs to access relational databases in a vendor-independent fashion. However, when using JDBC, the developer has to manage all database-related resources and han
5 min read
Servlet With JDBC
Servlets are the Java programs that run on the Java-enabled web server or application server. They are used to handle the request obtained from the webserver, process the request, produce the response, then send a response back to the webserver. Properties of Servlets are as follows: Servlets work on the server-side.Servlets are capable of handling
12 min read
Hibernate - Difference Between ORM and JDBC
Hibernate is a framework that is used to develop persistence logic that is independent of Database software. In JDBC to develop persistence logic, we deal with primitive types. Whereas Hibernate framework we use Objects to develop persistence logic that is independent of database software. ORM (Object-Relational Mapping) ORM, an abbreviation for Ob
4 min read
Spring JDBC Example
JDBC or Java Database Connectivity is a specification from Sun microsystems that provides a standard abstraction(that is API or Protocol) for java applications to communicate with various databases. It provides the language with java database connectivity standards. It is used to write programs required to access databases. JDBC along with the data
4 min read
Advantages of Spring Boot JDBC
Spring JDBC is used to build the Data Access Layer of enterprise applications, it enables developers to connect to the database and write SQL queries and update data to the relational database. The code related to the database has to be placed in DAO classes. SpringJDBC provides a class called JdbcTemplate to handle the database-related logic. Jdbc
3 min read
Spring - Using SQL Scripts with Spring JDBC + JPA + HSQLDB
We will be running the SQL scripts with Spring JDBC +JPA + HSQLDB. These scripts are used to perform SQL commands at the time of application start. Java Database Connectivity (JDBC) is an application programming interface (API) for the programming language Java, which defines how a client may access a database. JPA is just a specification that faci
3 min read
Difference Between Spring DAO vs Spring ORM vs Spring JDBC
The Spring Framework is an application framework and inversion of control container for the Java platform. The framework's core features can be used by any Java application, but there are extensions for building web applications on top of the Java EE platform. Spring-DAO Spring-DAO is not a spring module. It does not provide interfaces or templates
5 min read
JUnit - Sample Maven Project With JDBC
The quality of software should be of a high standard and then only it is liked and used by most people. A good programmer is one who prepares software error-free, JUnit helps us a lot in achieving that. In this article, let us see a MySQL database connecting with Java via JDBC, and the data is checked against JUnit. Example Maven Project Project St
6 min read
How to Call Stored Functions and Stored Procedures using JDBC?
JDBC or Java Database Connectivity is a Java API to connect and execute the query with the database. It is a specification from Sun microsystems that provides a standard abstraction(API or Protocol) for java applications to communicate with various databases. It provides the language with java database connectivity standards. It is used to write pr
5 min read
Article Tags :
Practice Tags :