23IT5301-ADVANCED JAVA
Hospital appointment booking system
Abstract:
The Hospital Appointment Management System is a web-based application
developed using Java Servlets, HTML, and MySQL to streamline hospital
operations and enhance patient experience. This system enables patients to
register, view doctor availability, and conveniently book appointments
online. Doctors can efficiently manage their schedules and monitor
upcoming appointments, while administrators can securely maintain patient,
doctor, and appointment databases.
By partially automating the traditional appointment scheduling process, the
system reduces patient waiting time, minimizes human errors, and improves
the overall efficiency of hospital management. This project demonstrates
seamless integration of web technologies and database systems to deliver a
user-friendly and reliable healthcare appointment solution.
Source code:
[Link]
<!DOCTYPE html>
<html>
<head><title>City Hospital - Smart</title></head>
<body>
<h2>Welcome to Smart City Hospital</h2>
<nav>
<a href="[Link]">Register</a> |
<a href="[Link]">Patient Login</a> |
<a href="doctor_login.html">Doctor Login</a> |
<a href="doctor_list.html">Doctors</a> |
<a href="book_appointment.html">Book Appointment</a> |
<a href="view_appointments.html">View Appointments</a> |
<a href="cancel_appointment.html">Cancel Appointment</a> |
<a href="admin_login.html">Admin Login</a> |
<a href="[Link]">About</a> |
<a href="[Link]">Contact</a>
</nav>
<p>Use the menu above to navigate.</p>
</body>
</html>
[Link]
<!DOCTYPE html>
<html>
<head><title>Register Patient</title></head>
<body>
<h2>Patient Registration</h2>
<form action="HospitalServlet" method="post">
<input type="hidden" name="action" value="register">
Name: <input type="text" name="name" required><br>
Email: <input type="email" name="email" required><br>
Password: <input type="password" name="password" required><br>
Age: <input type="number" name="age" required><br>
Gender: <input type="text" name="gender" required><br>
Phone: <input type="text" name="phone" required><br><br>
<input type="submit" value="Register">
</form>
</body>
</html>
[Link]
<!DOCTYPE html>
<html>
<head><title>Patient Login</title></head>
<body>
<h2>Patient Login</h2>
<form action="HospitalServlet" method="post">
<input type="hidden" name="action" value="login">
Email: <input type="email" name="email" required><br>
Password: <input type="password" name="password"
required><br><br> <input type="submit" value="Login">
</form>
</body>
</html>
book_appointment.html
<!DOCTYPE html>
<html>
<head><title>Book Appointment</title></head>
<body>
<h2>Book Appointment</h2>
<form action="HospitalServlet" method="post">
<input type="hidden" name="action" value="book">
Patient ID: <input type="number" name="patient_id"
required><br> Doctor ID: <input type="number"
name="doctor_id" required><br> Date: <input type="date"
name="date" required><br>
Time: <input type="time" name="time" required><br><br>
<input type="submit" value="Book Appointment">
</form>
</body>
</html>
view_appointments.html
<!DOCTYPE html>
<html>
<head><title>View Appointments</title></head>
<body>
<h2>View My Appointments</h2>
<form action="HospitalServlet" method="post">
<input type="hidden" name="action" value="view">
Enter Your Patient ID: <input type="number" name="patient_id"
required><br><br> <input type="submit" value="View Appointments">
</form>
</body>
</html>
doctor_list.html
<!DOCTYPE html>
<html>
<head><title>Doctors</title></head>
<body>
<h2>Our Doctors</h2>
<ul>
<li>Dr. Ramesh Kumar - Cardiologist</li>
<li>Dr. Priya Sharma - Dermatologist</li>
<li>Dr. Manoj Verma - Orthopedic</li>
<li>Dr. Anjali Rao - Pediatrician</li>
</ul>
<a href="[Link]">Home</a>
</body>
</html>
admin_login.html
<!DOCTYPE html>
<html>
<head><title>Admin Login</title></head>
<body>
<h2>Admin Login</h2>
<form action="HospitalServlet" method="post">
<input type="hidden" name="action" value="adminLogin">
Username: <input type="text" name="username" required><br>
Password: <input type="password" name="password"
required><br><br> <input type="submit" value="Login">
</form>
</body>
</html>
admin_dashboard.html
<!DOCTYPE html>
<html>
<head><title>Admin Dashboard</title></head>
<body>
<h2>Admin Dashboard</h2>
<p>Here you can extend to view all appointments from the
database.</p> <a href="[Link]">Home</a>
</body>
</html>
[Link]
<!DOCTYPE html>
<html>
<head><title>About</title></head>
<body>
<h2>About City Hospital</h2>
<p>City Hospital provides high-quality healthcare with top specialists and modern
facilities.</p> <a href="[Link]">Home</a>
</body>
</html>
[Link]
<!DOCTYPE html>
<html>
<head><title>Contact</title></head>
<body>
<h2>Contact Us</h2>
<p>Email: info@[Link]</p>
<p>Phone: +91 9876543210</p>
<p>Address: 123, Main Street, Your City</p>
<a href="[Link]">Home</a>
</body>
</html>
Cancel_Appointement.html
<!DOCTYPE html>
<html>
<head><title>Cancel Appointment</title></head>
<body>
<h2>Cancel Appointment</h2>
<form action="HospitalServlet" method="post">
<input type="hidden" name="action" value="cancel">
Appointment ID: <input type="number" name="appointment_id" required><br><br>
<input type="submit" value="Cancel Appointment">
</form>
</body>
</html>
[Link]
package [Link];
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class HospitalServlet extends HttpServlet {
private Connection connectDB() throws Exception {
[Link]("[Link]"); // Use old driver class
return [Link](
"jdbc:mysql://localhost:3306/hospitaldb", "root", "");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
String action = [Link]("action");
try (Connection con = connectDB()) {
// Registration
if ("register".equals(action)) {
String name = [Link]("name");
String email = [Link]("email");
String password = [Link]("password");
String age = [Link]("age");
String gender = [Link]("gender");
String phone = [Link]("phone");
PreparedStatement ps = [Link](
"INSERT INTO patients (name,email,password,age,gender,phone)
VALUES(?,?,?,?,?,?)");
[Link](1, name);
[Link](2, email);
[Link](3, password);
[Link](4, [Link](age));
[Link](5, gender);
[Link](6, phone);
[Link]();
[Link]("<h3>Registration Successful!</h3>");
[Link]("<a href='[Link]'>Login</a>");
}
// Patient login
else if ("login".equals(action)) {
String email = [Link]("email");
String password = [Link]("password");
PreparedStatement ps = [Link](
"SELECT * FROM patients WHERE email=? AND password=?");
[Link](1,email);
[Link](2,password);
ResultSet rs = [Link]();
if([Link]()) {
int id = [Link]("patient_id");
[Link]("<h2>Welcome "+[Link]("name")+"</h2>");
[Link]("<p>Patient ID: "+id+"</p>");
[Link]("<a href='book_appointment.html'>Book Appointment</a> | ");
[Link]("<a href='view_appointments.html'>View Appointments</a> | ");
[Link]("<a href='cancel_appointment.html'>Cancel Appointment</a>");
} else {
[Link]("<h3>Invalid login! <a href='[Link]'>Try Again</a></h3>");
}
}
// Doctor Login
else if ("doctorLogin".equals(action)) {
String email = [Link]("email");
String password = [Link]("password");
PreparedStatement ps = [Link](
"SELECT * FROM doctors WHERE email=? AND password=?");
[Link](1, email);
[Link](2, password);
ResultSet rs = [Link]();
if ([Link]()) {
int docId = [Link]("doctor_id");
[Link]("<h2>Welcome Dr. " + [Link]("name") + "</h2>");
[Link]("<h3>Your Appointments:</h3>");
PreparedStatement ps2 = [Link](
"SELECT a.appointment_id, [Link] AS patient, a.appointment_date, a.appointment_time,
[Link] " +
"FROM appointments a JOIN patients p ON a.patient_id=p.patient_id " +
"WHERE a.doctor_id=? ORDER BY a.appointment_date, a.appointment_time");
[Link](1, docId);
ResultSet rs2 = [Link]();
[Link]("<table
border='1'><tr><th>ID</th><th>Patient</th><th>Date</th><th>Time</th><th>Status</
th><th>Action</th></tr>");
while ([Link]()) {
[Link]("<tr><td>" + [Link]("appointment_id") + "</td><td>" +
[Link]("patient") +
"</td><td>" + [Link]("appointment_date") + "</td><td>" +
[Link]("appointment_time") + "</td><td>" + [Link]("status") +
"</td>" +
"<td><form method='post' action='HospitalServlet' style='margin:0;'><input
type='hidden' name='action' value='updateStatus'>" +
"<input type='hidden' name='appointment_id' value='" +
[Link]("appointment_id") + "'>" +
"<select
name='new_status'><option>Booked</option><option>Confirmed</option><option>Completed</
option><option>Cancelled</option></select>" +
"<input type='submit' value='Update'></form></td></tr>");
}
[Link]("</table>");
} else {
[Link]("<h3>Invalid Doctor Login! <a href='doctor_login.html'>Try Again</a></h3>");
}
}
// Book appointment (with simple clash check)
else if ("book".equals(action)) {
int patientId = [Link]([Link]("patient_id"));
int doctorId = [Link]([Link]("doctor_id"));
String date = [Link]("date");
String time = [Link]("time");
// Check doctor availability: no same doctor, date, time with non-cancelled status
PreparedStatement check = [Link](
"SELECT COUNT(*) AS cnt FROM appointments WHERE doctor_id=? AND
appointment_date=? AND appointment_time=? AND status<>'Cancelled'");
[Link](1, doctorId);
[Link](2, date);
[Link](3, time);
ResultSet cr = [Link]();
if ([Link]() && [Link]("cnt") > 0) {
[Link]("<h3>Doctor not available at selected slot. Choose different time.</h3>");
[Link]("<a href='book_appointment.html'>Back</a>");
} else {
// Also check patient double-booking
PreparedStatement check2 = [Link](
"SELECT COUNT(*) AS cnt FROM appointments WHERE patient_id=? AND
appointment_date=? AND appointment_time=? AND status<>'Cancelled'");
[Link](1, patientId);
[Link](2, date);
[Link](3, time);
ResultSet cr2 = [Link]();
if ([Link]() && [Link]("cnt") > 0) {
[Link]("<h3>You already have an appointment at this time.</h3>");
[Link]("<a href='book_appointment.html'>Back</a>");
} else {
PreparedStatement ps = [Link](
"INSERT INTO appointments
(patient_id,doctor_id,appointment_date,appointment_time) VALUES(?,?,?,?)");
[Link](1, patientId);
[Link](2, doctorId);
[Link](3, date);
[Link](4, time);
[Link]();
[Link]("<h3>Appointment Booked!</h3>");
[Link]("<a href='[Link]'>Home</a>");
}
}
}
// View appointments
else if("view".equals(action)) {
int patientId = [Link]([Link]("patient_id"));
PreparedStatement ps = [Link](
"SELECT a.appointment_id,[Link] as
doctor_name,[Link],a.appointment_date,a.appointment_time,[Link] " +
"FROM appointments a JOIN doctors d ON a.doctor_id=d.doctor_id WHERE
a.patient_id=? ORDER BY a.appointment_date, a.appointment_time");
[Link](1,patientId);
ResultSet rs = [Link]();
[Link]("<h2>My Appointments</h2>");
[Link]("<table
border='1'><tr><th>ID</th><th>Doctor</th><th>Specialization</th><th>Date</th><th>Time</
th><th>Status</th></tr>");
boolean hasData=false;
while([Link]()){
hasData=true;
[Link]("<tr>");
[Link]("<td>"+[Link]("appointment_id")+"</td>");
[Link]("<td>"+[Link]("doctor_name")+"</td>");
[Link]("<td>"+[Link]("specialization")+"</td>");
[Link]("<td>"+[Link]("appointment_date")+"</td>");
[Link]("<td>"+[Link]("appointment_time")+"</td>");
[Link]("<td>"+[Link]("status")+"</td>");
[Link]("</tr>");
}
if(!hasData) [Link]("<tr><td colspan='6'>No Appointments Found</td></tr>");
[Link]("</table><br><a href='[Link]'>Home</a>");
}
// Cancel appointment (mark status)
else if ("cancel".equals(action)) {
int appointmentId = [Link]([Link]("appointment_id"));
PreparedStatement ps = [Link](
"UPDATE appointments SET status='Cancelled' WHERE appointment_id=?");
[Link](1, appointmentId);
int updated = [Link]();
if (updated > 0) {
[Link]("<h3>Appointment Cancelled Successfully!</h3>");
} else {
[Link]("<h3>Invalid Appointment ID!</h3>");
}
[Link]("<a href='[Link]'>Home</a>");
}
// Update status by doctor
else if ("updateStatus".equals(action)) {
int appointmentId = [Link]([Link]("appointment_id"));
String newStatus = [Link]("new_status");
PreparedStatement ps = [Link](
"UPDATE appointments SET status=? WHERE appointment_id=?");
[Link](1, newStatus);
[Link](2, appointmentId);
int updated = [Link]();
if (updated > 0) [Link]("<h3>Status updated to "+newStatus+"</h3>");
else [Link]("<h3>Unable to update status.</h3>");
[Link]("<a href='[Link]'>Home</a>");
}
// Admin login (simple)
else if("adminLogin".equals(action)) {
String user=[Link]("username");
String pass=[Link]("password");
PreparedStatement ps = [Link](
"SELECT * FROM admin WHERE username=? AND password=?");
[Link](1,user);
[Link](2,pass);
ResultSet rs = [Link]();
if([Link]()) [Link]("admin_dashboard.html");
else [Link]("<h3>Invalid Admin! <a href='admin_login.html'>Try Again</a></h3>");
}
} catch(Exception e){
[Link]("<h3>Error: "+[Link]()+"</h3>");
}
}
}
MySQL Code
Outputs: