diff --git a/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/database/DatabaseConnection.java b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/database/DatabaseConnection.java new file mode 100644 index 0000000..437bbf9 --- /dev/null +++ b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/database/DatabaseConnection.java @@ -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. + *

+ * Required environment variables: + *

+ *

+ * + * @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 + *

+ * Used primarily for JUnit tests. Production should use the constructor + * using environment variables. + *

+ * + * @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); + } + + /** + * + */ + +}