-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHospitalManagementSystem.java
More file actions
222 lines (190 loc) · 8.45 KB
/
HospitalManagementSystem.java
File metadata and controls
222 lines (190 loc) · 8.45 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
import java.awt.*;
import java.sql.*;
import javax.swing.*;
public class HospitalManagementSystem extends JFrame {
private JTextField txtName, txtAge, txtAddress, txtContactNo, txtEmail, txtQualifications, txtTime;
private JComboBox<String> cmbGender, cmbBloodGroup, cmbPatient, cmbDoctor;
public HospitalManagementSystem() {
setTitle("Hospital Management System");
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(0, 2));
// Patient Form
add(new JLabel("Name:"));
txtName = new JTextField();
add(txtName);
add(new JLabel("Age:"));
txtAge = new JTextField();
add(txtAge);
add(new JLabel("Gender:"));
cmbGender = new JComboBox<>(new String[]{"Male", "Female"});
add(cmbGender);
add(new JLabel("Address:"));
txtAddress = new JTextField();
add(txtAddress);
add(new JLabel("Contact No:"));
txtContactNo = new JTextField();
add(txtContactNo);
add(new JLabel("Email:"));
txtEmail = new JTextField();
add(txtEmail);
JButton btnAddPatient = new JButton("Add Patient");
btnAddPatient.addActionListener(e -> addPatient());
add(btnAddPatient);
// Doctor Form
add(new JLabel("Doctor's Name:"));
txtQualifications = new JTextField();
add(txtQualifications);
add(new JLabel("Qualifications:"));
JTextField txtDoctorQualifications = new JTextField(); // Separate qualifications field
add(txtDoctorQualifications);
add(new JLabel("Blood Group:"));
cmbBloodGroup = new JComboBox<>(new String[]{"A+", "A-", "B+", "B-", "O+", "O-", "AB+", "AB-"});
add(cmbBloodGroup);
JButton btnAddDoctor = new JButton("Add Doctor");
btnAddDoctor.addActionListener(e -> addDoctor(txtDoctorQualifications.getText()));
add(btnAddDoctor);
// Appointment Form
add(new JLabel("Patient:"));
cmbPatient = new JComboBox<>();
loadPatients();
add(cmbPatient);
add(new JLabel("Doctor:"));
cmbDoctor = new JComboBox<>();
loadDoctors();
add(cmbDoctor);
add(new JLabel("Appointment Time:"));
txtTime = new JTextField();
add(txtTime);
JButton btnAddAppointment = new JButton("Add Appointment");
btnAddAppointment.addActionListener(e -> addAppointment());
add(btnAddAppointment);
setLocationRelativeTo(null);
}
private void addPatient() {
String name = txtName.getText().trim();
String ageStr = txtAge.getText().trim();
String contactNo = txtContactNo.getText().trim();
String email = txtEmail.getText().trim();
if (name.isEmpty() || ageStr.isEmpty() || contactNo.isEmpty() || email.isEmpty()) {
JOptionPane.showMessageDialog(this, "All fields must be filled out.");
return;
}
int age;
try {
age = Integer.parseInt(ageStr);
if (age <= 0) throw new NumberFormatException();
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "Please enter a valid age.");
return;
}
if (!isValidEmail(email)) {
JOptionPane.showMessageDialog(this, "Please enter a valid email address.");
return;
}
try (Connection con = Connect.ConnectDB()) {
String sql = "INSERT INTO Patient (PatientName, Age, Gender, Address, ContactNo, Email) VALUES (?, ?, ?, ?, ?, ?)";
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, name);
pst.setInt(2, age);
pst.setString(3, (String) cmbGender.getSelectedItem());
pst.setString(4, txtAddress.getText());
pst.setString(5, contactNo);
pst.setString(6, email);
pst.executeUpdate();
JOptionPane.showMessageDialog(this, "Patient added successfully!");
loadPatients(); // Reload patients for the dropdown
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Error: " + e.getMessage());
}
}
private void addDoctor(String qualifications) {
if (txtQualifications.getText().trim().isEmpty() || qualifications.trim().isEmpty()) {
JOptionPane.showMessageDialog(this, "Please fill out all doctor fields.");
return;
}
try (Connection con = Connect.ConnectDB()) {
String sql = "INSERT INTO Doctor (DoctorName, Qualifications, BloodGroup) VALUES (?, ?, ?)";
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, txtQualifications.getText());
pst.setString(2, qualifications);
pst.setString(3, (String) cmbBloodGroup.getSelectedItem());
pst.executeUpdate();
JOptionPane.showMessageDialog(this, "Doctor added successfully!");
loadDoctors(); // Reload doctors for the dropdown
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Error: " + e.getMessage());
}
}
private void addAppointment() {
if (cmbPatient.getSelectedItem() == null || cmbDoctor.getSelectedItem() == null || txtTime.getText().trim().isEmpty()) {
JOptionPane.showMessageDialog(this, "Please select a patient, a doctor, and provide an appointment time.");
return;
}
try (Connection con = Connect.ConnectDB()) {
String sql = "INSERT INTO Appointment (PatientID, DoctorID, AppointmentTime) VALUES (?, ?, ?)";
PreparedStatement pst = con.prepareStatement(sql);
String selectedPatient = (String) cmbPatient.getSelectedItem();
int patientID = Integer.parseInt(selectedPatient.split(" - ")[0]);
String selectedDoctor = (String) cmbDoctor.getSelectedItem();
int doctorID = Integer.parseInt(selectedDoctor.split(" - ")[0]);
pst.setInt(1, patientID);
pst.setInt(2, doctorID);
pst.setString(3, txtTime.getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(this, "Appointment added successfully!");
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Error: " + e.getMessage());
}
}
private void loadPatients() {
try (Connection con = Connect.ConnectDB()) {
String sql = "SELECT PatientID, PatientName FROM Patient";
PreparedStatement pst = con.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
cmbPatient.removeAllItems();
while (rs.next()) {
cmbPatient.addItem(rs.getInt("PatientID") + " - " + rs.getString("PatientName"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void loadDoctors() {
try (Connection con = Connect.ConnectDB()) {
String sql = "SELECT DoctorID, DoctorName FROM Doctor";
PreparedStatement pst = con.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
cmbDoctor.removeAllItems();
while (rs.next()) {
cmbDoctor.addItem(rs.getInt("DoctorID") + " - " + rs.getString("DoctorName"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private boolean isValidEmail(String email) {
String emailRegex = "^[A-Za-z0-9+_.-]+@(.+)$"; // Basic regex for email validation
return email.matches(emailRegex);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
HospitalManagementSystem hms = new HospitalManagementSystem();
hms.setVisible(true);
});
}
}
class Connect {
public static Connection ConnectDB() {
Connection con = null;
try {
String url = "jdbc:mysql://localhost:3306/hospital"; // Update with your DB details
String user = "root"; // Your DB username
String password = "password"; // Your DB password
con = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
}