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

Hospital Management

The document contains Java classes for a Hospital Management System, including functionalities for managing patients and doctors. The Patient class allows adding and viewing patients, while the Doctors class provides a method to view doctors and check their availability. The main HospitalManagement class orchestrates the application flow, including booking appointments and interacting with a MySQL database.

Uploaded by

giblibaba
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 views6 pages

Hospital Management

The document contains Java classes for a Hospital Management System, including functionalities for managing patients and doctors. The Patient class allows adding and viewing patients, while the Doctors class provides a method to view doctors and check their availability. The main HospitalManagement class orchestrates the application flow, including booking appointments and interacting with a MySQL database.

Uploaded by

giblibaba
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/ 6

Patient.

java class
package HospitalManagement;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Patient {


private Connection connection;
private Scanner scanner;

public Patient(Connection connection,Scanner scanner){


this.connection = connection;
this.scanner = scanner;
}

public void addPatient(){


System.out.println("Enter Patient Name: ");
String name = scanner.next();
System.out.println("Enter Patient Age : ");
int age = scanner.nextInt();
System.out.println("Enter Patient Gender : ");
String gender = scanner.next();

try {
String query = "INSERT INTO patients(name,age,gender)
VALUES(?,?,?)";
PreparedStatement preparedStatement =
connection.prepareStatement(query);
preparedStatement.setString(1, name);
preparedStatement.setInt(2,age);
preparedStatement.setString(3,gender);
int affectedRows = preparedStatement.executeUpdate();
if (affectedRows>0){
System.out.println("Patient Added Successfully!!");
}else {
System.out.println("Failed to add Patient!");
}

}catch (SQLException e){


e.printStackTrace();
}
}
public void viewPatients(){
String query = "select * from patients";
try{
PreparedStatement preparedStatement =
connection.prepareStatement(query);
ResultSet resultSet = preparedStatement.executeQuery();
System.out.println("+------------+------------------
+-------------+--------------+");
System.out.println("| Patient Id | Name | Age
| Gneder |");
System.out.println("+------------+------------------
+-------------+--------------+");
while(resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String gender = resultSet.getString("gender");
System.out.printf("|%-12s|%-18s|%-14s|%-14s|\n", id, name,
age, gender);
System.out.println("+------------+------------------
+-------------+--------------+");
}
}catch (SQLException e){
e.printStackTrace();
}
}

public boolean getPatientById(int id){


String query = "Select * from patients where id = ?";
try{
PreparedStatement preparedStatement =
connection.prepareStatement(query);
preparedStatement.setInt(1,id);
ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
return true;
}
else {
return false;
}
}catch (SQLException e){
e.printStackTrace();;
}
return false;
}

Doctors.java class
package HospitalManagement;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Doctors {


private Connection connection;

public Doctors(Connection connection){


this.connection = connection;

public static void viewDoctors(Connection connection){


String query = "select * from doctors";
try{
PreparedStatement preparedStatement =
connection.prepareStatement(query);
ResultSet resultSet = preparedStatement.executeQuery();
System.out.println("+------------+------------------
+---------------------------+");
System.out.println("| Doctor Id | Name |
Specialization |");
System.out.println("+------------+------------------
+---------------------------+");
while(resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String specialization =
resultSet.getString("specialization");
System.out.printf("|%-12s|%-18s|%-30s|\
n",id,name,specialization);
System.out.println("+------------+------------------
+---------------------------+");
}
}catch (SQLException e){
e.printStackTrace();
}
}

public boolean detDoctorsById(int id){


String query = "Select * from doctors where id = ?";
try{
PreparedStatement preparedStatement =
connection.prepareStatement(query);
preparedStatement.setInt(1,id);
ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
return true;
}
else {
return false;
}
}catch (SQLException e){
e.printStackTrace();;
}
return false;
}
}

HospitalManagement.java main file


package HospitalManagement;

import java.sql.*;
import java.util.Scanner;

public class HospitalManagement {


private static final String url =
"jdbc:mysql://localhost:3306/hospital";
private static final String username = "root";
private static final String password = "Akash@1320";

public static void main(String[] args){


try{
Class.forName("com.mysql.cj.jdbc.Driver");
}catch (ClassNotFoundException e){
e.printStackTrace();
}
Scanner scanner = new Scanner(System.in);
try{
Connection connection =
DriverManager.getConnection(url,username,password);
Patient patient = new Patient(connection,scanner);
Doctors doctors = new Doctors(connection);
while(true){
System.out.println("HOSPITAL MANAGEMENT SYSTEM ");
System.out.println("1. Add Patient");
System.out.println("2. View Patient");
System.out.println("3. View Doctors");
System.out.println("4. Book Appointment");
System.out.println("Exit");
System.out.println("Enter any choice : ");
int choice = scanner.nextInt();
switch (choice){
case 1:
// Add Patient
patient.addPatient();
System.out.println();
break;
case 2:
// View Patient
patient.viewPatients();
System.out.println();
break;
case 3:
// View Doctors
Doctors.viewDoctors(connection);
System.out.println();
break;
case 4:
// Book Appointment

bookAppointment(patient,doctors,connection,scanner);
System.out.println();
break;
case 5:
return;
default:
System.out.println("Enter Valid Choice!!");
}
}

}catch (SQLException e)
{
e.printStackTrace();
}
}

public static void bookAppointment(Patient patient, Doctors


doctors,Connection connection, Scanner scanner)
{
System.out.println("Enter patient id: ");
int patientid = scanner.nextInt();
System.out.println("Enter doctor id: ");
int doctorid = scanner.nextInt();
System.out.println("Enter appointment date (YYYY-MM-DD) : ");
String appointDate = scanner.next();
if(patient.getPatientById(patientid) &&
doctors.detDoctorsById(doctorid)){
if(checkDoctorAvailability(doctorid,appointDate,connection)){
String appointQuerry = "Insert into
appointments(patient_id, doctor_id, appointment_date) value(?,?,?)";
try{
PreparedStatement preparedStatement =
connection.prepareStatement(appointQuerry);
preparedStatement.setInt(1, patientid);
preparedStatement.setInt(2, doctorid);
preparedStatement.setString(3, appointDate);
int rowsAffected = preparedStatement.executeUpdate();
if(rowsAffected>0){
System.out.println("Appointment Booked!!!");
}else{
System.out.println("Failde to book appointment");
}
}catch (SQLException e){
e.printStackTrace();
}
}else{
System.out.println("Doctor is not availabele on this
date!!");
}
}else {
System.out.println("Either doctor or patient doesn't
Exist!!!");
}
}

public static boolean checkDoctorAvailability(int doctorid,String


appointDate,Connection connection){
String querry = "SELECT COUNT(*) FROM appointments WHERE doctor_id
= ? And appointment_date = ?";
try{
PreparedStatement preparedStatement =
connection.prepareStatement(querry);
preparedStatement.setInt(1,doctorid);
preparedStatement.setString(2,appointDate);
ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
int count = resultSet.getInt(1);
if(count==0) {
return true;
}else {
return false;
}
}
}catch (SQLException e){
e.printStackTrace();
}
return false;
}
}
note: it is just a prototype to show how much the
work is done!

You might also like