Nearby lessons
9 of 37JDBC - First Complete Program
📌 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
First Complete Program is a fundamental part of database programming with JDBC. This lesson explains Your First Complete JDBC Program (Modern) and Common Beginner Errors and Fixes with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
Your First Complete JDBC Program (Modern)
Here is the full six-step program for MySQL (Type-4 driver). This is the modern replacement for the old JDBC-ODBC examples in the classic material:
To run it, put the MySQL connector JAR in the classpath:
Example01
Your First Complete JDBC Program (Modern)
Example02
Common Beginner Errors and Fixes
| Error | Why it happens | Fix |
|---|---|---|
| ClassNotFoundException | Driver JAR not in classpath / wrong driver class name | Add the JAR; check the exact driver class name |
| SQLException: No suitable driver | Wrong JDBC URL, or driver not loaded | Check the URL prefix (jdbc:mysql:...) |
| Access denied for user | Wrong username or password | Check credentials |
| Unknown database | The database name in the URL does not exist | Create the database first |
| Connection refused | MySQL server is not running | Start the MySQL server |
📝 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 QuestionsProgress: 0 / 4