Commit 202df7f
committed
docs(jdbc): add detailed explanation of JDBC program flow and core interfaces
What
- Documented the step-by-step flow of how a Java program interacts with a database using JDBC.
- Explained the role of each core `java.sql` interface:
1. `Class.forName()` → dynamically loads JDBC driver.
2. `DriverManager` → manages drivers and creates connections.
3. `Connection` → represents active session with database.
4. `Statement` → executes SQL queries/updates.
5. `ResultSet` → processes query results.
- Added a complete runnable example demonstrating connection to SQLite (`univ.db`) and retrieving data from `students` table.
Why
- JDBC is fundamental for Java-based database applications, yet its flow can be confusing to beginners.
- Provides clarity on how each component fits into the lifecycle: load driver → connect → query → process → close.
- Reinforces importance of closing resources to prevent memory leaks and ensure DB reliability.
How
- Broke explanation into sections: purpose, usage, importance of each interface.
- Showed step-by-step process (load → connect → create → execute → process → close).
- Provided example with `Class.forName`, `DriverManager.getConnection`, `Statement.executeQuery`, and iterating over `ResultSet`.
Key Points
- **Driver loading** via `Class.forName()` registers the JDBC driver with `DriverManager`.
- **Connection** is mandatory for all SQL operations.
- **Statement** sends SQL commands; `executeQuery` for `SELECT`, `executeUpdate` for `INSERT/UPDATE/DELETE`.
- **ResultSet** is a cursor that moves row by row (`rs.next()`).
- **Closing resources** (`rs.close()`, `stmt.close()`, `con.close()`) is critical for proper cleanup.
Real-life Applications
- Web applications (e.g., Spring Boot) connecting to relational databases like MySQL, PostgreSQL, Oracle.
- Enterprise apps retrieving employee, order, or financial data.
- Standalone tools/scripts for reporting, data migration, or auditing.
Notes
- Always use **try-with-resources** or finally blocks to close JDBC resources.
- Use **PreparedStatement** instead of Statement for parameterized queries to improve security and performance.
- JDBC API abstracts database communication → same code can work across different databases by changing the driver.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent f1aadcc commit 202df7f
File tree
1 file changed
+8
-7
lines changed- Section28JDBCusingSQLite/JDBC
1 file changed
+8
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
| 8 | + | |
8 | 9 | | |
9 | | - | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
19 | | - | |
| 20 | + | |
20 | 21 | | |
21 | | - | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
83 | 84 | | |
84 | 85 | | |
85 | 86 | | |
86 | | - | |
| 87 | + | |
87 | 88 | | |
88 | 89 | | |
89 | 90 | | |
| |||
116 | 117 | | |
117 | 118 | | |
118 | 119 | | |
119 | | - | |
| 120 | + | |
0 commit comments