Hospital Management System using java Swing GUI

Introduction
The health care industry needs efficient and flexible management systems to process a large amount of patient and medical information properly and safely .This Hospital Management System built with Java Swing for graphical user interface and MySQL for live data processing. Its aim is to automate hospital workflows , emphasising primarily CRUD(create , read , update , delete) operations for both doctor and patients and allowing secure admin login so that access is limited to unauthorised users.It provides a clean and simple interface allowing hospital administrators to manage their records with ease. The admin authentication module guarantees that only authenticated users can access and change certain sensitive actions involving hospital data. The same interface allows real-time action on data, and provides a logout option that takes the user back to the login page promptly after the system session has ended.
Language & Interface
Hospital Management System using Java with source code using Java Graphical User Interface (GUI) programmed with Java Swing as frontend and MySQL as the backend for storing the data. It is a desktop GUI application in which the system will manage hospital operations dealing with patient and doctor records through online or real-time database interaction.
Key Highlights :
User Login : Only authenticated users can access the management system.
Patient Module : Provides total CRUD functionality to manage patient records efficiently and timely.
Doctor Module : Add, edit, view, or remove doctor information as needed.
Logout Functionality: Provides safe return to the login page once the task is completed. Real-time Sync: Changes are automatically updated in the database through MySQL Workbench.
This application offers a scalable architecture of modules—future features such as appointment scheduling, lab tests, and billing can be integrated with ease. By concentrating on basic CRUD operations, it creates a solid backend integration and accessible frontend imperative for healthcare functions.
Required Modules or packages
To run the Hospital Management System on your machine , you will need the following software components :
– Java Development kit (JDK 8 or later) : The core environment for running Java applications. – Java Swing : This is the part pf java standard Library that will be used to build GUI. – MySQL Database Server: Stores patient, doctor, and appointment records. – MySQL connector : allows java to connect to and interact with MySQL models.
System Setup Instructions:
1 – Install Java JDK
You can download this here: Oracle JDK
2 – Install NetBeans or Eclipse IDE
- Use NetBeans – this IDE will be already configured to use Swing.
- Use Eclipse – you will need to use the Swing plugin.
3 – Install XAMPP or MySQL Server
XAMPP is an all-in one solution that has MySQL, Apache, and PHP.
4 – Add JDBC Connector to the IDE
You can download it here: MySQL Connector/J
In your IDE, you’ll want to right-click your project → Properties → Libraries → Add JAR/Folder → choose the JDBC driver you are using.
How to Run the Code
- Clone the repository
git clone https://github.com/zahidrahimoon/Hospital-Management-System.git
- Setup MySQL Database
Launch MySQL workbench or open phpMyadmin.
Create a database : CREATE DATABASE hospital.
Use the SQL schema that comes with the project, if any; otherwise, create tables manually for:
– patients
– doctors
– admin
Table creation shall be as follows:
CREATE TABLE patients (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
age INT,
gender VARCHAR(10)
);
CREATE TABLE doctors (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
specialty VARCHAR(100)
);
CREATE TABLE admin (
username VARCHAR(50),
password VARCHAR(50)
);
- Configure the Database in the Java Code
In LoginPage.java or DBConnection.java, you should have something like the following for the connection string.
Connection con = DriverManager.getConnection(“jdbc:mysql://localhost:3306/hospital”, “root”, “”);
Make sure to change the username and password as per your MySQL Settings. 4. Open in IDE
Launch NetBeans, Eclipse, or any Java-supporting IDE.
Open your extracted project.
Add the MySQL JDBC Driver to your project libraries.
- Build and Run
Build the project: Run > Build Project
Run the main file: Run > Run File or Shift + F6
The application is started with the login prompt.
Code Explanation
Let’s explore one of the core functionalities of Adding a New Patient
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
String name = txtName.getText();
int age = Integer.parseInt(txtAge.getText());
String gender = (String) comboGender.getSelectedItem();
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hospital", "root", "");
PreparedStatement pst = con.prepareStatement("INSERT INTO patients(name, age, gender) VALUES (?, ?, ?)");
pst.setString(1, name);
pst.setInt(2, age);
pst.setString(3, gender);
pst.executeUpdate();
JOptionPane.showMessageDialog(this, "Patient Added Successfully"); } catch (SQLException e) {
e.printStackTrace();
}
}
- Admin Login
Everything starts in the login screen (Login.java) where the admin enters a username and password.
The credentials are then verified against hardcoded values or records in a database. After authentication is successful, the main dashboard (HomePage.java) is displayed.
- Patient Management (AddPatient.java, ViewPatient.java)
AddPatient.java runs a patient detail collection form for name, gender, contact, and address are collected through text boxes and combo boxes.
When the “Add” button is pressed, a SQL INSERT statement will be executed via PreparedStatement to save the patient details to the patients table.
ViewPatient.java runs an existing patients records display retrieved from an SQL SELECT query and displayed in a JTable.
Mentioned existing patient records will have functions available to update or delete the existing record that will be triggered by run using UPDATE and DELETE SQL queries that will be triggered by buttons.
Doctor Management (AddDoctor.java, ViewDoctor.java)
Similar to patient management.
This function will allow doctors to be added, viewed, edited, or deleted from their own list. Similar JDBC operations, but doctor specific fields such as specialization and department.
- Database Connectivity
Using JDBC throughout the project to connect the MySQL database.
There will either be a separate method or class (typically called something such as DBConnection.java) to hold the database access credential details, as well as provide a predictable way to open and close connection.
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hms
- GUI Creation
GUI components — JLabel, JTextField, JButton, JTable, JPanel — are used to build forms.
Layout managers (FlowLayout, GridLayout, AbsoluteLayout) are used to place the components in each window.
Event listeners (ActionListener) are used to listen to the button clicks to run database operations.
Data input: The program receives the user data from text boxes.
JDBC Connection: The program connects to the hospital database.
Prepared statement: The prepared statement is used to safely insert values into patient table . Feedback : Message box appears to confirm successful data entry.
Why This Matters:
- Prepared statements prevent SQL injection.
- It follows good practices of separating GUI and logic.
- Easy to adapt and extend for other modules (doctors, staff, etc.).
- Other similar methods exist for editing and deleting records, booking appointments, or generating bills. The entire codebase is modular and event-driven, which helps in future updates.
Source Code:
Output:



