├── DatabaseConnection.java ├── Main.java ├── PatientDAO.java ├── Doctor.java ├── Patient.java └── Appointment.java /DatabaseConnection.java: -------------------------------------------------------------------------------- 1 | package Hospital; 2 | 3 | import java.sql.*; 4 | 5 | public class DatabaseConnection { 6 | private static final String URL = "jdbc:mysql://localhost:3306/hospital_db"; 7 | private static final String USER = "root"; 8 | private static final String PASSWORD = ""; 9 | 10 | public static Connection getConnection() throws SQLException { 11 | return DriverManager.getConnection(URL, USER, PASSWORD); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Main.java: -------------------------------------------------------------------------------- 1 | package Hospital; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Patient p1 = new Patient(1, "Krishna", 21, "Male"); 6 | 7 | try { 8 | PatientDAO dao = new PatientDAO(); 9 | dao.addPatient(p1); 10 | System.out.println("Patient added successfully."); 11 | } catch (Exception e) { 12 | System.out.println("Error: " + e.getMessage()); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /PatientDAO.java: -------------------------------------------------------------------------------- 1 | package Hospital; 2 | 3 | import java.sql.*; 4 | 5 | public class PatientDAO { 6 | public void addPatient(Patient patient) throws SQLException { 7 | Connection conn = DatabaseConnection.getConnection(); 8 | String query = "INSERT INTO patients (id, name, age, gender) VALUES (?, ?, ?, ?)"; 9 | PreparedStatement stmt = conn.prepareStatement(query); 10 | stmt.setInt(1, patient.getId()); 11 | stmt.setString(2, patient.getName()); 12 | stmt.setInt(3, patient.getAge()); 13 | stmt.setString(4, patient.getGender()); 14 | stmt.executeUpdate(); 15 | stmt.close(); 16 | conn.close(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Doctor.java: -------------------------------------------------------------------------------- 1 | package Hospital; 2 | 3 | public class Doctor { 4 | private int id; 5 | private String name; 6 | private String specialization; 7 | 8 | public Doctor(int id, String name, String specialization) { 9 | this.id = id; 10 | this.name = name; 11 | this.specialization = specialization; 12 | } 13 | 14 | public int getId() { 15 | return id; 16 | } 17 | 18 | public void setId(int id) { 19 | this.id = id; 20 | } 21 | 22 | public String getName() { 23 | return name; 24 | } 25 | 26 | public void setName(String name) { 27 | this.name = name; 28 | } 29 | 30 | public String getSpecialization() { 31 | return specialization; 32 | } 33 | 34 | public void setSpecialization(String specialization) { 35 | this.specialization = specialization; 36 | } 37 | } -------------------------------------------------------------------------------- /Patient.java: -------------------------------------------------------------------------------- 1 | package Hospital; 2 | 3 | public class Patient { 4 | private int id; 5 | private String name; 6 | private int age; 7 | private String gender; 8 | 9 | public Patient(int id, String name, int age, String gender) { 10 | this.id = id; 11 | this.name = name; 12 | this.age = age; 13 | this.gender = gender; 14 | } 15 | 16 | public int getId() { 17 | return id; 18 | } 19 | 20 | public void setId(int id) { 21 | this.id = id; 22 | } 23 | 24 | public String getName() { 25 | return name; 26 | } 27 | 28 | public void setName(String name) { 29 | this.name = name; 30 | } 31 | 32 | public int getAge() { 33 | return age; 34 | } 35 | 36 | public void setAge(int age) { 37 | this.age = age; 38 | } 39 | 40 | public String getGender() { 41 | return gender; 42 | } 43 | 44 | public void setGender(String gender) { 45 | this.gender = gender; 46 | } 47 | } -------------------------------------------------------------------------------- /Appointment.java: -------------------------------------------------------------------------------- 1 | package Hospital; 2 | 3 | public class Appointment { 4 | private int id; 5 | private int patientId; 6 | private int doctorId; 7 | private String date; 8 | 9 | public Appointment(int id, int patientId, int doctorId, String date) { 10 | this.id = id; 11 | this.patientId = patientId; 12 | this.doctorId = doctorId; 13 | this.date = date; 14 | } 15 | 16 | public int getId() { 17 | return id; 18 | } 19 | 20 | public void setId(int id) { 21 | this.id = id; 22 | } 23 | 24 | public int getPatientId() { 25 | return patientId; 26 | } 27 | 28 | public void setPatientId(int patientId) { 29 | this.patientId = patientId; 30 | } 31 | 32 | public int getDoctorId() { 33 | return doctorId; 34 | } 35 | 36 | public void setDoctorId(int doctorId) { 37 | this.doctorId = doctorId; 38 | } 39 | 40 | public String getDate() { 41 | return date; 42 | } 43 | 44 | public void setDate(String date) { 45 | this.date = date; 46 | } 47 | } --------------------------------------------------------------------------------