Class Database

java.lang.Object
com.ehealthsystem.database.Database

public class Database extends Object
Class for for database communication (reading and writing data)
  • Field Details

  • Constructor Details

    • Database

      public Database()
  • Method Details

    • init

      public static void init()
      To be called on application start. Establishes connection to the database. Creates a database if it doesn't exist yet. Before creating a database, it asks for an admin password.
    • createDB

      public static void createDB(String initialAdminPassword) throws SQLException
      Create database with provided admin password
      Parameters:
      initialAdminPassword - provided admin password desired by the user
      Throws:
      SQLException - if a creation step failed
    • hashPassword

      public static String hashPassword(String password)
      Helper method to generate a password's hash
      Parameters:
      password - the password to hash
      Returns:
      hash
    • close

      public static void close()
      Properly closes the connection to the database Probably only needed if a big query would still be running
    • checkPassword

      public static boolean checkPassword(String usernameOrEmail, String password) throws SQLException
      Check if user entered their correct password Retrieves stored password and verifies that the entered one matches the stored hash This method is not part of the User class because there isn't a user object during login yet
      Parameters:
      usernameOrEmail - since a user can enter both username or email in the text field
      password - in plain text
      Returns:
      whether password is correct
      Throws:
      SQLException
    • insert

      public static int insert(String tableName, Object[][] parameters) throws SQLException, javax.activation.UnsupportedDataTypeException
      General method for inserting a single row into a database table
      Parameters:
      tableName - the name of the table to which the row shall be added
      parameters - array consisting of pairs of column name and value
      Returns:
      id of inserted row
      Throws:
      SQLException - if insert failed or succeeded but no generated ID was received
      javax.activation.UnsupportedDataTypeException
    • update

      public static void update(String tableName, Object[][] newValues, Object[][] conditions) throws SQLException, javax.activation.UnsupportedDataTypeException
      General method for updating rows in a database table
      Parameters:
      tableName - the name of the table where the row is to be found
      newValues - array consisting of pairs of column name and value, these values will be set on the matching rows
      conditions - array consisting of pairs of column name and value, rows have to match these criteria
      Throws:
      SQLException
      javax.activation.UnsupportedDataTypeException
    • insertValueIntoStatement

      private static void insertValueIntoStatement(int i, Object value, PreparedStatement statement) throws SQLException, javax.activation.UnsupportedDataTypeException
      General method for setting a value for a placeholder in a prepared statement
      Parameters:
      i - index of the placeholder
      value - the value to set for the placeholder
      statement - the query to apply the operation to
      Throws:
      SQLException
      javax.activation.UnsupportedDataTypeException
    • getAllUsers

      public static ArrayList<User> getAllUsers() throws SQLException, javax.activation.UnsupportedDataTypeException
      Get all rows of users table from the database For admin GUI
      Returns:
      all users
      Throws:
      SQLException
      javax.activation.UnsupportedDataTypeException
    • getUserByUsername

      public static User getUserByUsername(String username) throws SQLException, javax.activation.UnsupportedDataTypeException
      Get a user object with all user's details loaded from the database
      Parameters:
      username -
      Returns:
      Throws:
      SQLException
      javax.activation.UnsupportedDataTypeException
    • isUsernameTaken

      public static boolean isUsernameTaken(String username) throws SQLException
      Check, if a username is already taken, used for registration
      Parameters:
      username - the username to check
      Returns:
      whether the username is already taken
      Throws:
      SQLException
    • isEmailTaken

      public static boolean isEmailTaken(String email) throws SQLException
      Check, if an email is already taken, used for registration
      Parameters:
      email - the email address to check
      Returns:
      whether the email is already in use
      Throws:
      SQLException
    • getUserByUsernameOrEmail

      public static User getUserByUsernameOrEmail(String usernameOrEmail) throws SQLException, javax.activation.UnsupportedDataTypeException
      Get a user object with all user's details loaded from the database
      Parameters:
      usernameOrEmail - since a user can enter both username or email in the text field
      Returns:
      Throws:
      SQLException
      javax.activation.UnsupportedDataTypeException
    • getDoctors

      public static ArrayList<Doctor> getDoctors() throws SQLException
      Get all doctors from the database as objects of class Doctor
      Returns:
      all doctors
      Throws:
      SQLException - if query fails
    • getDoctorsBySpecialization

      public static ArrayList<Doctor> getDoctorsBySpecialization(String specialization) throws SQLException
      Get all doctors that have a specified specialization from the database as objects of class Doctor
      Returns:
      all doctors with the specified specialization
      Throws:
      SQLException - if query fails
    • loadDoctorsFromResultSet

      private static ArrayList<Doctor> loadDoctorsFromResultSet(ResultSet rs) throws SQLException
      Helper method to turn result set into array of Doctor objects
      Parameters:
      rs - resultSet after the query was executed
      Returns:
      Doctor objects
      Throws:
      SQLException - if reading an attribute fails
    • getHealthInformation

      public static ArrayList<HealthInformation> getHealthInformation(String email) throws SQLException
      Get health statuses of a user
      Parameters:
      email - User's email address
      Throws:
      SQLException
    • loadUsersFromResultSet

      private static ArrayList<User> loadUsersFromResultSet(ResultSet rs) throws SQLException, javax.activation.UnsupportedDataTypeException
      Helper method to turn result set into array of User objects
      Parameters:
      rs - resultSet after the query was executed
      Returns:
      User objects
      Throws:
      SQLException - if reading an attribute fails
      javax.activation.UnsupportedDataTypeException
    • loadDoctorFromId

      public static Doctor loadDoctorFromId(int doctorId) throws SQLException
      Get a doctor by their ID
      Parameters:
      doctorId - the numeric identifier for the doctor
      Returns:
      the requested doctor
      Throws:
      SQLException
    • loadDoctorSpecializations

      public static ArrayList<String> loadDoctorSpecializations(int id) throws SQLException
      Get a list of a doctor's specializations
      Parameters:
      id - the numeric identifier for the doctor
      Returns:
      list of names of their specializations
      Throws:
      SQLException
    • loadSpecializations

      public static ArrayList<String> loadSpecializations() throws SQLException
      Get a list of all specializations that doctors can have
      Returns:
      specialization names
      Throws:
      SQLException
    • loadProblems

      public static ArrayList<String> loadProblems() throws SQLException
      Get list of all potential health problems based on which you can select a doctor
      Returns:
      problem names
      Throws:
      SQLException
    • problemToSuitableSpecialization

      public static String problemToSuitableSpecialization(String problem) throws SQLException
      Get the suitable specialization for a provided health problem
      Parameters:
      problem - the health problem to get a suitable specialization for
      Returns:
      the suitable specialization for that problem
      Throws:
      SQLException
    • getDoctorsFreeTimes

      public static ArrayList<DoctorTimeSlot> getDoctorsFreeTimes(Doctor doctor, LocalDate selectedDate) throws SQLException
      Deprecated.
      : FoundDoctorFullController.getFreeTimeSlots() is used instead
      Throws:
      SQLException
    • loadAppointmentsFromResultSet

      public static ArrayList<Appointment> loadAppointmentsFromResultSet(ResultSet rs) throws SQLException, javax.activation.UnsupportedDataTypeException
      Helper method to turn result set into array of appointment objects
      Parameters:
      rs - resultSet after the query was executed
      Returns:
      list of appointments, may be empty
      Throws:
      SQLException - if reading an attribute fails
      javax.activation.UnsupportedDataTypeException
    • getDoctorsAppointments

      public static ArrayList<Appointment> getDoctorsAppointments(int doctor, LocalDate date) throws SQLException, javax.activation.UnsupportedDataTypeException
      Get appointments that a doctor already has within a range of days To be used to display a doctor's timetable to the patient, to find a free time for their appointment
      Parameters:
      doctor -
      date -
      Returns:
      doctorsAppointments
      Throws:
      SQLException - if reading an attribute fails
      javax.activation.UnsupportedDataTypeException
    • getUsersAppointments

      public static ArrayList<Appointment> getUsersAppointments(String username) throws SQLException, javax.activation.UnsupportedDataTypeException
      Get a user's past and future appointments, ordered by appointment time (newest first) To be used for the patient to see their appointments. This method is not part of the User class because the content is so similar to DB.getDoctorsAppointments() and hence shall be next to it
      Returns:
      usersAppointments
      Throws:
      SQLException
      javax.activation.UnsupportedDataTypeException
    • getUpcomingAppointmentsWithReminder

      public static ArrayList<Appointment> getUpcomingAppointmentsWithReminder() throws SQLException, javax.activation.UnsupportedDataTypeException
      Get upcoming appointments that have a reminder. To be used to set reminders initially (application start).
      Throws:
      SQLException
      javax.activation.UnsupportedDataTypeException
    • getUserForTableView

      public static javafx.collections.ObservableList<UserTableView> getUserForTableView() throws SQLException
      Load users into a table view for the Admin GUI
      Throws:
      SQLException
    • getPassword

      public static String getPassword(String email) throws SQLException
      Get a users password to display in the admin GUI
      Parameters:
      email -
      Returns:
      Throws:
      SQLException
    • getHealthInformationForTableView

      public static javafx.collections.ObservableList<HealthInformationTableView> getHealthInformationForTableView(String username) throws SQLException
      Get a users current health statuses so that they can manage theirs
      Parameters:
      username - the user to get health statuses for
      Throws:
      SQLException
    • deleteHealthInformation

      public static void deleteHealthInformation(HealthInformationTableView selected, String username) throws SQLException
      Delete a user's health status
      Parameters:
      selected - the selected table view from which the health status to delete is read out from
      username - the concerned user's name
      Throws:
      SQLException
    • getDisease

      public static ArrayList<String> getDisease() throws SQLException
      Get all possible diseases. Used by the UI for the patient to add a disease to their health information.
      Returns:
      list of all possible disease
      Throws:
      SQLException
    • addHealthInformation

      public static void addHealthInformation(String selected, String username) throws SQLException
      Add a disease to a user's health information
      Parameters:
      selected -
      username -
      Throws:
      SQLException
    • deleteUserInformation

      public static void deleteUserInformation(UserTableView selectedRow, String username) throws SQLException
      Delete a disease from a user's health information
      Parameters:
      selectedRow - the selected row from which the health information to delete is derived from
      username - the name of the user from which to delete this health information from
      Throws:
      SQLException