-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCRUDCompleteDemo.java
More file actions
76 lines (60 loc) · 2.68 KB
/
CRUDCompleteDemo.java
File metadata and controls
76 lines (60 loc) · 2.68 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
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
71
72
73
74
75
76
import java.sql.*;
public class CRUDCompleteDemo {
public static void main(String[] args){
String url = "jdbc:postgresql://localhost:5432/demo";
String user = "postgres";
String password = "password";
try {
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println("Connected to the database successfully.");
Statement stmt = conn.createStatement();
String insertSql = """
INSERT INTO management (student_id, student_name, department, marks, aws_certified)
VALUES (8, 'Kylian Mbappe', 'CSE', 88, true)
""";
int rowsInserted = stmt.executeUpdate(insertSql);
System.out.println("Insertion in Progress.. | Rows Inserted: " + rowsInserted);
String selectSql = """
SELECT student_id, student_name, department, marks
FROM management WHERE student_id = 8
""";
ResultSet rs = stmt.executeQuery(selectSql);
while(rs.next()) {
System.out.println(
"Inserted Student ID: " + rs.getInt("student_id") +
", Name: " + rs.getString("student_name") +
", Dept: " + rs.getString("department") +
", Marks: " + rs.getInt("marks")
);
}
rs.close();
String updateSql = """
UPDATE management SET marks = 95 WHERE student_id = 8
""";
int rowsUpdated = stmt.executeUpdate(updateSql);
System.out.println("Updation in Progress.. | Rows Updated: " + rowsUpdated);
ResultSet rsAfterUpdate = stmt.executeQuery(selectSql);
while(rsAfterUpdate.next()) {
System.out.println(
"Updated Student ID: " + rsAfterUpdate.getInt("student_id") +
", Name: " + rsAfterUpdate.getString("student_name") +
", Dept: " + rsAfterUpdate.getString("department") +
", Marks: " + rsAfterUpdate.getInt("marks")
);
}
rsAfterUpdate.close();
String deleteSql = """
DELETE FROM management WHERE student_id = 8
""";
int rowsDeleted = stmt.executeUpdate(deleteSql);
System.out.println("Deletion in Progress.. | Rows Deleted: " + rowsDeleted);
stmt.close();
conn.close();
}
catch (Exception e) {
System.out.println("An error occurred:");
e.printStackTrace();
}
}
}