Nearby lessons
8 of 37JDBC - Connections (6 Steps)
- The six standard steps of every JDBC program
- The JDBC URL and how to read it
- Loading and registering the driver (old vs modern)
- Your first complete JDBC program (modern MySQL)
- Common beginner errors and their fixes
Connections (6 Steps) is a fundamental part of database programming with JDBC. This lesson explains The Six Standard Steps, — Load and Register the Driver and — Establish the Connection with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
The Six Standard Steps
Every JDBC application follows the same six steps. Learn them once — every program in this material is a variation:
- Load and register the driver class.
- Establish a connection between the Java application and the database.
- Create a Statement object.
- Send and execute the SQL query.
- Process the results from the ResultSet.
- Close the connection.
Step 1 — Load and Register the Driver
The JDBC API is only interfaces. The database vendor provides the implementation — the driver software — as a JAR file. First we put that JAR in the classpath, then we load the driver class.
Old style (JDBC 3.0 and before): load the class with Class.forName(...). When a class is loaded, its static block runs automatically — and the driver's static block registers itself with DriverManager:
Inside the driver class there is a static block that does this registration for us:
Step 1 — Load and Register the Driver
You can also register manually:
Step 1 — Load and Register the Driver
Modern style (JDBC 4.0+, Java 6+)
From JDBC 4.0, the driver class is loaded automatically from the classpath using the META-INF/services file inside the driver JAR. You don't need `Class.forName()` at all — but writing it does no harm and still works.
Step 2 — Establish the Connection
Use DriverManager.getConnection() with three things: the JDBC URL, the username and the password.
The JDBC URL has a fixed syntax:
Step 2 — Establish the Connection
| Database | JDBC URL | Driver class (Type-4) |
|---|---|---|
| MySQL | jdbc:mysql://localhost:3306/dbname | com.mysql.cj.jdbc.Driver |
| Oracle | jdbc:oracle:thin:@localhost:1521:xe | oracle.jdbc.OracleDriver |
| PostgreSQL | jdbc:postgresql://localhost:5432/dbname | org.postgresql.Driver |
When you call getConnection(), DriverManager internally calls the driver's connect() method, which actually opens the road to the database.
Steps 3, 4 and 5 — Statement, Execute, Process
Step 6 — Close the Connection
Always close in reverse order: ResultSet → Statement → Connection. And even better — modern Java lets you use try-with-resources so closing happens automatically (we cover this in Chapter 6's coding standards).
- Six steps: load driver → connect → statement → execute → process → close.
- JDBC URL = jdbc : subprotocol : subname (always starts with jdbc).
- Class.forName() was needed before JDBC 4.0; now drivers auto-load.
- executeQuery() is for SELECT; it returns a ResultSet.
- Use rs.next() to walk through rows and getXXX() to read values.
- Always close resources; try-with-resources does it automatically.