0% found this document useful (0 votes)
8 views15 pages

Hospital Management System Project Structure

The document provides a complete code implementation for a Hospital Management System, including classes for Patient, Doctor, Appointment, and a client-side interface. It outlines the project structure, database connection utility, and CRUD operations for managing patients, doctors, and appointments. Additionally, it includes SQL commands for setting up the necessary database tables and instructions for running the program.

Uploaded by

priya31patil0504
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views15 pages

Hospital Management System Project Structure

The document provides a complete code implementation for a Hospital Management System, including classes for Patient, Doctor, Appointment, and a client-side interface. It outlines the project structure, database connection utility, and CRUD operations for managing patients, doctors, and appointments. Additionally, it includes SQL commands for setting up the necessary database tables and instructions for running the program.

Uploaded by

priya31patil0504
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Sure!

Below is the complete code for the Hospital Management System, including all the necessary
classes such as Patient, Doctor, Appointment, and the client-side implementation.

Project Structure:

HospitalManagementSystem

├── src

│ └── com

│ └── acc

│ ├── model

│ │ ├── Patient.java

│ │ ├── Doctor.java

│ │ └── Appointment.java

│ ├── deo

│ │ ├── OperableImpl.java # Implementing classes for DB operations

│ ├── service

│ │ ├── Operable.java # Interface in service package

│ ├── util

│ │ └── DBConnection.java # DB connection utility

│ └── Client.java # Main Client class

1. Patient.java

package com.acc.model;

