如何利用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開發一個簡單的在線醫院預約平臺。通過數據庫設計和對應的Java代碼,我們可以實現醫院、科室、醫生和預約等信息的查詢和創建。當然,這只是一個簡單的示例,實際開發中還需考慮更多的功能和優化。希望本文能給讀者帶來一些啟發,而實際開發中需要結合具體需求進行詳細設計和實現。
以上就是如何利用MySQL和Java開發一個簡單的在線醫院預約平臺的詳細內容,更多請關注www.92cms.cn其它相關文章!