Commit 27af603
committed
feat(jdbc-sqlite): add DataBase demo to fetch and display student records
What
- Added `DataBase` example under Section28JDBCusingSQLite.
- Establishes a connection to `univ.db` using SQLite JDBC driver.
- Executes a `SELECT * FROM students` query via `Statement`.
- Iterates over `ResultSet` to print all student details:
- Roll number
- Name
- City
- Department number
Why
- Demonstrates the use of plain `Statement` for executing static SQL queries.
- Provides a foundational example for working with `ResultSet` in JDBC.
- Useful for quick table dumps or when parameter binding is not required.
- Serves as a stepping stone before introducing `PreparedStatement`.
How
- Loaded SQLite JDBC driver with `Class.forName("org.sqlite.JDBC")`.
- Opened connection using `DriverManager.getConnection()`.
- Created `Statement` to run SQL queries.
- Executed `executeQuery("SELECT * FROM students")`.
- Used `rs.next()` loop to retrieve column values (`getInt`, `getString`).
- Printed records in a pipe-delimited format.
- Closed `Statement` and `Connection` to release resources.
Key Notes
- `Statement` is best for static SQL with no dynamic parameters.
- `ResultSet` cursor moves row by row; must check with `rs.next()`.
- Always close JDBC resources to prevent memory leaks.
- For dynamic queries, prefer `PreparedStatement`.
Real-life Applications
- Fetch all records for reporting modules.
- Generate tabular views in admin dashboards.
- Quick debugging tool to verify table contents.
- Base implementation for migrating data to another system.
Future Improvements
- Replace `Statement` with `PreparedStatement` for parameterized queries.
- Use try-with-resources for automatic resource cleanup.
- Format output into aligned tables for better readability.
- Extend functionality to filter records (e.g., by deptno or city).
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 96a5497 commit 27af603
File tree
1 file changed
+9
-9
lines changed- Section28JDBCusingSQLite/JAVA SQL Interfaces/src
1 file changed
+9
-9
lines changedLines changed: 9 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
11 | | - | |
12 | | - | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | | - | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
29 | | - | |
30 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
31 | 31 | | |
32 | 32 | | |
0 commit comments