0% found this document useful (0 votes)
22 views

Hosptal Management Project Coding PDF

The document shows the SQL code to create tables for a hospital management system database including patients, doctors, and appointments tables. It also shows sample data being inserted and functions to book appointments while checking doctor availability.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Hosptal Management Project Coding PDF

The document shows the SQL code to create tables for a hospital management system database including patients, doctors, and appointments tables. It also shows sample data being inserted and functions to book appointments while checking doctor availability.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SQL for the Hospital Management System

use mysql 8.1.0 connector fo it...


<mysql> show databases;
+----------------------------+
| Database |
+----------------------------+
| hospital_management_system |
| information_schema |
| mysql |
| performance_schema |
| sys |
+----------------------------+
5 rows in set (0.06 sec)

<mysql> use hospital_management_system;


Database changed

<mysql> CREATE TABLE patients(


-> id INT AUTO_INCREMENT PRIMARY KEY,
-> name VARCHAR(255) NOT NULL,
-> age INT NOT NULL,
-> gender VARCHAR(10) NOT NULL
-> );
Query OK, 0 rows affected (0.09 sec)

<mysql> CREATE TABLE doctors(


-> id INT AUTO_INCREMENT PRIMARY KEY,
-> name VARCHAR(255) NOT NULL,
-> specialisation VARCHAR(255) NOT NULL
-> );
Query OK, 0 rows affected (0.07 sec)

<mysql> CREATE table appointments(


-> id INT AUTO_INCREMENT PRIMARY KEY,
-> patient_id INT NOT NULL,
-> doctor_id INT NOT NULL,
-> appointment_date DATE NOT NULL,
-> FOREIGN KEY(patient_id) references patients(id),
-> FOREIGN KEY(doctor_id) references doctors(id)
-> );
Query OK, 0 rows affected (0.07 sec)

<mysql> show tables;


+--------------------------------------+
| Tables_in_hospital_management_system |
+--------------------------------------+
| appointments |
| doctors |
| patients |
+--------------------------------------+
3 rows in set (0.01 sec)

<mysql> describe appointments;


+------------------+------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| patient_id | int | NO | MUL | NULL | |
| doctor_id | int | NO | MUL | NULL | |
| appointment_date | date | NO | | NULL | |
+------------------+------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

<mysql> describe doctors;


+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| specialisation | varchar(255) | NO | | NULL | |
+----------------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

<mysql> describe patients;


+--------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| age | int | NO | | NULL | |
| gender | varchar(10) | NO | | NULL | |
+--------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

The DB administerator will add the doctors like this

<mysql> show databases;


+----------------------------+
| Database |
+----------------------------+
| hospital_management_system |
| information_schema |
| mysql |
| performance_schema |
| sys |
+----------------------------+
5 rows in set (0.01 sec)

<mysql> use hospital_management_system;


Database changed
<mysql> show tables;
+--------------------------------------+
| Tables_in_hospital_management_system |
+--------------------------------------+
| appointments |
| doctors |
| patients |
+--------------------------------------+
3 rows in set (0.01 sec)

<mysql> Insert into doctors(name,specialisation) values("Dr. Amit Singh","Cardiologist");


Query OK, 1 row affected (0.03 sec)

<mysql> Insert into doctors(name,specialisation) values("Dr. Anita Sharma","Physician");


Query OK, 1 row affected (0.00 sec)

<mysql> Insert into doctors(name,specialisation) values("Dr. Vijay Kumar","Neuro Surgeon");


Query OK, 1 row affected (0.00 sec)

<mysql> Insert into doctors(name,specialisation) values("Dr. Ankit Kumar","Head Surgeon");


Query OK, 1 row affected (0.00 sec)
-----------------------------------------------------------------------------------------------
------------------------
package hospitalmanagementsystem;

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

