Memra

JDBC architecture: drivers, connections & the four core interfaces

How JDBC abstracts database access and the interfaces every Java developer needs to know.

What JDBC is

JDBC (Java Database Connectivity) is a standard API in java.sql that lets Java code talk to any relational database — MySQL, PostgreSQL, SQLite, Oracle — through a common interface. The database vendor ships a JDBC driver (a jar) that implements that interface. Your application code never calls the driver directly; it talks to the standard API.

Connecting to a database

You open a connection via DriverManager.getConnection, passing a JDBC URL, a username, and a password:

Connection conn = DriverManager.getConnection(
    "jdbc:postgresql://localhost:5432/mydb",
    "alice",
    "s3cret"
);

JDBC URL format: jdbc:<subprotocol>://<host>:<port>/<database>[?options]

| Subprotocol | Example URL | |---|---| | postgresql | jdbc:postgresql://localhost:5432/mydb | | mysql | jdbc:mysql://localhost:3306/mydb | | h2 | jdbc:h2:mem:testdb (in-memory) | | sqlite | jdbc:sqlite:/path/to/file.db |

The four core interfaces

| Interface | Role | |---|---| | Driver | Registers with DriverManager; loaded from the jar | | Connection | Represents one database session; source of statements | | Statement / PreparedStatement | Executes SQL | | ResultSet | Holds rows returned by a query |

You rarely touch Driver directly — the driver jar auto-registers itself via a ServiceLoader mechanism when it's on the classpath (since JDBC 4.0 / Java 6). Just add the driver jar, write the URL, and DriverManager finds the right driver.

NORMAL ~/memra/learn/java-from-zero/jdbc-architecture utf-8 LF