Commit aae09f8
committed
feat(jdbc-sqlite): add InsertusingPreparedtable with PreparedStatement for safe student insertion
What
- Introduced `InsertusingPreparedtable.java` to demonstrate inserting records into the `students` table.
- Replaced raw SQL statements with `PreparedStatement` using placeholders (`?`).
- Added user interaction via `Scanner` to accept input for:
- roll (int)
- name (String)
- city (String)
- deptno (int)
Why
- PreparedStatement offers:
- Protection against SQL Injection attacks.
- Precompiled SQL → improved performance for repeated queries.
- Cleaner, safer, and more maintainable code.
How
- Setup:
- Loaded SQLite JDBC driver with `Class.forName("org.sqlite.JDBC")`.
- Established connection to `univ.db`.
- Query:
- `"INSERT INTO students (roll, name, city, deptno) VALUES (?, ?, ?, ?)"`.
- Bound user-provided values with `stm.setInt` / `stm.setString`.
- Execution:
- Called `executeUpdate()`, printing affected row count.
- Cleanup:
- Closed `PreparedStatement` and `Connection`.
Real-life applications
- Student registration forms in university systems.
- Employee onboarding where user input is inserted into HR databases.
- E-commerce apps storing customer details securely.
- Banking apps handling account creation without injection risks.
Notes
- Always validate user input before binding values.
- Use try-with-resources to auto-close connections and statements.
- For multiple inserts, reuse the same `PreparedStatement` in a loop.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 0bace29 commit aae09f8
File tree
1 file changed
+54
-11
lines changed- Section28JDBCusingSQLite/DMLUsingJDBC/src
1 file changed
+54
-11
lines changedLines changed: 54 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
9 | 11 | | |
10 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
11 | 16 | | |
12 | 17 | | |
13 | 18 | | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
19 | 24 | | |
| 25 | + | |
20 | 26 | | |
21 | 27 | | |
22 | 28 | | |
23 | 29 | | |
24 | 30 | | |
25 | | - | |
26 | | - | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
27 | 34 | | |
28 | | - | |
| 35 | + | |
| 36 | + | |
29 | 37 | | |
30 | 38 | | |
31 | | - | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
0 commit comments