Memra

JDBC architecture: drivers, connections & the four core interfaces

◈ 3 cards

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]

SubprotocolExample URL
postgresqljdbc:postgresql://localhost:5432/mydb
mysqljdbc:mysql://localhost:3306/mydb
h2jdbc:h2:mem:testdb (in-memory)
sqlitejdbc:sqlite:/path/to/file.db

The four core interfaces

InterfaceRole
DriverRegisters with DriverManager; loaded from the jar
ConnectionRepresents one database session; source of statements
Statement / PreparedStatementExecutes SQL
ResultSetHolds 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.

Your applicationConnection · PreparedStatement · ResultSetJDBC API (java.sql)standard interfacesVendor driver jarpostgresql · mysql · h2 · sqliteDatabasejdbc:postgresql://localhost:5432/mydbportablevendor-specific
Your code only ever names the java.sql interfaces. Changing the JDBC URL and swapping the driver jar changes the bottom two bands and nothing above them.
NORMAL ~/memra/learn/java-from-zero/jdbc-architecture utf-8 LF