-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewStudents.java
More file actions
171 lines (153 loc) · 6.87 KB
/
Copy pathViewStudents.java
File metadata and controls
171 lines (153 loc) · 6.87 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
163
164
165
166
167
168
169
170
171
/*
* 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 javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.border.EmptyBorder;
public class ViewStudents extends JFrame {
public ViewStudents() {
setTitle("View Students");
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 BorderLayout());
panel.setLayout(new GridLayout(5, 2, 10, 10));
JLabel rollLabel = new JLabel("Roll Number:");
ComponentStyler.setLabelStyle(rollLabel);
JTextField rollField = new JTextField();
ComponentStyler.setTextFieldStyle(rollField);
panel.add(rollLabel);
panel.add(rollField);
JButton viewStudent = new JButton("View Student Details:");
JButton viewAllStudents = new JButton("View All Students");
panel.add(viewStudent);
panel.add(viewAllStudents);
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
}
});
panel.add(new JLabel());
JTable table = new JTable();
DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(new String[]{"Roll No", "Name", "Branch", "Year"});
table.setModel(model);
// Style the table
table.setFillsViewportHeight(true);
table.setRowHeight(30);
table.setFont(new Font("Arial", Font.PLAIN, 14));
table.setBackground(Color.WHITE);
table.setForeground(Color.BLACK);
table.setSelectionBackground(Color.LIGHT_GRAY);
table.setSelectionForeground(Color.BLACK);
// Style the table header
JTableHeader tableHeader = table.getTableHeader();
tableHeader.setFont(new Font("Arial", Font.BOLD, 16));
tableHeader.setBackground(Color.pink);
tableHeader.setForeground(Color.WHITE);
tableHeader.setBorder(new EmptyBorder(0, 0, 0, 0));
tableHeader.setReorderingAllowed(false);
// Center align table header text and make it bold
DefaultTableCellRenderer headerRenderer = (DefaultTableCellRenderer) tableHeader.getDefaultRenderer();
headerRenderer.setHorizontalAlignment(JLabel.CENTER);
headerRenderer.setFont(new Font("Arial", Font.BOLD, 16));
// Apply the custom header renderer to each column
for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
table.getColumnModel().getColumn(i).setHeaderRenderer(headerRenderer);
}
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane, BorderLayout.CENTER);
add(headerPanel, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
setVisible(true);
viewStudent.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String roll = rollField.getText();
viewStudentDetails(roll, model);
}
});
viewAllStudents.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fetchStudentRecords(model);
}
});
}
public void viewStudentDetails(String roll, DefaultTableModel model) {
model.setRowCount(0); // Clear previous data
try (Connection conn = getConnection()) {
String query = "SELECT rollno, name, branch, year FROM students WHERE rollno = ?";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1, roll);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
int rollNo = rs.getInt("rollno");
String name = rs.getString("name");
String branch = rs.getString("branch");
String year = rs.getString("year");
model.addRow(new Object[]{rollNo, name, branch, year});
} else {
JOptionPane.showMessageDialog(this, "Roll number not found.");
}
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error fetching student details.");
}
}
public void fetchStudentRecords(DefaultTableModel model) {
model.setRowCount(0); // Clear previous data
try (Connection conn = getConnection()) {
String query = "SELECT rollno, name, branch, year FROM students";
PreparedStatement pstmt = conn.prepareStatement(query);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
int rollNo = rs.getInt("rollno");
String name = rs.getString("name");
String branch = rs.getString("branch");
String year = rs.getString("year");
model.addRow(new Object[]{rollNo, name, branch, year});
}
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error fetching student records.");
}
}
private 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 static void main(String[] args) {
SwingUtilities.invokeLater(() -> new ViewStudents().setVisible(true));
}
}