-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_students.java
More file actions
37 lines (29 loc) · 1.25 KB
/
Copy pathinsert_students.java
File metadata and controls
37 lines (29 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.sql.Connection;
import java.sql.PreparedStatement;
public class insert_students {
public static void insertStudent(String name, int age, String email) {
// the only place where the table name matters
String sql = "INSERT INTO students (name, age, email) VALUES (?, ?, ?)";
try (Connection conn = Making_Connection.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, name);
stmt.setInt(2, age);
stmt.setString(3, email);
int rows = stmt.executeUpdate();
System.out.println(rows + " student inserted successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
// Optional: testing the insert
public static void main(String[] args) {
insertStudent("Alice", 20, "alice@example.com");
insertStudent("Kim", 26, "kim@example.com");
insertStudent("John", 30, "John@example.com");
insertStudent("Jack", 25, "Jack@example.com");
insertStudent("Sarah", 34, "Sarah@example.com");
insertStudent("Sam", 23, "Sam@example.com");
insertStudent("Harry", 24, "Harry@example.com");
insertStudent("Mia", 27, "Mia@example.com");
}
}