-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Seperate File for Database Connection
- Loading branch information
AdrianBalunan
committed
Mar 11, 2026
1 parent
6c509f0
commit 8bce5de
Showing
1 changed file
with
96 additions
and
0 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
...ehelpapplication/src/main/java/ntnu/sytemutvikling/team6/database/DatabaseConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package ntnu.sytemutvikling.team6.database; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.DriverManager; | ||
| import java.sql.SQLException; | ||
|
|
||
| /** | ||
| * Represents a reusable database connection through enviroment variables. | ||
| */ | ||
|
|
||
| public class DatabaseConnection { | ||
| private final String databaseURL; | ||
| private final String username; | ||
| private final String password; | ||
| /** | ||
| * Constructs a new {@code DatabaseConnection} using database credentials | ||
| * retrieved from system environment variables. | ||
| * <p> | ||
| * Required environment variables: | ||
| * <ul> | ||
| * <li>{@code HMH_DB_URL}</li> | ||
| * <li>{@code HMH_DB_USERNAME}</li> | ||
| * <li>{@code HMH_DB_PASSWORD}</li> | ||
| * </ul> | ||
| * </p> | ||
| * | ||
| * @throws IllegalStateException if either databaseURL, username, or password is {@code null} or blank | ||
| */ | ||
|
|
||
| // Values stored in system environment variables for security (using ntnu's phpmyadmin for this project) | ||
| public DatabaseConnection() { | ||
| this.databaseURL = System.getenv("HMH_DB_URL"); | ||
| this.username = System.getenv("HMH_DB_USERNAME"); | ||
| this.password = System.getenv("HMH_DB_PASSWORD"); | ||
|
|
||
| if (this.databaseURL == null || this.databaseURL.isBlank()) { | ||
| throw new IllegalStateException("Database environment variable URL has not been set"); | ||
| } | ||
|
|
||
| if (this.username == null || this.username.isBlank()) { | ||
| throw new IllegalStateException("Username environment variable has not been set"); | ||
| } | ||
|
|
||
| if (this.password == null || this.password.isBlank()) { | ||
| throw new IllegalStateException("Password environment variable has not been set"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Constructs a new {@code DatabaseConnection} using database credentials | ||
| * <p> | ||
| * Used primarily for JUnit tests. Production should use the constructor | ||
| * using environment variables. | ||
| * </p> | ||
| * | ||
| * @param databaseURL the url to the database | ||
| * @param username the username used to log in to the database | ||
| * @param password the password used to log in to the database | ||
| * @throws IllegalArgumentException if databaseURL, username, or password is | ||
| * {@code null} or blank | ||
| */ | ||
|
|
||
| public DatabaseConnection(String databaseURL, String username, String password) { | ||
| this.databaseURL = databaseURL; | ||
| this.username = username; | ||
| this.password = password; | ||
|
|
||
| if (this.databaseURL == null || this.databaseURL.isBlank()) { | ||
| throw new IllegalArgumentException("Database environment variable URL has not been set"); | ||
| } | ||
|
|
||
| if (this.username == null || this.username.isBlank()) { | ||
| throw new IllegalArgumentException("Username environment variable has not been set"); | ||
| } | ||
|
|
||
| if (this.password == null || this.password.isBlank()) { | ||
| throw new IllegalArgumentException("Password environment variable has not been set"); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Creates and returns a new MySQL database connection. | ||
| * | ||
| * @return a {@link Connection} to the database | ||
| * @throws SQLException if the connection cannot be established | ||
| */ | ||
| public Connection getMySqlConnection() throws SQLException { | ||
| return DriverManager.getConnection(databaseURL, username, password); | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| */ | ||
|
|
||
| } |