Careeroza
JDBC Architecture
What is JDBC
JDBC (Java Database Connectivity, package java.sql) is the standard API for SQL databases. Your code talks to JDBC interfaces; a driver jar from the database vendor implements them. Spring Data JPA, Hibernate, jOOQ and MyBatis all use JDBC underneath.
Your code ──► JDBC API (java.sql: Connection, PreparedStatement, ResultSet)
│
▼
JDBC driver (mysql-connector-j, postgresql, ojdbc11)
│ network protocol
▼
Database (MySQL, PostgreSQL, Oracle)Core Interfaces
| Interface | Role |
|---|---|
DriverManager / DataSource | give you a Connection (DataSource = pooled, preferred) |
Connection | a session with the DB; controls transactions |
PreparedStatement | precompiled SQL with ? parameters |
ResultSet | cursor over the rows of a query |
SQLException | checked exception for all DB errors (getSQLState(), getErrorCode()) |
Driver Dependency
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
<scope>runtime</scope>
</dependency>
<!-- PostgreSQL: org.postgresql:postgresql:42.7.4 -->Note: Since JDBC 4 drivers register themselves automatically (ServiceLoader). Class.forName("com.mysql.cj.jdbc.Driver") is no longer needed.