Commit 181a8df
committed
feat(jdbc-sqlite): add interactive query execution with user input in Statements.java
What
- Added `Statements.java` under Section28JDBCusingSQLite package.
- Demonstrates executing parameterized SELECT queries using `PreparedStatement`.
- Integrated `Scanner` to allow dynamic `deptno` input from the user.
- Prints student records in a formatted structure or informs when no records are found.
Why
- Highlights secure and flexible query execution by combining JDBC with user input.
- Avoids SQL injection by binding parameters via `PreparedStatement` instead of string concatenation.
- Improves user experience with real-time query input/output.
- Shows practical usage of `ResultSet.isBeforeFirst()` to detect empty results.
How
- Setup:
- Loaded SQLite JDBC driver with `Class.forName("org.sqlite.JDBC")`.
- Established connection to `univ.db` SQLite database.
- Execution:
- Prepared query: `select * FROM students where deptno=?`.
- Prompted user for a department number (`deptno`) via `Scanner`.
- Bound parameter dynamically using `pstm.setInt(1, dno)`.
- Executed query with `executeQuery()`.
- Output:
- Checked if any records exist with `rs.isBeforeFirst()`.
- Iterated through `ResultSet`, printing student details (roll, name, city, deptno).
- Displayed "record not found" if no matching rows exist.
- Cleanup:
- Closed `PreparedStatement` and connection to release resources.
Key Notes
- `PreparedStatement` ensures safer, cleaner, and reusable SQL queries.
- `isBeforeFirst()` is useful for detecting empty results without iterating.
- Scanner allows runtime interaction with the database, simulating real-world applications.
- Example output format:
`101 | Alice | Delhi | 10 |`
Real-life Applications
- University management systems: fetch students by department dynamically.
- Employee systems: filter employees by department, branch, or role.
- Banking apps: query transactions by account/branch ID.
- Interactive console-based reporting tools for admins.
Future Improvements
- Add error handling for invalid input (e.g., non-numeric deptno).
- Implement try-with-resources for automatic cleanup of JDBC objects.
- Enhance output formatting into tabular form for readability.
- Extend functionality to allow updates, inserts, and deletes via user input.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent b2f1a1e commit 181a8df
File tree
1 file changed
+80
-31
lines changed- Section28JDBCusingSQLite/JAVA SQL Interfaces/Prepared Statements/src
1 file changed
+80
-31
lines changedLines changed: 80 additions & 31 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | 1 | | |
22 | 2 | | |
23 | 3 | | |
24 | 4 | | |
25 | 5 | | |
26 | 6 | | |
27 | | - | |
| 7 | + | |
28 | 8 | | |
29 | 9 | | |
30 | | - | |
31 | | - | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
32 | 15 | | |
33 | 16 | | |
34 | 17 | | |
| |||
53 | 36 | | |
54 | 37 | | |
55 | 38 | | |
56 | | - | |
57 | | - | |
| 39 | + | |
| 40 | + | |
58 | 41 | | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
63 | 48 | | |
64 | 49 | | |
65 | 50 | | |
66 | | - | |
| 51 | + | |
67 | 52 | | |
68 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
0 commit comments