Part 11 / Topic 7 of 10
JDBC: connecting to a real database
JDBC (Java Database Connectivity) is the JDK's API for talking to SQL databases: java.sql.Connection, Statement, ResultSet. What the JDK does not ship is a driver for any specific database — JDBC is an interface, and every database vendor publishes its own jar implementing it. This course uses SQLite (a real, file-based SQL database, not a toy) through org.xerial:sqlite-jdbc:3.50.3.0, downloaded once to examples/lib/sqlite-jdbc-3.50.3.0.jar — the one deliberate break from "nothing beyond the JDK" in this whole course (DESIGN-NOTES.md #7).
Think of JDBC as a universal phone jack, and the driver jar as the specific adapter for your country's socket shape — the jack (the API you write against) never changes; only the adapter does, per database.
Add the jar to the classpath by hand for both javac and java — no build tool, exactly as Part 7 managed multi-file compiles before any tooling existed for that either:
$ javac -cp lib/sqlite-jdbc-3.50.3.0.jar JdbcBasicsDemo.java
$ java -cp .:lib/sqlite-jdbc-3.50.3.0.jar JdbcBasicsDemo
(Windows: ; instead of : between classpath entries — the same rule as every -cp example since Part 1.)
String url = "jdbc:sqlite:" + dbFile; // the URL names the driver (jdbc:sqlite:) AND the database
try (Connection connection = DriverManager.getConnection(url)) {
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE books (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, author TEXT, price REAL)");
statement.executeUpdate("INSERT INTO books (title, author, price) VALUES ('The Guide', 'R. K. Narayan', 299.0)");
try (ResultSet rows = statement.executeQuery("SELECT * FROM books")) {
while (rows.next()) {
System.out.println(rows.getString("title"));
}
}
}
}
Connected to: data/jdbc-basics.db
Driver: SQLite JDBC 3.50.3.0
Table created.
Rows inserted: 3
-- all books, cheapest first --
#1 The Guide by R. K. Narayan Rs 299.00
#3 The White Tiger by Aravind Adiga Rs 349.00
#2 Em and the Big Hoom by Jerry Pinto Rs 399.00
-- one aggregate query --
total books: 3, average price: Rs 349.00
sqlite-jdbc registers itself with DriverManager automatically the moment the jar is on the classpath (JDBC 4+; no separate "load the driver class" line needed, unlike some much older JDBC tutorials you may find online). Connection, Statement and ResultSet are all AutoCloseable, so nesting them in try-with-resources (section 2's rule, three levels deep here) closes ResultSet → Statement → Connection, innermost first, even if a query throws partway through.
| Piece | What it is |
|---|---|
DriverManager.getConnection(url) |
opens one connection to the database named in the URL |
Connection |
one open conversation with the database; create statements from it |
Statement |
a SQL command you send as-is (fine for DDL like CREATE TABLE; see section 8 for anything with user data in it) |
ResultSet |
the rows a query returned; next() moves to the next row, false when there are no more |
Full program: examples/JdbcBasicsDemo.java.
Done here? The practice set for Part 11 — including the solutions — is on the part page.