Hospital Management System Java Project

by | Jun 28, 2023 | Basic Coding, Coding, Java

Home » Coding » Basic Coding » Hospital Management System Java Project

In the Hospital Management System Java Project, we will create a software application that helps hospitals and healthcare facilities manage their day-to-day operations efficiently. It typically includes functionalities such as patient registration, appointment scheduling, medical record management, billing and invoicing, pharmacy management, and inventory management for a hospital. 

Introduction

To build this project, firstly, we’ll begin with creating the framework by adding two buttons, “Patient” and “Doctors”. When you hit the patient button, it takes you to the panel where you can register the patient’s details like Name, age, Date of Appointment, slot, and consulting doctor name for booking the slot for an appointment with a doctor. After entering the details, you need to hit the “Add Record” button to save the patient’s details in the database. Next comes the Doctor’s panel, where you can register the details of the doctor working in your organization by inputting their name, age, specialty, experience, and consultation fees, which saves the details in the database.

Finally, this Hospital Management System saves the details of doctors and patients visiting the hospital. The organization can use this system to maintain the records of the consultations.

 

Objectives

The Hospital Management System Java Project has the following objectives that we want to achieve.

1. To create a user-friendly interface with the help of the Swing module.

2. To use SQL database to store information regarding patients having an appointment with doctors in the hospital.

3. To implement the tables that contain the patient’s and doctor’s information in different panels & attach the functionality of adding & updating the details in the UI and the database.

Requirements

1. Java Compiler IDE

2. Swing Module To Create The Graphical User Interface (GUI)

Source Code

