首頁 > 資料庫 > mysql教程 > 如何利用MySQL和Java開發一個簡單的線上醫院預約平台

如何利用MySQL和Java開發一個簡單的線上醫院預約平台

WBOY
發布: 2023-09-20 10:03:19
原創
736 人瀏覽過

如何利用MySQL和Java開發一個簡單的線上醫院預約平台

如何利用MySQL和Java開發一個簡單的線上醫院預約平台

#隨著社會發展和醫療水平提高,人們對醫療資源的需求也越來越大。為了滿足人們的就醫需求,開發一個簡單的線上醫院預約平台顯得十分必要。本文將介紹如何利用MySQL和Java來實現這一目標,並提供具體的程式碼範例。

  1. 資料庫設計

首先需要設計資料庫的結構,以儲存醫院、科室、醫生和預約等資訊。一個簡單的資料庫設計如下:

1.1 醫院表(hospital)
欄位:id, name, address, phone

1.2 科室表(department)
欄位:id, hospital_id, name

1.3 醫生表(doctor)
字段:id, department_id, name, title, introduction

1.4 預約表(appointment)
字段:id, doctor_id, patient_name, patient_phone, appointment_date

  1. 資料庫連線

在Java中,我們可以使用JDBC來連接MySQL資料庫。以下是一個簡單的資料庫連接程式碼範例:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseUtil {
    private static final String URL = "jdbc:mysql://localhost:3306/hospital";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "password";

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USERNAME, PASSWORD);
    }
}
登入後複製
  1. 查詢醫院和科室資訊
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class HospitalDao {
    public List<Hospital> getAllHospitals() {
        List<Hospital> hospitals = new ArrayList<>();
        String sql = "SELECT * FROM hospital";
        try (Connection connection = DatabaseUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql);
             ResultSet resultSet = statement.executeQuery()) {
            while (resultSet.next()) {
                Hospital hospital = new Hospital();
                hospital.setId(resultSet.getInt("id"));
                hospital.setName(resultSet.getString("name"));
                hospital.setAddress(resultSet.getString("address"));
                hospital.setPhone(resultSet.getString("phone"));
                hospitals.add(hospital);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return hospitals;
    }

    public List<Department> getDepartmentsByHospitalId(int hospitalId) {
        List<Department> departments = new ArrayList<>();
        String sql = "SELECT * FROM department WHERE hospital_id = ?";
        try (Connection connection = DatabaseUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setInt(1, hospitalId);
            try (ResultSet resultSet = statement.executeQuery()) {
                while (resultSet.next()) {
                    Department department = new Department();
                    department.setId(resultSet.getInt("id"));
                    department.setHospitalId(hospitalId);
                    department.setName(resultSet.getString("name"));
                    departments.add(department);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return departments;
    }
}
登入後複製
  1. 查詢醫生資訊
  2. ##
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class DoctorDao {
        public List<Doctor> getDoctorsByDepartmentId(int departmentId) {
            List<Doctor> doctors = new ArrayList<>();
            String sql = "SELECT * FROM doctor WHERE department_id = ?";
            try (Connection connection = DatabaseUtil.getConnection();
                 PreparedStatement statement = connection.prepareStatement(sql)) {
                statement.setInt(1, departmentId);
                try (ResultSet resultSet = statement.executeQuery()) {
                    while (resultSet.next()) {
                        Doctor doctor = new Doctor();
                        doctor.setId(resultSet.getInt("id"));
                        doctor.setDepartmentId(departmentId);
                        doctor.setName(resultSet.getString("name"));
                        doctor.setTitle(resultSet.getString("title"));
                        doctor.setIntroduction(resultSet.getString("introduction"));
                        doctors.add(doctor);
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return doctors;
        }
    }
    登入後複製
    建立預約
  1. import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.util.Date;
    
    public class AppointmentDao {
        public void createAppointment(int doctorId, String patientName, String patientPhone, Date appointmentDate) {
            String sql = "INSERT INTO appointment (doctor_id, patient_name, patient_phone, appointment_date) " +
                    "VALUES (?, ?, ?, ?)";
            try (Connection connection = DatabaseUtil.getConnection();
                 PreparedStatement statement = connection.prepareStatement(sql)) {
                statement.setInt(1, doctorId);
                statement.setString(2, patientName);
                statement.setString(3, patientPhone);
                statement.setDate(4, new java.sql.Date(appointmentDate.getTime()));
                statement.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    登入後複製
    以上程式碼範例示範如何使用MySQL和Java開發一個簡單的線上醫院預約平台。透過資料庫設計和對應的Java代碼,我們可以實現醫院、科室、醫生和預約等資訊的查詢和創建。當然,這只是一個簡單的範例,實際開發中還需考慮更多的功能和最佳化。希望本文能帶給讀者一些啟發,而實際開發中需要結合具體需求來進行詳細設計與實現。

    以上是如何利用MySQL和Java開發一個簡單的線上醫院預約平台的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板