Skip to content

Commit 0bace29

Browse files
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

File tree

1 file changed

+52
-11
lines changed

1 file changed

+52
-11
lines changed

Section28JDBCusingSQLite/DMLUsingJDBC/src/Database.java

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,65 @@ public class Database {
66
public static void main(String[] args) throws Exception {
77
Class.forName("org.sqlite.JDBC");
88

9-
Connection con = DriverManager.getConnection("jdbc:sqlite:C:/sqlite/univ.db");
9+
Connection con = DriverManager.getConnection("jdbc:sqlite:/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section28JDBCusingSQLite/univ.db\"");
1010

1111
Statement stm = con.createStatement();
1212

13-
//Inserting a value
14-
//If you run again then it say, PRIMARY KEY constraint failed (UNIQUE constraint failed: dept.deptno).
15-
//Primary key is Unique and cannot be null.
16-
//stm.executeUpdate("insert into dept values(60,'Chem added using JDBC')");
13+
/*
14+
Inserting a value:
15+
If you run again then it say, PRIMARY KEY constraint failed (UNIQUE constraint failed: dept.deptno).
16+
The primary key is Unique and cannot be null.
17+
stm.executeUpdate("insert into dept values(60,'Chem added using JDBC')");
1718
18-
//stm.executeUpdate("insert into dept values(70,'DML Done Using Insert Query')");
19+
stm.executeUpdate("insert into dept values(70,'DML Done Using Insert Query')");
1920
20-
//Perform delete operation
21-
//stm.executeUpdate("DELETE FROM dept WHERE deptno >= 60");
21+
Perform delete operation
22+
stm.executeUpdate("DELETE FROM dept WHERE deptno >= 60");
2223
23-
//Update into the database.
24-
//stm.executeUpdate("update dept set dnmae = 'Ai set using JDBC' where deptno=50");
24+
Update into the database.
25+
stm.executeUpdate("update dept set dnmae = 'Ai set using JDBC' where deptno=50");
26+
*/
2527

2628
stm.close();
2729
con.close();
2830
}
29-
}
31+
}
32+
33+
/*
34+
1. JDBC Setup:
35+
- `Class.forName("org.sqlite.JDBC");`
36+
→ SQLite JDBC driver ko load karta hai.
37+
- `DriverManager.getConnection("jdbc:sqlite:.../univ.db")`
38+
→ SQLite database file `univ.db` ke saath connection establish karta hai.
39+
→ Agar file exist nahi karti toh SQLite automatically bana deta hai.
40+
41+
2. Statement Object:
42+
- `Statement stm = con.createStatement();`
43+
→ Isse SQL commands (INSERT, DELETE, UPDATE, SELECT) execute kar sakte ho.
44+
45+
3. Insert Example:
46+
- `stm.executeUpdate("insert into dept values(60,'Chem added using JDBC')");`
47+
→ `dept` table me ek naya record add karega.
48+
- Agar firse wahi primary key (deptno = 60) insert karoge →
49+
PRIMARY KEY constraint failed (UNIQUE constraint failed) error aayega.
50+
- Reason: Primary key hamesha unique aur non-null hoti hai.
51+
52+
4. Delete Example:
53+
- `stm.executeUpdate("DELETE FROM dept WHERE deptno >= 60");`
54+
→ `deptno` 60 ya usse upar ke saare rows delete kar dega.
55+
56+
5. Update Example:
57+
- `stm.executeUpdate("update dept set dname = 'AI set using JDBC' where deptno=50");`
58+
→ Dept number 50 wale record ka naam update ho jayega.
59+
60+
6. Close Resources:
61+
- `stm.close(); con.close();`
62+
→ Database resources release karna important hai.
63+
64+
65+
✔ INSERT → naye records add karne ke liye.
66+
✔ DELETE → existing records remove karne ke liye.
67+
✔ UPDATE → existing records modify karne ke liye.
68+
✔ PRIMARY KEY constraint → duplicate values allow nahi karega.
69+
✔ Always close connection after queries.
70+
*/

0 commit comments

Comments
 (0)