Skip to content

Latest commit

 

History

History
236 lines (154 loc) · 6.11 KB

File metadata and controls

236 lines (154 loc) · 6.11 KB

JDBC Architecture

1. Overview

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:

  1. JDBC API layer (application side)
  2. JDBC Driver layer (database side)

These two work together to send SQL queries and receive results.


2. Overall JDBC Architecture Diagram

+------------------------------------------------------------+
|                    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)        |
+------------------------------------------------------------+

3. Components of JDBC Architecture

JDBC architecture has three major components:

3.1 JDBC API

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.

3.2 JDBC Driver

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

3.3 Database Server

Actual database software (MySQL, Oracle, PostgreSQL...). It receives commands from the JDBC driver and returns results.


4. How JDBC Works Internally (Flow)

Step-by-step execution flow:

Step 1: Load Driver

Class.forName("com.mysql.cj.jdbc.Driver");

Driver registers automatically with the DriverManager.

Step 2: Establish Connection

Application calls:

Connection con = DriverManager.getConnection(url, user, pass);

DriverManager → selects appropriate driver → through driver it opens a DB connection.

Step 3: Execute SQL Statement

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.

Step 4: Database Returns Result

Database → driver → converts raw DB response → Java ResultSet object.

Application processes:

while (rs.next()) {
    System.out.println(rs.getInt("id"));
}

Step 5: Close Resources

rs.close();
ps.close();
con.close();

Releases DB resources and connection.


5. JDBC Drivers — The Backbone of Architecture

JDBC defines 4 types of drivers.

5.1 Type 1 — JDBC-ODBC Bridge (Deprecated)

  • Translates JDBC to ODBC calls
  • Very slow
  • Removed in Java 8

Uses native database libraries

  • Platform-dependent
Java → JDBC API → Type 2 driver → Native API → DB

5.3 Type 3 — Network Protocol Driver

  • Uses a middleware server
  • Converts JDBC calls into DB-independent protocol

5.4 Type 4 — Pure Java Driver (Most Popular)

  • 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

6. Benefits of JDBC’s Layered Architecture

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

7. Real-World Workflow Example

Java Application:

PreparedStatement ps = con.prepareStatement(
    "SELECT * FROM employee WHERE id = ?"
);
ps.setInt(1, 101);
ResultSet rs = ps.executeQuery();

JDBC API:

  • Converts Java calls into driver calls

JDBC Driver:

  • Converts JDBC calls into database-specific protocol
  • Sends command to DB server

Database:

  • Executes SQL
  • Returns result → driver → ResultSet → Java application

Key Takeaways

  • 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