-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPatientRegistrationForm.java
More file actions
347 lines (305 loc) · 14.2 KB
/
PatientRegistrationForm.java
File metadata and controls
347 lines (305 loc) · 14.2 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package hospital_management;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
public class PatientRegistrationForm extends JFrame {
JLabel ageLabel = new JLabel("Age:");
JTextField nameField;
private JComboBox<String> gender=new JComboBox();
private JTextField age;
private JTextField id;
private JTable table = new JTable();
private JTextField addressField;
private JTextField contactField;
private JButton registerButton;
private JButton update = new JButton("UPDATE");
private JButton clear = new JButton("CLEAR");
private JButton exit = new JButton("EXIT");
private JButton delete = new JButton("DELETE");
private JButton search = new JButton("SEARCH");
private JPanel p=new JPanel();
public PatientRegistrationForm() {
JLabel imageLabel = new JLabel();
// Load the image from a file
ImageIcon imageIcon = new ImageIcon("C://Users/Hirut Tarekegn/Desktop/javaproject1/download.jfif");
// Set the image icon to the JLabel
imageLabel.setIcon(imageIcon);
p.add(imageLabel);
p.setBackground(Color.LIGHT_GRAY);
table1();
JScrollPane scrollPane = new JScrollPane(table);
String[] sex={"Male","Female"};
JPanel formPanel = new JPanel(new GridLayout(0, 2));
gender=new JComboBox<>(sex);
JLabel patietnnum = new JLabel("patientNO:");
id=new JTextField();
JLabel nameLabel = new JLabel("Name:");
nameField = new JTextField();
JLabel genderLabel = new JLabel("Gender:");
age= new JTextField();
JLabel addressLabel = new JLabel("Address:");
addressField = new JTextField();
JLabel contactLabel = new JLabel("Contact:");
contactField = new JTextField();
formPanel.add(patietnnum);
formPanel.add(id);
formPanel.add(nameLabel);
formPanel.add(nameField);
formPanel.add(genderLabel);
formPanel.add(gender);
//formPanel.add(new JLabel()); // Empty label for spacing
// formPanel.add(femaleRadioButton);
formPanel.add(ageLabel);
formPanel.add(age);
Color c1=new Color(250,100,255);
formPanel.setBackground(c1);
formPanel.add(addressLabel);
formPanel.add(addressField);
formPanel.add(contactLabel);
formPanel.add(contactField);
autoID();
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
Color c=new Color(250,0,255);
buttonPanel.add(update);
buttonPanel.add(delete);
buttonPanel.add(clear);
buttonPanel.add(exit);
buttonPanel.add(search);
buttonPanel.setBackground(c);
registerButton = new JButton("Register");
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
int selectedRow = table.getSelectedRow();
if (selectedRow != -1) { // If a row is selected
DefaultTableModel model = (DefaultTableModel) table.getModel();
id.setText(model.getValueAt(selectedRow, 0).toString());
nameField.setText(model.getValueAt(selectedRow, 1).toString());
gender.setSelectedItem(model.getValueAt(selectedRow, 2));
age.setText(model.getValueAt(selectedRow, 3).toString());
addressField.setText(model.getValueAt(selectedRow, 4).toString());
contactField.setText(model.getValueAt(selectedRow, 5).toString());
}
}
});
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
registerPatient();
table1();
}
});
update.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
String idd=id.getText();
String name = nameField.getText();
String gend = (String) gender.getSelectedItem();
String aggge = age.getText();
String address = addressField.getText();
String contact = contactField.getText();
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/java_project", "root", "1234");
String sql="update patient set patient_Full_name='"+name+"', gender='"+gend+"', age='"+aggge+"', adrress='"+address+"',contact='"+contact+"' where patientNo='"+idd+"'";
PreparedStatement statement=con.prepareStatement(sql);
//Pst=conn.prepareStatement();
int rowsupdated= statement.executeUpdate(sql);
if(rowsupdated>0){
JOptionPane.showMessageDialog(null, "updated");
table1();
}else{
JOptionPane.showMessageDialog(null, "you didn't update any thing");}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(DoctorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}catch (SQLException ex) {
Logger.getLogger(PatientRegistrationForm.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
id.setText("");
nameField.setText("");
gender.setSelectedIndex(0);
age.setText("");
addressField.setText("");
contactField.setText("");
}
});
search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/java_project", "root", "1234");
PreparedStatement pst=con.prepareStatement("select * from patient where patientNo='"+id.getText()+"'");
// String sql="select * from login where S_id="+btn3.getText()+"'"+" and enrollment=extention";
Statement st = con.createStatement();
ResultSet rs=pst.executeQuery();
if(rs.next()){
String str="select * from doctor where patientNo='"+id.getText()+"'";
ResultSet result=st.executeQuery(str);
while(result.next()){
String Name=result.getString(2);
String agender=result.getString(3);
String agae=result.getString(4);
String address=result.getString(5);
String contact=result.getString(6);
nameField.setText("");
gender.setSelectedIndex(0);
age.setText("");
addressField.setText("");
contactField.setText("");
}
}
else {
JOptionPane.showMessageDialog(null, "this id is not found in the database");
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(DoctorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (SQLException ex) {
java.util.logging.Logger.getLogger(DoctorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
}
});
delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog(null,"Sure? You want to delete?", "Swing Tester",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(result == JOptionPane.YES_OPTION){
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/java_project", "root", "1234");
String sql="delete from patient where patientNo=?";
// preparedStatement Statement = con.createStatement();
PreparedStatement Statement=con.prepareStatement(sql);
Statement.setString(1,id.getText());
//pst.execute();
//JOptionPane.showMessageDialog(null,"Deleted");
int rowsdeleted= Statement.executeUpdate();
if(rowsdeleted> 0){
JOptionPane.showMessageDialog(null,"deleted successfully");
table1();
}else{
JOptionPane.showMessageDialog(null,"you deleted nothing");
}} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(DoctorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (SQLException ex) {
java.util.logging.Logger.getLogger(DoctorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
}else {
JOptionPane.showMessageDialog(null,"you deleted nothing"); }
}
});
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Recieption recieption = new Recieption();
setVisible(false);
}
});
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(formPanel, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
add(p,BorderLayout.PAGE_START);
add(mainPanel, BorderLayout.CENTER);
add(registerButton, BorderLayout.SOUTH);
add(scrollPane,BorderLayout.EAST);
setTitle("Patient Registration Form");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 550);
setLocationRelativeTo(null);
}
public void registerPatient() {
String idd=id.getText();
String name = nameField.getText();
String gend = (String) gender.getSelectedItem();
String aggge = age.getText();
String address = addressField.getText();
String contact = contactField.getText();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/java_project", "root", "1234");
Statement stmt=(Statement)con.createStatement();
String insert=("insert into patient VALUES('" + idd + "','" + name + "','" + gend + "','" +aggge+"','" + address+
"','" + contact + "')");
stmt.executeUpdate(insert);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null,"Patient number is duplicated give other");
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(DoctorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
JOptionPane.showMessageDialog(this, "Patient registered successfully!");
// Clear the form fields after registration
nameField.setText("");
//maleRadioButton.setSelected(true);
age.setText("");
addressField.setText("");
contactField.setText("");
}
public void table1() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/java_project", "root", "1234");
PreparedStatement pst = con.prepareStatement("SELECT * FROM patient");
ResultSet rs = pst.executeQuery();
DefaultTableModel model = new DefaultTableModel();
model.addColumn("patientNo");
model.addColumn("patient_Full_name");
model.addColumn("gender");
model.addColumn("age");
model.addColumn("adrress");
model.addColumn("contact");
while (rs.next()) {
Object[] rowData = new Object[6];
rowData[0] = rs.getString("patientNo");
rowData[1] = rs.getString("patient_Full_name");
rowData[2] = rs.getString("gender");
rowData[3] = rs.getString("age");
rowData[4] = rs.getString("adrress");
rowData[5] = rs.getString("contact");
model.addRow(rowData);
}
table.setModel(model);
rs.close();
pst.close();
con.close();
} catch (ClassNotFoundException | SQLException ex) {
ex.printStackTrace();
}
}
public void autoID() {
try {
// Load the MySQL JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Establish a connection to the database
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/java_project", "root", "1234");
// Prepare a statement to retrieve the maximum patientNo from the patient table
PreparedStatement pst = con.prepareStatement("SELECT MAX(patientNo) FROM patient");
// Execute the query and retrieve the result set
ResultSet rs = pst.executeQuery();
rs.next();
// Get the maximum patientNo value from the result set
String maxPatientNo = rs.getString("MAX(patientNo)");
// Check if the maximum patientNo is null
if (maxPatientNo == null) {
// f it's null, set the ID to "ps001"
id.setText("ps001");
} else {
// Extract the numeric portion of the maximum patientNo and increment it
long n = Long.parseLong(maxPatientNo.substring(2));
n++;
// Format the incremented number as a three-digit string and set the ID
id.setText("ps" + String.format("%03d", n));
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(PatientRegistrationForm.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(PatientRegistrationForm.class.getName()).log(Level.SEVERE, null, ex);
}
}
}