1
+ package com .StudentManagementCRUD ;
2
+
3
+ import java .sql .Connection ;
4
+ import java .sql .PreparedStatement ;
5
+ import java .sql .ResultSet ;
6
+ import java .sql .SQLException ;
7
+ import java .sql .Statement ;
8
+
9
+ public class StudentOp {
10
+
11
+ public void addStudent (int studentId , String studentName , int studentAge , double sgpa , String phNumber ) {
12
+ try (Connection conn = DatabaseConnection .getConnection ()) {
13
+ String query = "INSERT INTO Students (studentId, studentName, studentAge, sgpa, phNumber) VALUES (?, ?, ?, ?, ?)" ;
14
+ PreparedStatement stmt = conn .prepareStatement (query );
15
+ stmt .setInt (1 , studentId );
16
+ stmt .setString (2 , studentName );
17
+ stmt .setInt (3 , studentAge );
18
+ stmt .setDouble (4 , sgpa );
19
+ stmt .setString (5 , phNumber );
20
+
21
+ stmt .executeUpdate ();
22
+ } catch (SQLException e ) {
23
+ e .printStackTrace ();
24
+ }
25
+ }
26
+
27
+ public void displayStudents () {
28
+ try (Connection conn = DatabaseConnection .getConnection ()) {
29
+ String query = "SELECT * FROM Students" ;
30
+ Statement stmt = conn .createStatement ();
31
+ ResultSet rs = stmt .executeQuery (query );
32
+
33
+ while (rs .next ()) {
34
+ System .out .println ("ID: " + rs .getInt ("studentId" ));
35
+ System .out .println ("Name: " + rs .getString ("studentName" ));
36
+ System .out .println ("Age: " + rs .getInt ("studentAge" ));
37
+ System .out .println ("CGPA: " + rs .getDouble ("sgpa" ));
38
+ System .out .println ("Phone Number: " + rs .getString ("phNumber" ));
39
+ System .out .println ("-------------------------" );
40
+ }
41
+ } catch (SQLException e ) {
42
+ e .printStackTrace ();
43
+ }
44
+ }
45
+
46
+ public void findStudent (int searchId ) {
47
+ try (Connection conn = DatabaseConnection .getConnection ()) {
48
+ String query = "SELECT * FROM Students WHERE studentId = ?" ;
49
+ PreparedStatement stmt = conn .prepareStatement (query );
50
+ stmt .setInt (1 , searchId );
51
+
52
+ ResultSet rs = stmt .executeQuery ();
53
+ if (rs .next ()) {
54
+ System .out .println ("ID: " + rs .getInt ("studentId" ));
55
+ System .out .println ("Name: " + rs .getString ("studentName" ));
56
+ System .out .println ("Age: " + rs .getInt ("studentAge" ));
57
+ System .out .println ("CGPA: " + rs .getDouble ("sgpa" ));
58
+ System .out .println ("Phone Number: " + rs .getString ("phNumber" ));
59
+ } else {
60
+ System .out .println ("Student not found" );
61
+ }
62
+ } catch (SQLException e ) {
63
+ e .printStackTrace ();
64
+ }
65
+ }
66
+
67
+ public void removeStudnet (int removeId ) {
68
+ try (Connection conn = DatabaseConnection .getConnection ()) {
69
+ String query = "DELETE FROM Students WHERE studentId = ?" ;
70
+ PreparedStatement stmt = conn .prepareStatement (query );
71
+ stmt .setInt (1 , removeId );
72
+
73
+ int rowsAffected = stmt .executeUpdate ();
74
+ if (rowsAffected > 0 ) {
75
+ System .out .println ("Student removed successfully." );
76
+ } else {
77
+ System .out .println ("Student not found." );
78
+ }
79
+ } catch (SQLException e ) {
80
+ e .printStackTrace ();
81
+ }
82
+ }
83
+
84
+ public void updateStudent (int studentId , String newName , int newAge , double newSgpa , String newPhoneNumber ) {
85
+ try (Connection conn = DatabaseConnection .getConnection ()) {
86
+ String query = "UPDATE Students SET studentName = ?, studentAge = ?, sgpa = ?, phNumber = ? WHERE studentId = ?" ;
87
+ PreparedStatement stmt = conn .prepareStatement (query );
88
+
89
+ stmt .setString (1 , newName ); // New Name
90
+ stmt .setInt (2 , newAge ); // New Age
91
+ stmt .setDouble (3 , newSgpa ); // New SGPA
92
+ stmt .setString (4 , newPhoneNumber ); // New Phone Number
93
+ stmt .setInt (5 , studentId ); // Where studentId matches
94
+
95
+ int rowsAffected = stmt .executeUpdate ();
96
+ if (rowsAffected > 0 ) {
97
+ System .out .println ("Student details updated successfully." );
98
+ } else {
99
+ System .out .println ("Student not found." );
100
+ }
101
+ } catch (SQLException e ) {
102
+ e .printStackTrace ();
103
+ }
104
+ }
105
+ }
0 commit comments