Commit b2f1a1e
committed
feat(jdbc-sqlite): add parameterized query using PreparedStatement in Call.java
What
- Introduced `Call.java` under Section28JDBCusingSQLite.
- Demonstrates executing parameterized SELECT queries with JDBC.
- Uses `PreparedStatement` to fetch student records by `deptno`.
Why
- Prevents SQL injection by avoiding string concatenation in queries.
- Enhances code readability and maintainability with reusable query templates.
- Provides safer way to filter results dynamically with parameters.
How
- Setup:
- Loaded SQLite JDBC driver: `Class.forName("org.sqlite.JDBC")`.
- Established database connection with `DriverManager.getConnection(url)`.
- Execution:
- Created `PreparedStatement` with query: `SELECT * FROM students WHERE deptno = ?`.
- Bound parameter `deptno = 10` via `setInt(1, 10)`.
- Executed query and iterated `ResultSet` to print student details (roll, name, city, deptno).
- Cleanup:
- Closed `ResultSet`, `PreparedStatement`, and `Connection` properly.
Key Notes
- SQLite does not support stored procedures → parameterized queries via `PreparedStatement` are the alternative.
- `%d | %s | %s | %d` format ensures neatly aligned output for each student row.
- Placeholders (`?`) allow dynamic query reuse without rewriting SQL strings.
Real-life Applications
- Login systems: validating username/password safely against DB.
- Student management: fetching students by department/grade dynamically.
- E-commerce apps: querying products by category or price range.
- Banking apps: retrieving transactions by account number securely.
Future Improvements
- Add multiple parameters (e.g., `deptno` + `city`) for advanced filtering.
- Wrap code in try-with-resources for automatic cleanup.
- Implement pagination using `LIMIT` and `OFFSET` for large result sets.
- Abstract query execution into utility/service classes for reusability.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent aae09f8 commit b2f1a1e
File tree
1 file changed
+25
-0
lines changed- Section28JDBCusingSQLite/JAVA SQL Interfaces/CallableStatement/src
1 file changed
+25
-0
lines changedLines changed: 25 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
2 | 27 | | |
0 commit comments