如何利用MySQL和Java开发一个简单的在线医院预约平台

WBOY
WBOY 原创
2023-09-20 10:03:19 310浏览

如何利用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. 查询医生信息
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中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。