public class Patient {

private int id;

private String name;

private int age;

private String disease;

public Patient(int id, String name, int age, String disease) {

this.id = id;
this.name = name;

this.age = age;

this.disease = disease;

public int getId() {

return id;

public String getName() {

return name;

public int getAge() {

return age;

public String getDisease() {

return disease;

@Override

public String toString() {

return "Patient ID: " + id + ", Name: " + name + ", Age: " + age + ", Disease: " + disease;

2. Doctor.java

package com.acc.model;

public class Doctor {


private int id;

private String name;

private String specialization;

public Doctor(int id, String name, String specialization) {

this.id = id;

this.name = name;

this.specialization = specialization;

public int getId() {

return id;

public String getName() {

return name;

public String getSpecialization() {

return specialization;

@Override

public String toString() {

return "Doctor ID: " + id + ", Name: " + name + ", Specialization: " + specialization;

3. Appointment.java

package com.acc.model;
import java.util.Date;

public class Appointment {

private int id;

private Patient patient;

private Doctor doctor;

private Date appointmentDate;

public Appointment(int id, Patient patient, Doctor doctor, Date appointmentDate) {

this.id = id;

this.patient = patient;

this.doctor = doctor;

this.appointmentDate = appointmentDate;

public int getId() {

return id;

public Patient getPatient() {

return patient;

public Doctor getDoctor() {

return doctor;

public Date getAppointmentDate() {

return appointmentDate;

}
@Override

public String toString() {

return "Appointment ID: " + id + ", Patient: " + patient.getName() + ", Doctor: " +
doctor.getName() + ", Date: " + appointmentDate;

4. Operable.java (Interface in the service package)

package com.acc.service;

import com.acc.model.Patient;

import com.acc.model.Doctor;

import com.acc.model.Appointment;

import java.util.List;

public interface Operable {

List<Patient> findAllPatients();

List<Doctor> findAllDoctors();

void addPatient(Patient patient);

void addDoctor(Doctor doctor);

void addAppointment(Appointment appointment);

void deletePatient(int id);

void deleteDoctor(int id);

void updatePatientAge(int id, int age);

5. OperableImpl.java (Implementation in the deo package)

package com.acc.deo;

import com.acc.model.Patient;

import com.acc.model.Doctor;
import com.acc.model.Appointment;

import com.acc.util.DBConnection;

import com.acc.service.Operable;

import java.sql.*;

import java.util.ArrayList;

import java.util.List;

public class OperableImpl implements Operable {

@Override

public List<Patient> findAllPatients() {

List<Patient> patients = new ArrayList<>();

String query = "SELECT * FROM patient";

try (Connection con = DBConnection.getDBConnection();

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery(query)) {

while (rs.next()) {

patients.add(new Patient(rs.getInt("id"), rs.getString("name"), rs.getInt("age"),


rs.getString("disease")));

} catch (SQLException e) {

e.printStackTrace();

return patients;

@Override

public List<Doctor> findAllDoctors() {


List<Doctor> doctors = new ArrayList<>();

String query = "SELECT * FROM doctor";

try (Connection con = DBConnection.getDBConnection();

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery(query)) {

while (rs.next()) {

doctors.add(new Doctor(rs.getInt("id"), rs.getString("name"), rs.getString("specialization")));

} catch (SQLException e) {

e.printStackTrace();

return doctors;

@Override

public void addPatient(Patient patient) {

String query = "INSERT INTO patient(id, name, age, disease) VALUES(?, ?, ?, ?)";

try (Connection con = DBConnection.getDBConnection();

PreparedStatement stmt = con.prepareStatement(query)) {

stmt.setInt(1, patient.getId());

stmt.setString(2, patient.getName());

stmt.setInt(3, patient.getAge());

stmt.setString(4, patient.getDisease());

stmt.executeUpdate();

} catch (SQLException e) {

e.printStackTrace();

}
}

@Override

public void addDoctor(Doctor doctor) {

String query = "INSERT INTO doctor(id, name, specialization) VALUES(?, ?, ?)";

try (Connection con = DBConnection.getDBConnection();

PreparedStatement stmt = con.prepareStatement(query)) {

stmt.setInt(1, doctor.getId());

stmt.setString(2, doctor.getName());

stmt.setString(3, doctor.getSpecialization());

stmt.executeUpdate();

} catch (SQLException e) {

e.printStackTrace();

@Override

public void addAppointment(Appointment appointment) {

String query = "INSERT INTO appointment(id, patient_id, doctor_id, appointment_date)


VALUES(?, ?, ?, ?)";

try (Connection con = DBConnection.getDBConnection();

PreparedStatement stmt = con.prepareStatement(query)) {

stmt.setInt(1, appointment.getId());

stmt.setInt(2, appointment.getPatient().getId());

stmt.setInt(3, appointment.getDoctor().getId());

stmt.setDate(4, new java.sql.Date(appointment.getAppointmentDate().getTime()));

stmt.executeUpdate();
} catch (SQLException e) {

e.printStackTrace();

@Override

public void deletePatient(int id) {

String query = "DELETE FROM patient WHERE id = ?";

try (Connection con = DBConnection.getDBConnection();

PreparedStatement stmt = con.prepareStatement(query)) {

stmt.setInt(1, id);

stmt.executeUpdate();

} catch (SQLException e) {

e.printStackTrace();

@Override

public void deleteDoctor(int id) {

String query = "DELETE FROM doctor WHERE id = ?";

try (Connection con = DBConnection.getDBConnection();

PreparedStatement stmt = con.prepareStatement(query)) {

stmt.setInt(1, id);

stmt.executeUpdate();

} catch (SQLException e) {

e.printStackTrace();

}
}

@Override

public void updatePatientAge(int id, int age) {

String query = "UPDATE patient SET age = ? WHERE id = ?";

try (Connection con = DBConnection.getDBConnection();

PreparedStatement stmt = con.prepareStatement(query)) {

stmt.setInt(1, age);

stmt.setInt(2, id);

stmt.executeUpdate();

} catch (SQLException e) {

e.printStackTrace();

6. DBConnection.java (Database Utility for Connection)

package com.acc.util;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

public class DBConnection {

public static Connection getDBConnection() {

Connection con = null;

try {

Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hospital_management",
"root", "password"); // replace with your database credentials

} catch (ClassNotFoundException | SQLException e) {

e.printStackTrace();

return con;

7. Client.java (Main Menu-Driven Program)

package com.acc;

import com.acc.model.Patient;

import com.acc.model.Doctor;

import com.acc.model.Appointment;

import com.acc.deo.OperableImpl;

import java.util.Scanner;

public class Client {

public static void main(String[] args) {

OperableImpl operable = new OperableImpl();

Scanner sc = new Scanner(System.in);

while (true) {

System.out.println("\nHospital Management System Menu");

System.out.println("1. Add Patient");

System.out.println("2. Add Doctor");

System.out.println("3. Add Appointment");

System.out.println("4. Update Patient Age");

System.out.println("5. Delete Patient");


System.out.println("6. Delete Doctor");

System.out.println("7. View All Patients");

System.out.println("8. View All Doctors");

System.out.println("9. Exit");

System.out.print("Enter your choice: ");

int choice = sc.nextInt();

switch (choice) {

case 1:

System.out.print("Enter Patient ID: ");

int patientId = sc.nextInt();

sc.nextLine(); // consume newline

System.out.print("Enter Patient Name: ");

String name = sc.nextLine();

System.out.print("Enter Patient Age: ");

int age = sc.nextInt();

sc.nextLine(); // consume newline

System.out.print("Enter Patient Disease: ");

String disease = sc.nextLine();

operable.addPatient(new Patient(patientId, name, age, disease));

break;

case 2:

System.out.print("Enter Doctor ID: ");

int doctorId = sc.nextInt();

sc.nextLine(); // consume newline

System.out.print("Enter Doctor Name: ");

String doctorName = sc.nextLine();

System.out.print("Enter Doctor Specialization: ");

String specialization = sc.nextLine();

operable.addDoctor(new Doctor(doctorId, doctorName, specialization));


break;

case 3:

System.out.print("Enter Appointment ID: ");

int appointmentId = sc.nextInt();

System.out.print("Enter Patient ID: ");

int pId = sc.nextInt();

System.out.print("Enter Doctor ID: ");

int dId = sc.nextInt();

sc.nextLine(); // consume newline

System.out.print("Enter Appointment Date (yyyy-MM-dd): ");

String dateStr = sc.nextLine();

java.util.Date date = java.sql.Date.valueOf(dateStr);

Appointment appointment = new Appointment(appointmentId, new Patient(pId, "", 0,


""), new Doctor(dId, "", ""), date);

operable.addAppointment(appointment);

break;

case 4:

System.out.print("Enter Patient ID to update age: ");

int updateId = sc.nextInt();

System.out.print("Enter new age: ");

int newAge = sc.nextInt();

operable.updatePatientAge(updateId, newAge);

break;

case 5:

System.out.print("Enter Patient ID to delete: ");

int deletePatientId = sc.nextInt();

operable.deletePatient(deletePatientId);

break;
case 6:

System.out.print("Enter Doctor ID to delete: ");

int deleteDoctorId = sc.nextInt();

operable.deleteDoctor(deleteDoctorId);

break;

case 7:

System.out.println("All Patients:");

operable.findAllPatients().forEach(System.out::println);

break;

case 8:

System.out.println("All Doctors:");

operable.findAllDoctors().forEach(System.out::println);

break;

case 9:

System.out.println("Exiting...");

sc.close();

return;

default:

System.out.println("Invalid choice! Try again.");

Database Setup

Make sure to have the following tables in your MySQL database:


1. Patients Table:

CREATE TABLE patient (

id INT PRIMARY KEY,

name VARCHAR(100),

age INT,

disease VARCHAR(100)

);

2. Doctors Table:

CREATE TABLE doctor (

id INT PRIMARY KEY,

name VARCHAR(100),

specialization VARCHAR(100)

);

3. Appointments Table:

CREATE TABLE appointment (

id INT PRIMARY KEY,

patient_id INT,

doctor_id INT,

appointment_date DATE,

FOREIGN KEY (patient_id) REFERENCES patient(id),

FOREIGN KEY (doctor_id) REFERENCES doctor(id)

);

Final Remarks

With this setup, the project is ready to run, and you can interact with the program through the
console to manage hospital-related data. You can add patients, doctors, and appointments, update
and delete records, and view all patients and doctors.

Make sure your database is set up correctly, and the DBConnection.java class contains the proper
credentials (e.g., database name, username, and password).

You might also like