Write a code on Add button for Hospital Management System project

Summary

This postmortem analyzes a failure in a Java Swing + MySQL Hospital Management System where clicking the Add button did not insert patient data and occasionally threw exceptions. The issue stemmed from a combination of incorrect JDBC driver class name, silent parsing failures, and missing database constraints awareness.

Root Cause

The primary root cause was using the wrong MySQL JDBC driver class name:

  • The code used: com.mysql.cj.jdbc.driver
  • The correct class is: com.mysql.cj.jdbc.Driver (uppercase D)

Additional contributing issues:

  • Unvalidated user input causing NumberFormatException
  • Insert query without column names, which breaks when table schema changes
  • Lack of logging, making debugging harder
  • Swallowing exceptions inside a generic catch block

Why This Happens in Real Systems

Real production systems often fail for similar reasons:

  • Case-sensitive class names in Java lead to runtime failures
  • Developers assume input is always valid
  • Database schemas evolve, but code using INSERT INTO table VALUES(...) does not
  • GUI applications hide stack traces, masking the real error
  • Driver loading issues are common when switching MySQL versions

Real-World Impact

Failures like this can cause:

  • Data loss because inserts silently fail
  • Inconsistent patient records, affecting medical workflows
  • Support tickets and downtime
  • Developer confusion due to misleading error dialogs

Example or Code (if necessary and relevant)

Below is a corrected version of the button handler using proper driver loading, validation, and safer SQL:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    try {
        int patientId = Integer.parseInt(patientid.getText());
        int patientAge = Integer.parseInt(patientAge.getText());
        String pname = patientName.getText();
        String dname = doctorName.getText();

        Class.forName("com.mysql.cj.jdbc.Driver");

        try (Connection con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/HospitalManagementSystem",
                "root",
                "0000")) {

            String query = "INSERT INTO patient (id, name, age, doctor) VALUES (?, ?, ?, ?)";
            PreparedStatement pstmt = con.prepareStatement(query);

            pstmt.setInt(1, patientId);
            pstmt.setString(2, pname);
            pstmt.setInt(3, patientAge);
            pstmt.setString(4, dname);

            pstmt.executeUpdate();
        }

        JOptionPane.showMessageDialog(this, "Data Inserted");
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, e.getMessage());
    }
}

How Senior Engineers Fix It

Experienced engineers typically:

  • Validate all user input before parsing
  • Use explicit column lists in SQL inserts
  • Load JDBC drivers correctly
  • Log full stack traces instead of showing generic dialogs
  • Add database constraints to prevent invalid data
  • Write integration tests to catch DB failures early

Why Juniors Miss It

Junior developers often overlook:

  • Case sensitivity in Java class names
  • The importance of explicit SQL column names
  • The need for input validation before parsing integers
  • How driver loading works in JDBC
  • Why generic catch blocks hide the real problem

They tend to focus on the GUI behavior and assume the backend “just works,” which leads to subtle bugs slipping through.

Leave a Comment