This repository was archived by the owner on Jul 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent.java
More file actions
229 lines (195 loc) · 7.7 KB
/
Copy pathStudent.java
File metadata and controls
229 lines (195 loc) · 7.7 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// package tec181;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
public class Student extends JFrame {
static final int EVENTS = 3;
private JPanel contentPane;
public Student() {
JTextArea[] idArea = new JTextArea[EVENTS];
JTextArea[] titleArea = new JTextArea[EVENTS];
JTextArea[] descriptionArea = new JTextArea[EVENTS];
Connection conn = Database.dbConnect();
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 851, 425);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setBounds(5, 5, 835, 386);
contentPane.add(layeredPane);
layeredPane.setLayout(null);
JLabel ongEvent = new JLabel("Ongoing Events");
ongEvent.setBounds(0, 11, 252, 53);
layeredPane.add(ongEvent);
ongEvent.setFont(new Font("Simplex", Font.BOLD, 25));
ongEvent.setHorizontalAlignment(SwingConstants.CENTER);
JPanel panel = new JPanel();
panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
panel.setBounds(10, 56, 614, 319);
layeredPane.add(panel);
panel.setLayout(null);
JScrollBar scrollBar = new JScrollBar();
scrollBar.setBounds(597, 0, 17, 319);
panel.add(scrollBar);
idArea[1] = new JTextArea();
idArea[1].setEditable(false);
idArea[1].setFont(new Font("Monospaced", Font.BOLD, 20));
idArea[1].setBounds(10, 11, 35, 32);
panel.add(idArea[1]);
titleArea[1] = new JTextArea();
titleArea[1].setFont(new Font("Microsoft YaHei UI", Font.BOLD, 15));
titleArea[1].setEditable(false);
titleArea[1].setBounds(62, 11, 284, 32);
panel.add(titleArea[1]);
descriptionArea[1] = new JTextArea();
descriptionArea[1].setEditable(false);
descriptionArea[1].setBounds(10, 54, 413, 84);
panel.add(descriptionArea[1]);
JButton applyButton_1 = new JButton("Apply");
applyButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
PreparedStatement pst = conn.prepareStatement("SELECT * FROM `applied` WHERE userName=? AND title=?");
pst.setString(1, Login.login);
pst.setString(2, titleArea[1].getText());
if(titleArea[1].getText().length() == 0) {
JOptionPane.showMessageDialog(null, "There is no event! You can not apply.", "No Event", JOptionPane.ERROR_MESSAGE);
} else {
ResultSet rs = pst.executeQuery();
if(rs.next()) {
JOptionPane.showMessageDialog(null, "You already applied for this course.", "Duplicate Entry", JOptionPane.ERROR_MESSAGE);
} else {
pst = conn.prepareStatement("INSERT INTO applied(userName, title) VALUES(?, ?)");
pst.setString(1, Login.login);
pst.setString(2, titleArea[1].getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(null, Login.login + " sucessfully apllied for " + titleArea[1].getText(), "Conformation", JOptionPane.INFORMATION_MESSAGE);
}
}
pst.close();
} catch(SQLException e1) {
System.out.println(e1.getMessage());
}
}
});
applyButton_1.setBounds(461, 55, 89, 23);
panel.add(applyButton_1);
idArea[2] = new JTextArea();
idArea[2].setEditable(false);
idArea[2].setFont(new Font("Monospaced", Font.BOLD, 20));
idArea[2].setBounds(10, 149, 35, 32);
panel.add(idArea[2]);
titleArea[2] = new JTextArea();
titleArea[2].setFont(new Font("Microsoft YaHei UI", Font.BOLD, 15));
titleArea[2].setEditable(false);
titleArea[2].setBounds(62, 149, 284, 32);
panel.add(titleArea[2]);
descriptionArea[2] = new JTextArea();
descriptionArea[2].setEditable(false);
descriptionArea[2].setBounds(10, 192, 413, 84);
panel.add(descriptionArea[2]);
JButton applyButton_2 = new JButton("Apply");
applyButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
PreparedStatement pst = conn.prepareStatement("SELECT * FROM `applied` WHERE userName=? AND title=?");
pst.setString(1, Login.login);
pst.setString(2, titleArea[2].getText());
if(titleArea[2].getText().length() == 0) {
JOptionPane.showMessageDialog(null, "There is no event! You can not apply.", "No Event", JOptionPane.ERROR_MESSAGE);
} else {
ResultSet rs = pst.executeQuery();
if(rs.next()) {
JOptionPane.showMessageDialog(null, "You already applied for this course.", "", JOptionPane.ERROR_MESSAGE);
} else {
pst = conn.prepareStatement("INSERT INTO applied(userName, title) VALUES(?, ?)");
pst.setString(1, Login.login);
pst.setString(2, titleArea[2].getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(null, Login.login + " Sucessfully apllied for " + titleArea[2].getText(), "Conformation", JOptionPane.INFORMATION_MESSAGE);
}
}
pst.close();
} catch(SQLException e1) {
System.out.println(e1.getMessage());
}
}
});
applyButton_2.setBounds(461, 182, 89, 23);
panel.add(applyButton_2);
JLabel admin = new JLabel("Student");
admin.setHorizontalAlignment(SwingConstants.CENTER);
admin.setBounds(658, 11, 145, 53);
admin.setFont(new Font("Simplex", Font.BOLD, 25));
layeredPane.add(admin);
JLabel studentPortal = new JLabel("Portal");
studentPortal.setHorizontalAlignment(SwingConstants.CENTER);
studentPortal.setBounds(668, 56, 116, 38);
studentPortal.setFont(new Font("Simplex", Font.BOLD, 25));
layeredPane.add(studentPortal);
JButton logoutButton = new JButton("Logout ");
logoutButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
MainWindow frame = new MainWindow();
frame.setVisible(true);
dispose();
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
logoutButton.setFont(new Font("Segoe UI Semibold", Font.BOLD, 13));
logoutButton.setBounds(680, 291, 89, 23);
layeredPane.add(logoutButton);
JButton exitButton = new JButton("Exit");
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
exitButton.setFont(new Font("Segoe UI Semibold", Font.BOLD, 13));
exitButton.setBounds(680, 336, 89, 23);
layeredPane.add(exitButton);
// get events from database
try {
PreparedStatement pst = conn.prepareStatement("SELECT * FROM `events`");
ResultSet rs = pst.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
for(int i=1; i<=count; i++) {
pst = conn.prepareStatement("SELECT * FROM `events` WHERE id=?");
pst.setInt(1, i);
rs = pst.executeQuery();
while(rs.next()) {
idArea[i].setText(rs.getString("id"));
titleArea[i].setText(rs.getString("title"));
descriptionArea[i].setText(rs.getString("decription"));
}
pst.close();
}
rs.close();
} catch(SQLException e) {
System.out.println(e.getMessage());
}
}
}