Nearby lessons

8 of 37

JDBC - Connections (6 Steps)

📌 What You Will Learn
  • 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.
Example01
JCode Cell
11. Load & register driver
22. Establish Connection
33. Create Statement
44. Send & execute SQL
55. Process ResultSet
66. Close 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:

Example02
JCode Cell
1Class.forName("com.mysql.cj.jdbc.Driver"); // loads + registers

Step 1 — Load and Register the Driver

You can also register manually:

Example03
JCode Cell
1class JdbcDriver {
2 static {
3 DriverManager.registerDriver(new JdbcDriver());
4 }
5}

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.

In simple words: From JDBC 4.0 the driver loads itself from the classpath — you never have to write `Class.forName()` again. The driver JAR tells DriverManager it exists, so step 1 of the six steps becomes optional.
Trainer's Note: Trainer's advice: for modern Java, simply skip step 1. Put the driver JAR in the classpath, and the DriverManager will find it by itself. We still write Class.forName() in this chapter once, only so you understand the older code you will meet in college materials.
Example04
JCode Cell
1Driver d = new JdbcDriver();
2DriverManager.registerDriver(d);

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:

Example05
JCode Cell
1Connection con = DriverManager.getConnection(jdbcUrl, username, password);

Step 2 — Establish the Connection

In simple words: The JDBC URL is always three parts separated by colons: `jdbc : driver : database detail`. The first part is always the word jdbc, the middle part names the driver, and the last part carries the host, port and database name.
DatabaseJDBC URLDriver class (Type-4)
MySQLjdbc:mysql://localhost:3306/dbnamecom.mysql.cj.jdbc.Driver
Oraclejdbc:oracle:thin:@localhost:1521:xeoracle.jdbc.OracleDriver
PostgreSQLjdbc:postgresql://localhost:5432/dbnameorg.postgresql.Driver

When you call getConnection(), DriverManager internally calls the driver's connect() method, which actually opens the road to the database.

Example06
JCode Cell
1<mainprotocol> : <subprotocol> : <subname>
2 | | |
3 always driver database detail
4 "jdbc"
5 
6Example (MySQL): jdbc:mysql://localhost:3306/college
7Example (Oracle): jdbc:oracle:thin:@localhost:1521:xe

Steps 3, 4 and 5 — Statement, Execute, Process

Example07
JCode Cell
1// Step 3 - create the statement (the vehicle)
2Statement st = con.createStatement();
3 
4// Step 4 - send and execute the query (for SELECT use executeQuery)
5ResultSet rs = st.executeQuery("SELECT * FROM employee");
6 
7// Step 5 - process the result rows
8while (rs.next()) {
9 System.out.println(rs.getInt(1) + " " + rs.getString(2));
10}

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).

In simple words: Always close JDBC resources in reverse order — ResultSet, then Statement, then Connection. In modern Java, try-with-resources closes them automatically, so you cannot forget.
Example08
JCode Cell
1con.close(); // free the connection (and its resources)
📝 Key Takeaways
  • 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.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4