Companion Notes: Your First Query Through a Database Driver
These notes are written like the chapters of a short screencast, but there is no video; everything you need is in the text. We use Java and JDBC because it shows each step a driver performs very clearly. The same steps exist in every language.
Chapter 1: what we are building
A tiny Java program that connects to a PostgreSQL database, asks for the names of all books, and prints them. If you use another language, read along anyway and look for the same five steps: load the driver, connect, send a query, read results, close.
Chapter 2: getting the driver
JDBC (Java Database Connectivity) is Java's standard interface for databases. Each database provides a JDBC driver as a .jar library. With a build tool such as Maven or Gradle, you add the PostgreSQL JDBC driver as a dependency; the PostgreSQL project's JDBC site lists the exact coordinates and current version. Modern drivers register themselves automatically, so you do not need extra code to "load" them.
Chapter 3: connecting
String url = "jdbc:postgresql://localhost:5432/library";
String user = System.getenv("DB_USER");
String password = System.getenv("DB_PASSWORD");
try (Connection conn = DriverManager.getConnection(url, user, password)) {
// chapters 4 and 5 go here
}
The jdbc:postgresql:// part tells Java which driver to use. User and password come from environment variables so they are not written in the code. try ( ... ) is a "try-with-resources" block: Java closes the connection automatically at the end, even if an error happens.
Chapter 4: sending a query with a placeholder
String sql = "SELECT title FROM books WHERE year >= ? ORDER BY title";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, 2020);
// chapter 5 goes here
}
A PreparedStatement holds SQL with ? placeholders. setInt(1, 2020) fills the first placeholder with the number 2020. The value is sent separately from the SQL, which is what protects against SQL injection.
Chapter 5: reading the results
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
System.out.println(rs.getString("title"));
}
}
A ResultSet is like a bookmark that moves through the rows. next() moves to the next row and returns false when there are none left. getString("title") reads the title column of the current row. Here the driver is converting database text into a Java String.
Chapter 6: when things go wrong
| Message you might see | What it usually means |
|---|---|
| "No suitable driver found" | The driver library is missing, or the URL prefix is mistyped |
| "Connection refused" | The database is not running, or the host or port is wrong |
| "password authentication failed" | Wrong user name or password |
| "relation books does not exist" | The table is missing, or you connected to a different database |
All of these arrive as a SQLException. Read its message slowly; it is the driver passing on what the database said.
Chapter 7: the same steps elsewhere
| Step | Java (JDBC) | Python (DB-API) |
|---|---|---|
| Connect | DriverManager.getConnection | connect() |
| Prepare query | prepareStatement with ? | cursor.execute(sql, params) |
| Read rows | ResultSet.next() | loop over the cursor |
| Close | try-with-resources | close() or with |
To understand what the driver is doing behind these calls, read an essay on database drivers.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.
Illustrative outline only: no live session, recording, registration or certificate is offered on this site. Any speakers, panels or schedules mentioned are hypothetical.