-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentUpdateApp.java
More file actions
162 lines (141 loc) · 5.67 KB
/
Copy pathStudentUpdateApp.java
File metadata and controls
162 lines (141 loc) · 5.67 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package studentmanagementsystem;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class StudentUpdateApp extends JFrame {
private JTextField idField;
private JTextField nameField;
private JTextField branchField;
private JTextField yearField;
public StudentUpdateApp() {
setTitle("Student Update App");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel headerPanel = new JPanel();
headerPanel.setBackground(Color.pink);
headerPanel.setPreferredSize(new Dimension(800, 50)); // Match frame width
headerPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); // Center components
JLabel headerLabel = new JLabel("Student Management System");
headerLabel.setFont(new Font("Arial", Font.BOLD, 20));
headerLabel.setForeground(Color.WHITE);
headerLabel.setHorizontalAlignment(SwingConstants.CENTER);
headerPanel.add(headerLabel);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 2,10,10));
JLabel id=new JLabel("Student ID:");
ComponentStyler.setLabelStyle(id);
panel.add(id);
idField = new JTextField();
ComponentStyler.setTextFieldStyle(idField);
panel.add(idField);
JLabel name=new JLabel("Name:");
ComponentStyler.setLabelStyle(name);
panel.add(name);
nameField = new JTextField();
ComponentStyler.setTextFieldStyle(nameField);
panel.add(nameField);
JLabel branch=new JLabel("Branch:");
ComponentStyler.setLabelStyle(branch);
panel.add(branch);
branchField = new JTextField();
ComponentStyler.setTextFieldStyle(branchField);
panel.add(branchField);
JLabel year=new JLabel("Year:");
ComponentStyler.setLabelStyle(year);
panel.add(year);
yearField = new JTextField();
ComponentStyler.setTextFieldStyle(yearField);
panel.add(yearField);
JButton backButton = new JButton("Back");
panel.add(backButton);
backButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new StudentManagementSystemGUI().setVisible(true);
dispose(); // Close the registration window
}
});
JButton updateButton = new JButton("Update");
panel.add(updateButton);
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateStudentDetails();
}
});
setVisible(true);
setLayout(new BorderLayout());
add(headerPanel, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
}
private void updateStudentDetails() {
String id = idField.getText();
String name = nameField.getText();
String branch = branchField.getText();
String year = yearField.getText();
String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&serverTimezone=UTC";
String user = "root";
String password = "root";
StringBuilder sqlBuilder = new StringBuilder("UPDATE students SET ");
boolean first = true;
if (!name.isEmpty()) {
sqlBuilder.append("name = ?");
first = false;
}
if (!branch.isEmpty()) {
if (!first) sqlBuilder.append(", ");
sqlBuilder.append("branch = ?");
first = false;
}
if (!year.isEmpty()) {
if (!first) sqlBuilder.append(", ");
sqlBuilder.append("year = ?");
}
sqlBuilder.append(" WHERE rollno = ?");
try (Connection conn = DriverManager.getConnection(url, user, password))
{
// Check if the ID exists in the database
String checkIdQuery = "SELECT COUNT(*) FROM students WHERE rollno = ?";
PreparedStatement checkIdStmt = conn.prepareStatement(checkIdQuery);
checkIdStmt.setInt(1, Integer.parseInt(id));
ResultSet rs = checkIdStmt.executeQuery();
rs.next();
int count = rs.getInt(1);
if (count == 0) {
JOptionPane.showMessageDialog(this, "Invalid ID. Student not found.");
return;
}
PreparedStatement pstmt = conn.prepareStatement(sqlBuilder.toString());
int paramIndex = 1;
if (!name.isEmpty()) {
pstmt.setString(paramIndex++, name);
}
if (!branch.isEmpty()) {
pstmt.setString(paramIndex++, branch);
}
if (!year.isEmpty()) {
pstmt.setString(paramIndex++, year);
}
pstmt.setInt(paramIndex, Integer.parseInt(id));
pstmt.executeUpdate();
JOptionPane.showMessageDialog(this, "Student details updated successfully!");
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error updating student details.");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new StudentUpdateApp());
}
}