-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprescription.java
279 lines (173 loc) · 6.71 KB
/
prescription.java
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class prescription extends JFrame implements ActionListener, ListSelectionListener {
private static final long serialVersionUID = 1L;
JTable options = null;
int counter = 0;
ResultSet rs = null;
Object [][] opt = null;
String appID = null;
String dbQuery = null;
String patient = null;
JPanel first = null;
JTextField medTF = null;
int catA = 0;
int catB = 0;
int catC = 0;
public prescription(String id){
appID = id;
gettingPatient();
this.setSize(500,600); // This is the frame, the numbers are the size of the windows
this.setTitle("Presciptions"); // Setting the title of the window
this.setVisible(true); // We're saying to the window to show itself
this.setLayout(new GridLayout(3,0));
first = new JPanel();
this.add(first);
drawingTable();
JPanel second = new JPanel();
this.add(second);
JLabel medLabel = new JLabel("Medication");
second.add(medLabel);
medTF = new JTextField(15);
second.add(medTF);
JPanel third = new JPanel();
this.add(third);
JButton prescribe = new JButton("Prescribe");
third.add(prescribe);
prescribe.addActionListener(this);
prescribe.setActionCommand("prescribe");
JButton delete = new JButton("Delete");
third.add(delete);
delete.addActionListener(this);
delete.setActionCommand("delete");
JButton save = new JButton("Save");
third.add(save);
save.addActionListener(this);
save.setActionCommand("save");
validate();
repaint();
}
public void storingOptions(){
opt = new Object [100][3];
try{
int rowCounter = 0;
while(rs.next()){
String id = rs.getString("ID_Presc");
String date = rs.getString("Date");
String medication = rs.getString("Medication");
opt [rowCounter][0] = id;
opt [rowCounter][1] = date;
opt [rowCounter][2] = medication;
if (medication.equals("VND1") || medication.equals("XXV2") || medication.equals("HNF232") || medication.equals("GB334")){
catA++;
}
else if (medication.equals("X34") || medication.equals("HH5") || medication.equals("DDF23") || medication.equals("JHH7")){
catB++;
}
else if (medication.equals("543H") || medication.equals("344BB") || medication.equals("JUY9") || medication.equals("232B")){
catC++;
}
rowCounter ++;
}
}
catch(SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
public void alert(){
if (catA >= 2){
JOptionPane.showMessageDialog(this, "Be careful as two or more medicines from GROUP A have been prescribed to this patient");
}
if (catB >= 2){
JOptionPane.showMessageDialog(this, "Be careful as two or more medicines from GROUP B have been prescribed to this patient");
}
if (catC >= 2){
JOptionPane.showMessageDialog(this, "Be careful as two or more medicines from GROUP C have been prescribed to this patient");
}
catA = 0;
catB = 0;
catC = 0;
}
public void gettingPatient(){
dbQuery = "SELECT * FROM `surgery`.`prescriptions` INNER JOIN `surgery`.`appointments` ON prescriptions.Consultation = appointments.ID_App WHERE ID_App = "+appID+";";
database DB = new database ();
rs = (ResultSet) DB.accessDB(dbQuery);
try{
while(rs.next()){
patient = rs.getString("Patient");
}
}
catch(SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
public void gettingMedication(){
dbQuery = "SELECT * FROM `surgery`.`prescriptions` INNER JOIN `surgery`.`appointments` ON prescriptions.Consultation = appointments.ID_App INNER JOIN `surgery`.`patients` ON appointments.Patient = patients.ID_Pat WHERE ID_Pat = "+patient+";";
database DB = new database ();
rs = (ResultSet) DB.accessDB(dbQuery);
storingOptions();
}
public void drawingTable(){
first.removeAll();
gettingMedication();
String[] columnNames = {"ID","Date","Medication"};
options = new JTable(opt, columnNames);
options.getSelectionModel().addListSelectionListener(this);
JScrollPane js = new JScrollPane(options);
first.add(js);
alert();
this.validate();
this.repaint();
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("prescribe")){
String med = medTF.getText();
dbQuery = "INSERT INTO `surgery`.`prescriptions` (`Consultation`, `Medication`) VALUES ('"+appID+"', '"+med+"');";
database DB = new database ();
rs = (ResultSet) DB.accessDB(dbQuery);
drawingTable();
}
else if (e.getActionCommand().equals("delete")){
String id = (String) options.getValueAt(options.getSelectedRow(), 0);
dbQuery = "DELETE FROM `surgery`.`prescriptions` WHERE `ID_Presc`= "+id+";";
database DB = new database ();
rs = (ResultSet) DB.accessDB(dbQuery);
drawingTable();
}
else if (e.getActionCommand().equals("save")){
String id = (String) options.getValueAt(options.getSelectedRow(), 0);
String med = medTF.getText();
dbQuery = "UPDATE `surgery`.`prescriptions` SET `Medication` = '"+med+"' WHERE `ID_Presc`="+id+";";
database DB = new database ();
rs = (ResultSet) DB.accessDB(dbQuery);
drawingTable();
}
}
public void valueChanged(ListSelectionEvent arg0) {
counter ++;
if (counter == 2){
String med = (String) options.getValueAt(options.getSelectedRow(), 2);
medTF.setText(med);
counter = 0;
}
}
}