-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateStudent.java
More file actions
132 lines (116 loc) · 4.98 KB
/
Copy pathupdateStudent.java
File metadata and controls
132 lines (116 loc) · 4.98 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
/*
* 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.*;
public class updateStudent extends JFrame {
private JComboBox<String> fieldComboBox;
private JTextField idField, valueField;
private JButton updateButton;
public Connection getConnection() throws SQLException {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&serverTimezone=UTC";
String username = "root";
String password = "root";
return DriverManager.getConnection(url, username, password);
}
public updateStudent() {
setTitle("Update Student");
setSize(900, 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(4, 2, 10, 10));
JLabel updateid = new JLabel("Enter The Student RollNo To Update:");
ComponentStyler.setLabelStyle(updateid);
panel.add(updateid);
idField = new JTextField();
panel.add(idField);
ComponentStyler.setTextFieldStyle(idField);
JLabel updateLabel = new JLabel("Field to Update:");
updateLabel.setFont(new Font("Arial", Font.BOLD, 20));
panel.add(updateLabel);
String[] s = new String[]{"Name", "Branch", "Year"};
fieldComboBox = new JComboBox<>(s);
// Set custom renderer to change font size
fieldComboBox.setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
label.setFont(new Font("Arial", Font.PLAIN, 20)); // Change the font size here
return label;
}
});
panel.add(fieldComboBox);
JLabel newLabel = new JLabel("New Value:");
newLabel.setFont(new Font("Arial", Font.BOLD, 20));
panel.add(newLabel);
valueField = new JTextField();
panel.add(valueField);
ComponentStyler.setTextFieldStyle(valueField);
JButton backButton = new JButton("Back");
panel.add(backButton);
updateButton = new JButton("Update");
panel.add(updateButton);
backButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new StudentManagementSystemGUI().setVisible(true);
dispose(); // Close the registration window
}
});
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateStudentRecord();
}
});
add(headerPanel, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
}
private void updateStudentRecord() {
int id = Integer.parseInt(idField.getText());
String selectedField = fieldComboBox.getSelectedItem().toString().toLowerCase();
String newValue = valueField.getText();
try (Connection conn = getConnection()) {
String query = "UPDATE students SET " + selectedField + " = ? WHERE rollno = ?";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1, newValue);
pstmt.setInt(2, id);
int rowsAffected = pstmt.executeUpdate();
if (rowsAffected > 0) {
JOptionPane.showMessageDialog(this, "Student record updated successfully.");
} else {
JOptionPane.showMessageDialog(this, "Student ID not found.");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new updateStudent().setVisible(true);
}
});
}
}