Class UserDAO
- All Implemented Interfaces:
IUserDAO
User
entities in the SQLite database.
This Data Access Object (DAO) provides methods to perform CRUD operations on the
users
table in the SQLite database. Users are identified by a unique ID and
username, with a stored password hash, supporting authentication and user management
within the application.
- Author:
- Joshua M.
-
Constructor Summary
ConstructorsConstructorDescriptionUserDAO
(SQLiteConnection sqliteConnection) Creates aUserDAO
table with an SQLite database connection. -
Method Summary
Modifier and TypeMethodDescriptionvoid
createUser
(User user) Inserts a newUser
entity to the database.void
deleteUser
(User user) Removes aUser
entity from the database.Retrieves allUser
entities from the database.getUser
(int id) Retrieves aUser
entity by its ID.Retrieves aUser
entity by its username.void
updateUser
(User user) Updates an existingUser
entity in the database.
-
Constructor Details
-
UserDAO
Creates aUserDAO
table with an SQLite database connection.- Parameters:
sqliteConnection
- theSQLiteConnection
instance for database access- Throws:
SQLException
- if a database error occurs during initialisationRuntimeException
- if the SQLite connection cannot be established
-
-
Method Details
-
createUser
Inserts a newUser
entity to the database.This method inserts a
User
entity into theusers
table, storing its username and password hash. The user ID is automatically generated by the SQLite database and set on theUser
entity to ensure uniqueness.- Specified by:
createUser
in interfaceIUserDAO
- Parameters:
user
- theUser
entity to save- Throws:
SQLException
- if a database error occurs during insertion
-
updateUser
Updates an existingUser
entity in the database.This method updates the username and password hash of a
User
entity in theusers
table, identified by the user’s ID.- Specified by:
updateUser
in interfaceIUserDAO
- Parameters:
user
- theUser
entity to update- Throws:
SQLException
- if a database error occurs during update
-
deleteUser
Removes aUser
entity from the database.This method removes a
User
entity from theusers
table identified by the user’s ID. Cascading deletions in thechats
andmessages
tables, handled by foreign key constraints.- Specified by:
deleteUser
in interfaceIUserDAO
- Parameters:
user
- theUser
entity to delete- Throws:
SQLException
- if a database error occurs during deletion
-
getUser
Retrieves aUser
entity by its ID.This method fetches a single
User
entity from theusers
table that matches the specified ID. Returnsnull
if no user is found for the given ID.- Specified by:
getUser
in interfaceIUserDAO
- Parameters:
id
- the ID of the user- Returns:
- the
User
entity, ornull
if none exists - Throws:
IllegalArgumentException
- ifid
is negativeSQLException
- if a database error occurs during retrieval
-
getUser
Retrieves aUser
entity by its username.This method fetches a single
User
entity from theusers
table that matches the specified username. Returnsnull
if no user is found for the given username.- Specified by:
getUser
in interfaceIUserDAO
- Parameters:
username
- the username of the user- Returns:
- the
User
entity, ornull
if none exists - Throws:
IllegalArgumentException
- ifusername
isnull
or emptySQLException
- if a database error occurs during retrieval
-
getAllUsers
Retrieves allUser
entities from the database.This method fetches all users from the
users
table. Returns a list ofUser
entities, which may be empty if no users are found.- Specified by:
getAllUsers
in interfaceIUserDAO
- Returns:
- a
List
ofUser
entities, or an empty list if none exist - Throws:
SQLException
- if a database error occurs during retrieval
-