JDBC
JDBC is the JDK API for talking to relational databases. A driver implements Connection, Statement, and ResultSet. You write SQL, bind parameters, and read rows.
Detailed Explanation
DriverManager.getConnection(url, user, pass) or a DataSource gives you a Connection. From it you create a PreparedStatement, set parameters, executeQuery or executeUpdate, and walk a ResultSet. Close everything. try-with-resources is the rule: result set, statement, connection (or return the connection to a pool, which close does for pooled connections).
The URL is driver-specific (jdbc:postgresql://host/db). The driver JAR must be on the classpath. JDBC 4+ loads drivers via ServiceLoader; you rarely Class.forName.
JDBC is stringly typed SQL plus indexes or names on the result set. You handle SQLException (checked). Transactions are setAutoCommit(false), then commit or rollback. Isolation levels sit on the connection.
You reach for raw JDBC when you want full control, a tiny app, or a batch that JPA would make worse. You reach for JPA/Hibernate when you have a domain model and CRUD. You reach for jOOQ or Jdbi when you want SQL-first with less boilerplate than JDBC.
The cost of JDBC is boilerplate and easy resource leaks. The cost of concatenating SQL with user input is injection. Always bind. The cost of holding a connection during HTTP to another service is pool exhaustion.
ResultSet is forward-only by default. Do not treat it as a list after the statement closes. Map rows into objects inside the try block.
JDBC is still the floor under every ORM. When Hibernate emits a bad query, you debug JDBC logs. When a batch job must insert a million rows, PreparedStatement.addBatch plus a sane fetch size is the path that does not explode memory. Learn setFetchSize, generated keys (RETURN_GENERATED_KEYS), and how your driver reports warnings. Those details do not go away because you added Spring Data. They wait until the first production incident.
Code Examples
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class CountUsers {
static int count(String url, String user, String pass) throws Exception {
try (Connection c = DriverManager.getConnection(url, user, pass);
PreparedStatement ps = c.prepareStatement("select count(*) from users");
ResultSet rs = ps.executeQuery()) {
rs.next();
return rs.getInt(1);
}
}
}import java.sql.Connection;
import java.sql.PreparedStatement;
public class Insert {
static void add(Connection c, String name) throws Exception {
try (PreparedStatement ps = c.prepareStatement("insert into users(name) values (?)")) {
ps.setString(1, name); // bound, not concatenated
ps.executeUpdate();
}
}
}Common Gotchas
Forgetting to close connections leaks the pool
use try-with-resources or a template that closes.
`Statement` with string concat is SQL injection
use `PreparedStatement`.
Pooled `close()` returns the connection; it does not shut down the database
Pooled `close()` returns the connection; it does not shut down the database
Reading a `ResultSet` after close throws
Reading a `ResultSet` after close throws