JDBC (Java Database Connectivity) architecture defines how Java applications communicate with relational databases. It provides a standard API layer over database-specific drivers, ensuring:
- Database independence
- Easy switching between different databases
- Consistent programming model
JDBC follows a multi-layer architecture, consisting of:
- JDBC API layer (application side)
- JDBC Driver layer (database side)
These two work together to send SQL queries and receive results.
+------------------------------------------------------------+
| Java Application |
| (Your Java Code using JDBC API: Connection, Statement) |
+-----------------------------↑------------------------------+
|
JDBC API (java.sql)
|
+-----------------------------↓------------------------------+
| JDBC Driver |
| (Vendor-specific driver: MySQL, Oracle, PostgreSQL) |
+-----------------------------↑------------------------------+
|
Database Protocol
|
+-----------------------------↓------------------------------+
| Database Server |
| (MySQL, Oracle, PostgreSQL, SQL Server) |
+------------------------------------------------------------+
JDBC architecture has three major components:
Provided by Java, found in:
java.sql.*javax.sql.*
It contains the important interfaces:
| JDBC API Component | Description |
|---|---|
| DriverManager | Loads and manages database drivers |
| Connection | Represents a DB connection |
| Statement | Executes SQL statements |
| PreparedStatement | Precompiled SQL, safer |
| CallableStatement | Calls stored procedures |
| ResultSet | Stores result of SELECT queries |
These interfaces hide database-specific details → your Java code remains independent of the DB vendor.
JDBC drivers are vendor-specific implementations (MySQL, Oracle, PostgreSQL drivers).
They translate Java method calls into database-specific protocol commands.
Located in the JAR file you add to your project, e.g.:
- MySQL:
mysql-connector-j.jar - PostgreSQL:
postgresql.jar - Oracle:
ojdbc8.jar
Actual database software (MySQL, Oracle, PostgreSQL...). It receives commands from the JDBC driver and returns results.
Step-by-step execution flow:
Class.forName("com.mysql.cj.jdbc.Driver");Driver registers automatically with the DriverManager.
Application calls:
Connection con = DriverManager.getConnection(url, user, pass);DriverManager → selects appropriate driver → through driver it opens a DB connection.
Application creates:
PreparedStatement ps = con.prepareStatement("SELECT * FROM emp");
ResultSet rs = ps.executeQuery();JDBC API → sends SQL through the driver → driver translates to database protocol → DB executes query.
Database → driver → converts raw DB response → Java ResultSet object.
Application processes:
while (rs.next()) {
System.out.println(rs.getInt("id"));
}rs.close();
ps.close();
con.close();Releases DB resources and connection.
JDBC defines 4 types of drivers.
- Translates JDBC to ODBC calls
- Very slow
- Removed in Java 8
- Platform-dependent
Java → JDBC API → Type 2 driver → Native API → DB
- Uses a middleware server
- Converts JDBC calls into DB-independent protocol
- Directly converts JDBC calls into database-specific protocol
- 100% Java
- Platform-independent
- Used by MySQL, Oracle, PostgreSQL drivers
Java → JDBC API → Type 4 Driver → Database
| Benefit | Explanation |
|---|---|
| Database independence | Your code does not change when switching DB (only driver changes) |
| Modularity | Application, JDBC API, driver, and DB remain separate |
| Flexibility | Can use any relational DB supporting JDBC |
| Security | API manages sensitive connections securely |
| Extensibility | Drivers can be replaced or upgraded |
PreparedStatement ps = con.prepareStatement(
"SELECT * FROM employee WHERE id = ?"
);
ps.setInt(1, 101);
ResultSet rs = ps.executeQuery();- Converts Java calls into driver calls
- Converts JDBC calls into database-specific protocol
- Sends command to DB server
- Executes SQL
- Returns result → driver → ResultSet → Java application
-
JDBC architecture is layered: Application → JDBC API → Driver → Database
-
JDBC API contains all standard interfaces (
Connection,Statement, etc.) -
JDBC drivers perform translation between Java and DB protocol
-
Type 4 driver is most commonly used today (pure Java driver)
-
JDBC ensures database independence by abstracting database-specific behavior