Class BaseDAO<T>

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.sql.Connection connection  
    • Constructor Summary

      Constructors 
      Constructor Description
      BaseDAO()
      Constructor that establishes a database connection
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void delete​(int id)
      Deletes a record from the database
      java.util.List<T> findAll()
      Retrieves all records from the database table
      T findById​(int id)
      Retrieves a single record by its ID
      protected abstract java.lang.String[] getColumnNames()
      Gets the column names for the database table
      protected abstract java.lang.String getTableName()
      Gets the name of the database table this DAO operates on
      void insert​(T entity)
      Inserts a new record into the database Note: Derby uses GENERATED ALWAYS AS IDENTITY for auto-incrementing IDs
      protected abstract T mapResultSetToEntity​(java.sql.ResultSet rs)
      Maps a ResultSet row to an entity object
      protected abstract void setPreparedStatementParams​(java.sql.PreparedStatement ps, T entity)
      Sets the parameters for a PreparedStatement based on entity properties
      void update​(T entity, int id)
      Updates an existing record in the database
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • connection

        protected java.sql.Connection connection
    • Constructor Detail

      • BaseDAO

        public BaseDAO()
        Constructor that establishes a database connection
        Throws:
        java.lang.RuntimeException - if database connection fails
    • Method Detail

      • getTableName

        protected abstract java.lang.String getTableName()
        Gets the name of the database table this DAO operates on
        Returns:
        The table name
      • getColumnNames

        protected abstract java.lang.String[] getColumnNames()
        Gets the column names for the database table
        Returns:
        Array of column names
      • mapResultSetToEntity

        protected abstract T mapResultSetToEntity​(java.sql.ResultSet rs)
                                           throws java.sql.SQLException
        Maps a ResultSet row to an entity object
        Parameters:
        rs - The ResultSet containing the database row
        Returns:
        The mapped entity object
        Throws:
        java.sql.SQLException - if a database error occurs
      • setPreparedStatementParams

        protected abstract void setPreparedStatementParams​(java.sql.PreparedStatement ps,
                                                           T entity)
                                                    throws java.sql.SQLException
        Sets the parameters for a PreparedStatement based on entity properties
        Parameters:
        ps - The PreparedStatement to set parameters for
        entity - The entity containing the values to set
        Throws:
        java.sql.SQLException - if a database error occurs
      • findAll

        public java.util.List<T> findAll()
                                  throws java.sql.SQLException
        Retrieves all records from the database table
        Returns:
        A list of all entities
        Throws:
        java.sql.SQLException - if a database error occurs
      • findById

        public T findById​(int id)
                   throws java.sql.SQLException
        Retrieves a single record by its ID
        Parameters:
        id - The ID of the record to retrieve
        Returns:
        The entity with the specified ID, or null if not found
        Throws:
        java.sql.SQLException - if a database error occurs
      • insert

        public void insert​(T entity)
                    throws java.sql.SQLException
        Inserts a new record into the database Note: Derby uses GENERATED ALWAYS AS IDENTITY for auto-incrementing IDs
        Parameters:
        entity - The entity to insert
        Throws:
        java.sql.SQLException - if a database error occurs
      • update

        public void update​(T entity,
                           int id)
                    throws java.sql.SQLException
        Updates an existing record in the database
        Parameters:
        entity - The entity with updated values
        id - The ID of the record to update
        Throws:
        java.sql.SQLException - if a database error occurs
      • delete

        public void delete​(int id)
                    throws java.sql.SQLException
        Deletes a record from the database
        Parameters:
        id - The ID of the record to delete
        Throws:
        java.sql.SQLException - if a database error occurs