Java Project – Patient Record System
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
SQL Queries
CREATE DATABASE IF NOT EXISTS hospital_db;
USE hospital_db;
CREATE TABLE IF NOT EXISTS patients (
patient_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
age INT,
gender VARCHAR(10),
contact VARCHAR(15)
);
CREATE TABLE IF NOT EXISTS visits (
visit_id INT AUTO_INCREMENT PRIMARY KEY,
patient_id INT,
visit_date DATE,
reason VARCHAR(255),
diagnosis VARCHAR(255),
treatment TEXT,
FOREIGN KEY (patient_id) REFERENCES patients(patient_id)
);
CREATE TABLE IF NOT EXISTS payments (
payment_id INT AUTO_INCREMENT PRIMARY KEY,
visit_id INT,
amount DECIMAL(10,2),
method VARCHAR(50),
payment_date DATE,
FOREIGN KEY (visit_id) REFERENCES visits(visit_id)
);Program 1
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
public class PatientRecordSystem extends JFrame {
Connection con;
Statement stmt;
JTextField nameField, ageField, genderField, contactField;
JTextField reasonField, diagnosisField, treatmentField, amountField, methodField;
JButton addPatientButton, addVisitButton, addPaymentButton, deletePatientButton, searchPatientButton;
public PatientRecordSystem() {
setTitle("Patient Record System");
setSize(500, 600);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
connectDatabase();
JLabel nameLabel = new JLabel("Name:");
JLabel ageLabel = new JLabel("Age:");
JLabel genderLabel = new JLabel("Gender:");
JLabel contactLabel = new JLabel("Contact:");
nameField = new JTextField();
ageField = new JTextField();
genderField = new JTextField();
contactField = new JTextField();
addPatientButton = new JButton("Add Patient");
nameLabel.setBounds(30, 30, 100, 25); nameField.setBounds(150, 30, 200, 25);
ageLabel.setBounds(30, 70, 100, 25); ageField.setBounds(150, 70, 200, 25);
genderLabel.setBounds(30, 110, 100, 25); genderField.setBounds(150, 110, 200, 25);
contactLabel.setBounds(30, 150, 100, 25); contactField.setBounds(150, 150, 200, 25);
addPatientButton.setBounds(150, 190, 200, 30);
add(nameLabel); add(nameField); add(ageLabel); add(ageField);
add(genderLabel); add(genderField); add(contactLabel); add(contactField);
add(addPatientButton);
JLabel reasonLabel = new JLabel("Visit Reason:");
JLabel diagnosisLabel = new JLabel("Diagnosis:");
JLabel treatmentLabel = new JLabel("Treatment:");
reasonField = new JTextField();
diagnosisField = new JTextField();
treatmentField = new JTextField();
addVisitButton = new JButton("Add Visit (Patient ID 1)");
reasonLabel.setBounds(30, 240, 100, 25); reasonField.setBounds(150, 240, 300, 25);
diagnosisLabel.setBounds(30, 280, 100, 25); diagnosisField.setBounds(150, 280, 300, 25);
treatmentLabel.setBounds(30, 320, 100, 25); treatmentField.setBounds(150, 320, 300, 25);
addVisitButton.setBounds(150, 360, 300, 30);
add(reasonLabel); add(reasonField); add(diagnosisLabel); add(diagnosisField);
add(treatmentLabel); add(treatmentField); add(addVisitButton);
JLabel amountLabel = new JLabel("Amount:");
JLabel methodLabel = new JLabel("Method:");
amountField = new JTextField();
methodField = new JTextField();
addPaymentButton = new JButton("Add Payment (Visit ID 1)");
amountLabel.setBounds(30, 410, 100, 25); amountField.setBounds(150, 410, 200, 25);
methodLabel.setBounds(30, 450, 100, 25); methodField.setBounds(150, 450, 200, 25);
addPaymentButton.setBounds(150, 490, 200, 30);
add(amountLabel); add(amountField); add(methodLabel); add(methodField);
add(addPaymentButton);
// Delete and Search Section
deletePatientButton = new JButton("Delete Patient by ID");
searchPatientButton = new JButton("Search Patient by ID");
deletePatientButton.setBounds(30, 530, 200, 30);
searchPatientButton.setBounds(260, 530, 200, 30);
add(deletePatientButton);
add(searchPatientButton);
addPatientButton.addActionListener(e -> addPatient());
addVisitButton.addActionListener(e -> addVisit(1));
addPaymentButton.addActionListener(e -> addPayment(1));
deletePatientButton.addActionListener(e -> {
String pid = JOptionPane.showInputDialog("Enter Patient ID to Delete:");
deletePatientById(pid);
});
searchPatientButton.addActionListener(e -> {
String pid = JOptionPane.showInputDialog("Enter Patient ID to Search:");
searchPatientById(pid);
});
setVisible(true);
}
void connectDatabase() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/hospital_db", "root", "");
stmt = con.createStatement();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "DB Error: " + ex.getMessage());
}
}
void addPatient() {
try {
String query = "INSERT INTO patients (name, age, gender, contact) VALUES ('" +
nameField.getText() + "', " + Integer.parseInt(ageField.getText()) + ", '" +
genderField.getText() + "', '" + contactField.getText() + "')";
stmt.executeUpdate(query);
JOptionPane.showMessageDialog(this, "Patient added.");
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error adding patient: " + ex.getMessage());
}
}
void addVisit(int patientId) {
try {
String query = "INSERT INTO visits (patient_id, visit_date, reason, diagnosis, treatment) VALUES (" +
patientId + ", CURDATE(), '" + reasonField.getText() + "', '" + diagnosisField.getText() +
"', '" + treatmentField.getText() + "')";
stmt.executeUpdate(query);
JOptionPane.showMessageDialog(this, "Visit added for patient ID " + patientId);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error adding visit: " + ex.getMessage());
}
}
void addPayment(int visitId) {
try {
String query = "INSERT INTO payments (visit_id, amount, method, payment_date) VALUES (" +
visitId + ", " + Double.parseDouble(amountField.getText()) + ", '" +
methodField.getText() + "', CURDATE())";
stmt.executeUpdate(query);
JOptionPane.showMessageDialog(this, "Payment added for visit ID " + visitId);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error adding payment: " + ex.getMessage());
}
}
void deletePatientById(String pid) {
try {
int patientId = Integer.parseInt(pid);
int confirm = JOptionPane.showConfirmDialog(this, "Are you sure to delete patient ID " + patientId + "?");
if (confirm == JOptionPane.YES_OPTION) {
stmt.executeUpdate("DELETE FROM payments WHERE visit_id IN (SELECT visit_id FROM visits WHERE patient_id = " + patientId + ")");
stmt.executeUpdate("DELETE FROM visits WHERE patient_id = " + patientId);
stmt.executeUpdate("DELETE FROM patients WHERE patient_id = " + patientId);
JOptionPane.showMessageDialog(this, "Patient and related data deleted.");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error deleting patient: " + ex.getMessage());
}
}
void searchPatientById(String pid) {
try {
int patientId = Integer.parseInt(pid);
ResultSet rs = stmt.executeQuery("SELECT * FROM patients WHERE patient_id = " + patientId);
if (rs.next()) {
String info = "Name: " + rs.getString("name") + "\nAge: " + rs.getInt("age") + "\nGender: " + rs.getString("gender") + "\nContact: " + rs.getString("contact");
JOptionPane.showMessageDialog(this, info);
} else {
JOptionPane.showMessageDialog(this, "No patient found with ID " + patientId);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error searching patient: " + ex.getMessage());
}
}
public static void main(String[] args) {
new PatientRecordSystem();
}
}
Your opinion matters
Please write your valuable feedback about DataFlair on Google

