JDBC Connection in Java: Step-by-Step Guide for Beginners (2025)

Table of contents
JDBC Connection in Java: Step-by-Step Guide for Beginners (2025)
JDBC (Java Database Connectivity) is the foundation of working with databases in Java. But if you're new to backend development, JDBC can feel confusing at first.
In this post, weโll break down JDBC into 5 simple steps, explain what they do, and how they work internally. No fluff โ just clear concepts and real understanding.
๐ง What is JDBC?
JDBC is a Java API that allows Java applications to interact with relational databases like MySQL, PostgreSQL, Oracle, etc.
Think of it as a bridge between Java code and your database.
๐ JDBC Architecture
Components of JDBC Architecture
Application: Java application or servlet that sends SQL requests.
JDBC API:
Interfaces:
Driver
,Connection
,Statement
,PreparedStatement
,CallableStatement
,ResultSet
,RowSet
Classes:
DriverManager
,Types
,Blob
,Clob
DriverManager: Selects and connects to the right driver.
JDBC Drivers: Handle low-level communication with the database.
Architecture Models
1. Two-Tier Architecture
Java Application โ JDBC Driver โ Database
Direct connection from application to DB via JDBC driver.
2. Three-Tier Architecture
Java Application โ Application Server โ JDBC Driver โ Database
The application interacts with a middle-tier server, which manages the database connection.
๐ JDBC Components
JDBC API:
java.sql
: Core interfaces and classes (connections, queries, result sets)javax.sql
: Advanced features (connection pooling, data sources)
JDBC DriverManager:
Loads and manages JDBC drivers
Establishes DB connections
JDBC Test Suite:
- Tests JDBC driver functionality (insert, update, delete, etc.)
JDBC Drivers:
Type-1: JDBC-ODBC Bridge (Deprecated)
Type-2: Native-API driver (Partially Java)
Type-3: Network Protocol driver (Fully Java)
Type-4: Thin driver (Fully Java, most commonly used)
๐ 5 Steps to Connect Java with MySQL using JDBC
โ Step 1: Load the JDBC Driver
Class.forName("com.mysql.cj.jdbc.Driver");
What: Registers the MySQL driver with Java. How: The class's static block registers the driver using DriverManager.registerDriver()
.
โ Step 2: Establish the Connection
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/yourDB", "root", "password");
What: Creates a session with the database. How: Uses the driver to open a TCP/IP socket, authenticate credentials, and return a connection object.
โ Step 3: Create Statement / Query
Statement stmt = con.createStatement();
String query = "SELECT * FROM students";
ResultSet rs = stmt.executeQuery(query);
What: Sends SQL commands to the DB. How: SQL is compiled and executed by the database engine; results are returned as ResultSet
.
โ Step 4: Process the ResultSet
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println(id + " - " + name);
}
What: Reads DB data row by row. How: JDBC fetches and buffers rows in the ResultSet; methods like getInt()
convert DB types to Java types.
โ Step 5: Close the Connection
con.close();
What: Ends the connection and frees resources. How: Socket is closed, memory is freed, and pooled connections are returned to the pool.
๐ฏ Summary Table
Step | Description | Internal Working |
Load Driver | Register JDBC driver | Static block โ DriverManager |
Get Connection | Connect to DB | TCP/IP socket + authentication |
Create Statement | Build SQL command | Statement buffer setup |
Process Result | Read DB data | Row buffer โ Java values |
Close Connection | End session | Socket closed + memory cleanup |
๐ JDBC Classes and Interfaces
Class/Interface | Description |
DriverManager | Manages JDBC drivers and DB connections |
Connection | Represents a DB session |
Statement | Executes static SQL queries |
PreparedStatement | Precompiled SQL statement with parameters |
CallableStatement | Executes stored procedures |
ResultSet | Result set of a query |
SQLException | Handles SQL exceptions |
๐ Create a Simple JDBC Application
import java.sql.*;
public class Geeks {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/your_database";
String username = "your_username";
String password = "your_password";
String query = "INSERT INTO students (id, name) VALUES (109, 'bhatt')";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection c = DriverManager.getConnection(url, username, password);
Statement st = c.createStatement();
int count = st.executeUpdate(query);
System.out.println("Number of rows affected: " + count);
st.close();
c.close();
System.out.println("Connection closed.");
} catch (ClassNotFoundException e) {
System.err.println("JDBC Driver not found: " + e.getMessage());
} catch (SQLException e) {
System.err.println("SQL Error: " + e.getMessage());
}
}
}
Output: Inserts a new record into the students
table.
๐ Key Features of JDBC
Platform Independence: Works across operating systems.
Standard API: One API for multiple DBs.
Multi-DB Support: Compatible with MySQL, PostgreSQL, Oracle, etc.
๐ Coming Up Next:
In the next post, weโll explore
PreparedStatement
vsStatement
, and how it prevents SQL Injection.
๐ If this helped you, drop a like or comment.
๐ฌ Have doubts? I reply to every technical question.
Written by: Harsh
Tagline: Learning backend one line at a time.
Subscribe to my newsletter
Read articles from Harsh Jaiman directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