public class HospitalManagementSystem {


private static final String url="jdbc:mysql://localhost:3308/hospital_management_system";
//security wise very important,
//static : no need to make object in the main method
//final : once the value is set , it wont change during the course of the execution

private static final String username="root";


private static final String password="root";

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);
//making the instances of the patient and the doctor class
Patient patient = new Patient(connection,scanner);
Doctor doctor = new Doctor(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 Appointments");
System.out.println("5 Exit");

System.out.println("Enter Your Choice-->");


int choice = scanner.nextInt();
switch(choice){
case 1:
// to add patient
patient.addPatient();
System.out.println();
break;

case 2:
//view patient;
patient.viewPatients();
System.out.println();
break;

case 3:
//view doctors;
doctor.viewDoctors();
System.out.println();
break;

case 4:
//Book Appointments
bookAppointment(patient , doctor , connection , scanner);
System.out.println();
break;

case 5:
return;

default:
System.out.print("Enter a Valid Choice!");

}
}
}
catch(SQLException e){
e.printStackTrace();
}
}
public static void bookAppointment(Patient patient , Doctor doctor , Connection
connection, Scanner scanner){
// 1st add the patient then appointment;
//then check whether the doctor is free?
System.out.print("Enter the patient id:");
int patientid = scanner.nextInt();
System.out.print("Enter the doctors id:");
int doctorId = scanner.nextInt();
System.out.print("Enter the appointment date (YYYY-MM-DD) ");
String appointmentDate = scanner.next();

if(patient.getPatientById(patientid) && doctor.getDoctorById(doctorId)){


if(checkDoctorAvailibility(doctorId , appointmentDate , connection)){
String appointmentQuery = "Insert into
appointments(patient_id,doctor_id,appointment_date) values(?,?,?)";

try{
PreparedStatement
preparedStatement=connection.prepareStatement(appointmentQuery);

preparedStatement.setInt(1,patientid);
preparedStatement.setInt(2,doctorId);
preparedStatement.setString(3,appointmentDate);
int rowsaffected=preparedStatement.executeUpdate();
if(rowsaffected>0){
System.out.print("Appointments Booked!");
}
else{
System.out.print("Failed to book appointment");
}
}
catch(SQLException e){
e.printStackTrace();

}
}
else{
System.out.println("doctor is not available for this date");
}
}
else{
System.out.println("either doctor is busy , Patient not exist");
}

}
public static boolean checkDoctorAvailibility(int doctorId , String appointmentDate,
Connection connection){
String query = "Select Count(*) From appointments Where doctor_id =? And
appointment_date=?";
try{
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1,doctorId);
preparedStatement.setString(2,appointmentDate);
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;
}
}
-----------------------------------------------------------------------------------------------
--------------------------------------------------
package hospitalmanagementsystem;

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.print("Enter Patient Name-->");
String name = scanner.next();
System.out.print("Enter Patient age-->");
int age = scanner.nextInt();
System.out.print("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("Data is inserted ");
}
else{
System.out.println("Data entry failed");

}
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("PAtients : ");
System.out.println("+------------+-------------------+-----------+---------
-----+");
System.out.println("| Patient ID | Name | Age |
Gender |");
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|%-19s|%-11s|%-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;
}
}
-----------------------------------------------------------------------------------------------
--------------------------------------------------
package hospitalmanagementsystem;

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

public class Doctor {


private Connection connection;

public Doctor(Connection connection){


this.connection=connection;
}
public void viewDoctors(){

String query = "Select * from doctors";


try{
PreparedStatement preparedStatement = connection.prepareStatement(query);
ResultSet resultSet = preparedStatement.executeQuery();
System.out.println("Doctors : ");
System.out.println("+------------+-------------------+--------------------------
+");
System.out.println("| Doctors ID | Name | Specialisation
|");
System.out.println("+------------+-------------------+--------------------------
+");
while(resultSet.next()){

int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String specialisation = resultSet.getString("specialisation");

System.out.printf("|%-12s|%-19s|%-26s|\n",id,name,specialisation);
System.out.println("+------------+-------------------+-------------------------
+");

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

public boolean getDoctorById(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;
}
}

You might also like