Skip to content

Commit

Permalink
Feat: Seperate File for Database Connection
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianBalunan committed Mar 11, 2026
1 parent 6c509f0 commit 8bce5de
Showing 1 changed file with 96 additions and 0 deletions.
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);
}

/**
*
*/

}

0 comments on commit 8bce5de

Please sign in to comment.