Java Swing and MySQL desktop application for managing the daily records of a small hospital or clinic.
This project is built as an Object-Oriented Programming hospital management system. Its purpose is to keep common hospital data in one place and make it easy for an administrator to:
- Register and log in to the system.
- Manage doctors and their specializations.
- Manage patients and their basic medical information.
- Schedule and update appointments between patients and doctors.
- Manage medicine inventory, prices, quantities, and expiry dates.
- Search records quickly from each module.
The system is not meant to be a production medical records platform. It is a learning project that demonstrates Java OOP, Swing GUI development, JDBC, DAO classes, validation, and MySQL database operations.
- Login and registration screens.
- Dashboard with live totals for doctors, patients, appointments, and medicines.
- Doctors module with add, edit, delete, search, and refresh.
- Patients module with add, edit, delete, search, and refresh.
- Appointments module with doctor/patient selection, date, time, status, notes, search, and refresh.
- Medicines module with add, edit, delete, search, and refresh.
- Input validation for required fields, email, age, price, quantity, experience, date, and time.
- MySQL database script with demo records.
- Separated packages for models, database access, UI screens, dialogs, and utilities.
- Java 8 or higher
- Java Swing for the graphical user interface
- MySQL for persistent storage
- JDBC for database communication
- MySQL Connector/J included in
lib/
HospitalManagementSystem/
|-- README.md
|-- hospital_db.sql
|-- lib/
| `-- mysql-connector-j-9.7.0.jar
`-- src/
`-- hospital/
|-- Main.java
|-- db/
| |-- DBConnection.java
| |-- UserDAO.java
| |-- DoctorDAO.java
| |-- PatientDAO.java
| |-- AppointmentDAO.java
| `-- MedicineDAO.java
|-- models/
| |-- User.java
| |-- Doctor.java
| |-- Patient.java
| |-- Appointment.java
| `-- Medicine.java
|-- ui/
| |-- LoginFrame.java
| |-- RegisterFrame.java
| |-- DashboardFrame.java
| |-- DoctorsPanel.java
| |-- PatientsPanel.java
| |-- AppointmentsPanel.java
| |-- MedicinesPanel.java
| `-- dialogs/
| |-- DoctorDialog.java
| |-- PatientDialog.java
| |-- AppointmentDialog.java
| `-- MedicineDialog.java
`-- utils/
|-- UITheme.java
`-- Validator.java
The application starts from src/hospital/Main.java.
Main.main() applies the shared Swing look and feel through UITheme.applyLookAndFeel(), then opens LoginFrame on the Swing event dispatch thread.
LoginFrame asks for username and password.
When the user clicks Sign In, the form validates that both fields are filled. Then UserDAO.login() runs a SQL query against the users table:
SELECT * FROM users WHERE username = ? AND password = ?If a matching record exists, the login window closes and DashboardFrame opens.
RegisterFrame creates a new account. It validates:
- Full name is required.
- Username is required.
- Email must use a valid email format.
- Password must be at least 6 characters.
- Confirm password must match password.
If validation passes, UserDAO.register() checks whether the username already exists, then inserts the new user into the users table.
DashboardFrame is the main application window after login.
It contains:
- A left sidebar for navigation.
- A top bar showing the current page name.
- A main content area that changes when a module is selected.
- Dashboard cards showing live totals from the database.
The totals come from DAO methods:
DoctorDAO.getTotalDoctors()PatientDAO.getTotalPatients()AppointmentDAO.getTotalAppointments()MedicineDAO.getTotalMedicines()
Each module is a Swing JPanel shown inside the dashboard content area.
The modules follow the same pattern:
- Load records from the matching DAO.
- Display records in a non-editable
JTable. - Open a dialog for adding or editing records.
- Delete selected records after confirmation.
- Search records using a keyword.
- Refresh the table to show all records again.
Dialogs are used for add and edit operations:
DoctorDialogPatientDialogAppointmentDialogMedicineDialog
Each dialog collects form data, validates it with Validator, and returns a model object to the panel. The panel then passes that model object to the correct DAO method.
The db package contains DAO classes. DAO means Data Access Object.
The DAO classes hide SQL from the UI. For example, DoctorsPanel does not directly write SQL. It calls methods like:
doctorDAO.getAllDoctors()doctorDAO.addDoctor(doctor)doctorDAO.updateDoctor(doctor)doctorDAO.deleteDoctor(id)doctorDAO.searchDoctors(keyword)
This makes the project easier to understand because the UI code and database code are separated.
The models package contains simple Java classes that represent database records:
UserDoctorPatientAppointmentMedicine
These classes use private fields with getters and setters, which demonstrates encapsulation.
The utils package contains:
UITheme.java: shared colors, fonts, buttons, cards, fields, table styling, and look-and-feel setup.Validator.java: reusable validation methods for input fields.
The database script hospital_db.sql creates these tables:
users: login and registration accounts.doctors: doctor profiles.patients: patient records.appointments: appointments connected to patients and doctors.medicines: medicine inventory.
The appointments table uses foreign keys:
patient_idreferencespatients.iddoctor_idreferencesdoctors.id
If a patient or doctor is deleted, related appointments are also deleted because the SQL script uses ON DELETE CASCADE.
- Install Java JDK 8 or higher.
- Install MySQL Server.
- Use the included MySQL connector file:
lib/mysql-connector-j-9.7.0.jar.
Open MySQL Workbench, phpMyAdmin, or the MySQL command line and run:
SOURCE path/to/hospital_db.sql;Or open hospital_db.sql and execute the full script manually.
The script creates:
- The
hospital_dbdatabase. - All required tables.
- A default admin account.
- Demo doctors, patients, appointments, and medicines.
Open src/hospital/db/DBConnection.java and update this line if your MySQL password is different:
private static final String PASSWORD = "fahair";Also check:
private static final String URL = "jdbc:mysql://localhost:3306/hospital_db";
private static final String USER = "root";From the project root on Windows PowerShell:
javac -cp "lib\mysql-connector-j-9.7.0.jar" -d out src\hospital\Main.java src\hospital\db\*.java src\hospital\models\*.java src\hospital\ui\*.java src\hospital\ui\dialogs\*.java src\hospital\utils\*.javajava -cp "out;lib\mysql-connector-j-9.7.0.jar" hospital.MainYou can also open the project in NetBeans, IntelliJ IDEA, or Eclipse.
Make sure to:
- Add
lib/mysql-connector-j-9.7.0.jarto the project libraries. - Set the main class to
hospital.Main. - Run
hospital_db.sqlbefore logging in. - Check the MySQL password in
DBConnection.java.
| Username | Password |
|---|---|
| admin | admin123 |
- Start the program.
- Log in using
adminandadmin123. - The dashboard opens and shows record totals.
- Click
Doctorsto add, edit, delete, search, or refresh doctors. - Click
Patientsto manage patient records. - Click
Appointmentsto schedule a patient with a doctor. - Click
Medicinesto manage medicine inventory. - Click
Logoutto return to the login screen.
- Encapsulation: model fields are private and accessed through getters/setters.
- Abstraction: DAO classes hide SQL details from the UI.
- Inheritance: UI classes extend Swing classes such as
JFrame,JPanel, andJDialog. - Composition: frames and panels are built from reusable Swing components.
- Separation of concerns: models, DAO classes, UI classes, dialogs, validation, and theme code are separated.
- Passwords are stored as plain text for learning purposes. A real system should hash passwords.
- Date fields use
YYYY-MM-DD. - Time fields use
HH:MM. - The application expects MySQL to be running before login.
- If a table appears empty, first confirm the database script has been executed and
DBConnection.javahas the correct credentials.