Commit 0bace29
committed
feat(jdbc-sqlite): add DML operations (INSERT, DELETE, UPDATE) in Database example
What
- Enhanced `Database.java` under Section28JDBCusingSQLite with Data Manipulation Language (DML) examples.
- Demonstrates how to perform:
- `INSERT` → add new department records into `dept` table.
- `DELETE` → remove rows based on condition (`deptno >= 60`).
- `UPDATE` → modify existing row values (e.g., update department name for `deptno=50`).
- Added inline comments explaining constraints (e.g., PRIMARY KEY uniqueness).
Why
- Provides hands-on examples of executing SQL write operations using JDBC.
- Highlights the importance of constraints like `PRIMARY KEY` and error handling.
- Shows how Java applications interact with relational databases to manipulate data.
How
- Setup:
- Loaded SQLite JDBC driver with `Class.forName("org.sqlite.JDBC")`.
- Established connection via `DriverManager.getConnection("jdbc:sqlite:.../univ.db")`.
- Execution:
- Created `Statement` object to run SQL commands.
- Used `executeUpdate()` for INSERT, DELETE, and UPDATE.
- Resource management:
- Closed `Statement` and `Connection` to free resources.
Key Notes
- Running INSERT with duplicate primary key values results in `PRIMARY KEY constraint failed`.
- DELETE operation removes rows permanently; always test with WHERE conditions.
- UPDATE modifies specific records; ensure WHERE clause is accurate to avoid accidental bulk updates.
- JDBC auto-creates database file if it does not exist in the given path.
Real-life Applications
- Enterprise apps: employee/department management systems.
- Inventory management: inserting, updating, and removing stock entries.
- Banking apps: updating balances, deleting old records, inserting new transactions.
- Education systems: maintaining student and department records.
Future Improvements
- Switch to `PreparedStatement` to prevent SQL injection and improve performance.
- Add transaction management (`commit`, `rollback`) for multi-step operations.
- Implement try-with-resources for automatic resource closing.
- Extend example with `SELECT` queries to validate DML results.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 41d516b commit 0bace29
1 file changed
+52
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
17 | 18 | | |
18 | | - | |
| 19 | + | |
19 | 20 | | |
20 | | - | |
21 | | - | |
| 21 | + | |
| 22 | + | |
22 | 23 | | |
23 | | - | |
24 | | - | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
25 | 27 | | |
26 | 28 | | |
27 | 29 | | |
28 | 30 | | |
29 | | - | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 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 | + | |
0 commit comments