如何利用MySQL和Java開發一個簡單的線上醫院預約平台
#隨著社會發展和醫療水平提高,人們對醫療資源的需求也越來越大。為了滿足人們的就醫需求,開發一個簡單的線上醫院預約平台顯得十分必要。本文將介紹如何利用MySQL和Java來實現這一目標,並提供具體的程式碼範例。
首先需要設計資料庫的結構,以儲存醫院、科室、醫生和預約等資訊。一個簡單的資料庫設計如下:
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
在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); } }
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; } }
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; } }
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開發一個簡單的線上醫院預約平台的詳細內容。更多資訊請關注PHP中文網其他相關文章!