Skip to content

Commit aae09f8

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

1 file changed

+54
-11
lines changed

Section28JDBCusingSQLite/DMLUsingJDBC/src/InsertusingPreparedtable.java

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,70 @@ public class InsertusingPreparedtable {
55
public static void main(String[] args) throws SQLException, ClassNotFoundException {
66
Class.forName("org.sqlite.JDBC");
77

8-
Connection con = DriverManager.getConnection("jdbc:sqlite:C:/sqlite/univ.db");
8+
// Use your actual macOS path to univ.db
9+
String url = "jdbc:sqlite:/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section28JDBCusingSQLite/univ.db";
10+
Connection con = DriverManager.getConnection(url);
911

10-
PreparedStatement stm = con.prepareStatement("insert into students values(?,?,?,?)");
12+
// Prepared statement for inserting into students.
13+
PreparedStatement stm = con.prepareStatement(
14+
"INSERT INTO students (roll, name, city, deptno) VALUES (?, ?, ?, ?)"
15+
);
1116

1217
Scanner sc = new Scanner(System.in);
1318

14-
System.out.println("Enter a values to insert into the student data: ");
15-
int r = sc.nextInt();
16-
String name = sc.next();
17-
String city = sc.next();
18-
int dno = sc.nextInt();
19+
System.out.println("Enter values to insert into the student table (roll name city deptno):");
20+
int r = sc.nextInt(); // roll number
21+
String name = sc.next(); // name
22+
String city = sc.next(); // city
23+
int dno = sc.nextInt(); // department number
1924

25+
// Bind parameters
2026
stm.setInt(1, r);
2127
stm.setString(2, name);
2228
stm.setString(3, city);
2329
stm.setInt(4, dno);
2430

25-
stm.executeUpdate();
26-
//It is useful we change the data anf executed many times.
31+
// Execute
32+
int rows = stm.executeUpdate();
33+
System.out.println(rows + " row(s) inserted successfully.");
2734

28-
stm.close();;
35+
// Cleanup
36+
stm.close();
2937
con.close();
3038
}
31-
}
39+
}
40+
41+
/*
42+
1. Classes / Setup:
43+
- `Class.forName("org.sqlite.JDBC");` → SQLite JDBC driver load karta hai.
44+
- `DriverManager.getConnection("jdbc:sqlite:.../univ.db");` → SQLite database `univ.db` ke saath connection banata hai.
45+
46+
2. PreparedStatement:
47+
- Query: `"INSERT INTO students (roll, name, city, deptno) VALUES (?, ?, ?, ?)"`.
48+
- `?` placeholders use hote hain jahan values runtime pe bind ki jaati hain.
49+
- Benefits:
50+
✔ Prevents SQL Injection.
51+
✔ Query pre-compiled hoti hai → fast execution.
52+
✔ Reusable with different values.
53+
54+
3. Logic:
55+
- `Scanner` se user input liya jata hai → roll, name, city, deptno.
56+
- `stm.setInt(1, r);` → pehle placeholder ko roll assign hota hai.
57+
- `stm.setString(2, name);` → doosra placeholder = name.
58+
- `stm.setString(3, city);` → teesra placeholder = city.
59+
- `stm.setInt(4, dno);` → chautha placeholder = deptno.
60+
- `executeUpdate()` query ko run karta hai aur number of affected rows return karta hai.
61+
62+
4. Main Method Flow:
63+
- Connection open hota hai.
64+
- User input binds hota hai PreparedStatement me.
65+
- an Insert query executes hoti hai.
66+
- Console pe output: `"1 row(s) inserted successfully."`
67+
- Finally `stm.close(); con.close();` resources free kar diye jaate hain.
68+
69+
✔ `PreparedStatement` = safer + faster alternative to `Statement`.
70+
✔ `?` = placeholders jo runtime pe bind hote hain.
71+
✔ SQL Injection se protection milta hai.
72+
✔ `executeUpdate()` DML (INSERT/UPDATE/DELETE) ke liye hota hai.
73+
✔ Hamesha resources close karna zaroori hai (best practice).
74+
*/

0 commit comments

Comments
 (0)