Skip to content

Commit b2f1a1e

Browse files
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 changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,27 @@
1+
import java.sql.*;
2+
13
public class Call {
4+
public static void main(String[] args) throws Exception {
5+
Class.forName("org.sqlite.JDBC");
6+
String url = "jdbc:sqlite:/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section28JDBCusingSQLite/univ.db";
7+
8+
Connection con = DriverManager.getConnection(url);
9+
10+
// Use PreparedStatement because SQLite does not support stored procedures
11+
PreparedStatement cst = con.prepareStatement("SELECT * FROM students WHERE deptno = ?");
12+
cst.setInt(1, 10); // example department
13+
14+
ResultSet rs = cst.executeQuery();
15+
while (rs.next()) {
16+
System.out.printf("%d | %s | %s | %d%n",
17+
rs.getInt("roll"),
18+
rs.getString("name"),
19+
rs.getString("city"),
20+
rs.getInt("deptno"));
21+
}
22+
23+
rs.close();
24+
cst.close();
25+
con.close();
26+
}
227
}

0 commit comments

Comments
 (0)