package com.company;
public class Main {
public static void main(String[] args) {
new MainScreen ();
}
}
package com.company;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainScreen {
private JButton PATIENTSButton;
private JButton DOCTORSButton;
private JPanel mainPanel;
JFrame mainF = new JFrame();
public MainScreen() {
mainF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainF.setContentPane(mainPanel);
mainF.pack();
mainF.setLocationRelativeTo(null);
mainF.setVisible(true);
PATIENTSButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Patients();
}
});
DOCTORSButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Doctor();
}
});
}
}
package com.company;
import com.toedter.calendar.JDateChooser;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.*;
import java.text.DateFormat;
import java.util.Vector;
public class Patients {
private JTextField nameData;
private JTextField ageData;
private static int counter=1;
private JTable table1;
private JButton ADDRECORDButton;
private JButton UPDATERECORDButton;
private JPanel patientPanel;
private JComboBox slot;
private JPanel datePanel;
private JComboBox comboBox1;
private JLabel total;
private JComboBox litres;
JFrame patientF = new JFrame();
JDateChooser dateChooser = new JDateChooser();
public Patients(){
patientF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
patientF.setContentPane(patientPanel);
patientF.pack();
patientF.setLocationRelativeTo(null);
datePanel.add(dateChooser);
patientF.setVisible(true);
updateCombo();
tableData();
ADDRECORDButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(nameData.getText().equals("")|| ageData.getText().equals("")|| slot.getSelectedItem()==null){
JOptionPane.showMessageDialog(null,"Please Fill All Fields to add Record.");
}else{
try {
String sql = "insert into patient"+"(NAME,AGE,DATE,SLOT,CONSULTING_DOCTOR)"+"values (?,?,?,?,?)";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root");
PreparedStatement statement = connection.prepareStatement(sql);
String date= DateFormat.getDateInstance().format(dateChooser.getDate());
statement.setString(1, nameData.getText());
statement.setString(2, ageData.getText());
statement.setString(3,date);
statement.setString(4,""+slot.getSelectedItem());
statement.setString(5,""+comboBox1.getSelectedItem());
statement.executeUpdate();
JOptionPane.showMessageDialog(null,"DETAILS ADDED SUCCESSFULLY");
nameData.setText("");
ageData.setText("");
dateChooser.setCalendar(null);
}catch (Exception ex){
JOptionPane.showMessageDialog(null,ex.getMessage());
}
tableData();
}
}
});
UPDATERECORDButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
String date= DateFormat.getDateInstance().format(dateChooser.getDate());
String sql = "UPDATE patient " +
"SET DATE = '"+ date+"',SLOT='"+ slot.getSelectedItem()+
" WHERE NAME= '"+nameData.getText()+"'";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root");
PreparedStatement statement = connection.prepareStatement(sql);
statement.executeUpdate();
JOptionPane.showMessageDialog(null,"Updated successfully");
}catch (Exception e2){
System.out.println(e2);
}
tableData();
}
});
table1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
DefaultTableModel dm = (DefaultTableModel)table1.getModel();
int selectedRow = table1.getSelectedRow();
nameData.setText(dm.getValueAt(selectedRow,0).toString());
ageData.setText(dm.getValueAt(selectedRow,1).toString());
}
});
}
public void updateCombo(){
String a = "select * from doctor";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root");
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(a);
while (rs.next()){
comboBox1.addItem(rs.getString("NAME"));
}
}catch (Exception e){
JOptionPane.showMessageDialog(null,e.getMessage());
}
}
public void tableData() {
try{
String a= "Select* from patient";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root");
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(a);
table1.setModel(buildTableModel(rs));
}catch (Exception ex1){
JOptionPane.showMessageDialog(null,ex1.getMessage());
}
}
public static DefaultTableModel buildTableModel(ResultSet rs)
throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
public int count(){
int counter = 0;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root");
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("Select PATIENT_ID from clinic");
while (rs.next()){
counter= rs.getInt(1);
}
}catch (Exception e){
System.out.println(e);
}
return counter;
}
}
package com.company;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.*;
import java.text.DateFormat;
import java.util.Vector;
public class Doctor {
private JTextField nameData;
private JTextField ageData;
private JTable table1;
private JButton ADDRECORDButton;
private JButton UPDATERECORDButton;
private JPanel doctorPanel;
private JTextField fee;
private JTextField exp;
private JTextField speciality;
JFrame doctF = new JFrame();
public Doctor(){
doctF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
doctF.setContentPane(doctorPanel);
doctF.pack();
doctF.setLocationRelativeTo(null);
doctF.setVisible(true);
tableData();
ADDRECORDButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(nameData.getText().equals("")|| ageData.getText().equals("")){
JOptionPane.showMessageDialog(null,"Please Fill All Fields to add Record.");
}else{
try {
String sql = "insert into doctor"+"(NAME,AGE,SPECIALITY,EXP,CONSULTATION_FEE)"+"values (?,?,?,?,?)";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root");
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, nameData.getText());
statement.setString(2, ageData.getText());
statement.setString(3,speciality.getText());
statement.setString(4,exp.getText());
statement.setString(5,fee.getText());
statement.executeUpdate();
JOptionPane.showMessageDialog(null,"DETAILS ADDED SUCCESSFULLY");
nameData.setText("");
ageData.setText("");
speciality.setText("");
exp.setText("");
fee.setText("");
}catch (Exception ex){
JOptionPane.showMessageDialog(null,ex.getMessage());
}
tableData();
}
}
});
UPDATERECORDButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
String sql = "UPDATE doctor " +
"SET EXP = '"+ exp.getText()+"',AGE='"+ ageData.getText()+
"',CONSULTATION_FEE='"+ fee.getText()+"'"+
" WHERE NAME= '"+nameData.getText()+"'";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root");
PreparedStatement statement = connection.prepareStatement(sql);
statement.executeUpdate();
JOptionPane.showMessageDialog(null,"Updated successfully");
}catch (Exception e2){
System.out.println(e2);
}
tableData();
}
});
table1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
DefaultTableModel dm = (DefaultTableModel)table1.getModel();
int selectedRow = table1.getSelectedRow();
nameData.setText(dm.getValueAt(selectedRow,0).toString());
ageData.setText(dm.getValueAt(selectedRow,1).toString());
speciality.setText(dm.getValueAt(selectedRow,2).toString());
exp.setText(dm.getValueAt(selectedRow,3).toString());
fee.setText(dm.getValueAt(selectedRow,4).toString());
}
});
}
public void tableData() {
try{
String a= "Select* from doctor";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root");
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(a);
table1.setModel(buildTableModel(rs));
}catch (Exception ex1){
JOptionPane.showMessageDialog(null,ex1.getMessage());
}
}
public static DefaultTableModel buildTableModel(ResultSet rs)
throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
}

Explanation of the Code

The source code of the Hospital Management System Java Project has two sections. The first involves creating the GUI, and the second is retrieving the patient’s information from the SQL database & updating the hospital record according to the user’s input.

Let’s see the GUI part first:

1. It consists of 2 buttons. One has the patient’s information & the other has the doctor’s details.

2. Both panels have 5 text fields that have patient’s & doctor’s information, respectively.

3. Both panels have 2 buttons each for adding and updating the information in the database.

Moving to the retrieval, we will apply the following:

1. The first step is to build a connection with the database using the Connection object.

2. We inject the query that stores table data in ResultSet.

3. Finally, send data to Jtable.

4. Adding & Updating functionalities are added so that it gets updated in the database and the interface.

Output

Main Interface

Hospital Management System Java Project

Figure 1: This is the main page of the Hospital Management System

Patients Panel

Hospital Management System Java Project - Patient's Panel

Figure 2: This is the Patient record page, where details of patients are saved for the appointment.

Doctors Panel

Hospital Management System Java Project - Doctor Panel

Figure 3: This is the doctor’s page where the doctor’s information is saved.

Conclusion

Hence, we have successfully built a Hospital Management System Java Project that helps to manage patients’ as well as doctors’ information & helps to manage a hospital easily with Swing used for GUI. This program is a very efficient & easy way to maintain the data regarding patients & doctors. With the implementation of the Swing module for GUI, this program is a very efficient & easy way to manage patients’ as well as doctors’ information and store the results in a database.

More Java Projects>>>

You May Also Like To Create…

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *