diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DatabaseManager.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DatabaseManager.java index 5d41544..5bd06dc 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DatabaseManager.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DatabaseManager.java @@ -1,67 +1,64 @@ package ntnu.systemutvikling.team6.database; -import ntnu.systemutvikling.team6.models.Charity; -import ntnu.systemutvikling.team6.models.CharityRegistry; -import ntnu.systemutvikling.team6.scraper.APICharityData; - import java.sql.*; import java.util.List; +import java.util.UUID; + +import ntnu.systemutvikling.team6.models.Charity; +import ntnu.systemutvikling.team6.scraper.APICharityData; /** * Manages creation of MySQL tables (for now) - *
- * This class is responsible for creating the tables needed for the application, if not done already - * and maintaining the {@code charities} table based on data retrieved from the IK API. - *
+ * + *This class is responsible for creating the tables needed for the application, if not done + * already and maintaining the {@code charities} table based on data retrieved from the IK API. */ - public class DatabaseManager { - private final DatabaseConnection connection; - - /** - * Contractor for DatabaseManager. It uses a DatabaseConnection object that contains a connection credentials - * using environment variables in the DatabaseConnection. - */ - public DatabaseManager(){ - this.connection = new DatabaseConnection(); + private final DatabaseConnection connection; + + /** + * Contractor for DatabaseManager. It uses a DatabaseConnection object that contains a connection + * credentials using environment variables in the DatabaseConnection. + */ + public DatabaseManager() { + this.connection = new DatabaseConnection(); + } + + /** + * Connection test for the Database. Does a simple SELECT SQL query and returns either true og and + * Exception if failed + * + * @return true if Sucsedd or SQLExepction if failed + */ + public boolean testConnection() { + try (Connection conn = connection.getMySqlConnection(); + Statement stmt = conn.createStatement()) { + + ResultSet rs = stmt.executeQuery("SELECT 1"); + + if (rs.next()) { + System.out.println("Database connection verified."); + return true; + } + + } catch (SQLException e) { + System.out.println("Database connection failed."); + e.printStackTrace(); } - /** - * Connection test for the Database. - * Does a simple SELECT SQL query and returns either true og and Exception if failed - * - * @return true if Sucsedd or SQLExepction if failed - */ - public boolean testConnection() { - try (Connection conn = connection.getMySqlConnection(); - Statement stmt = conn.createStatement()) { - - ResultSet rs = stmt.executeQuery("SELECT 1"); - - if (rs.next()) { - System.out.println("Database connection verified."); - return true; - } - - } catch (SQLException e) { - System.out.println("Database connection failed."); - e.printStackTrace(); - } - - return false; - } - - /** - * Creates the {@code charities} table if it does not already exist. - *
- * The table structure is based on fields from {@link APICharityData}. - *
- * @throws RuntimeException if a {@link SQLException} occurs while creating the table - */ - - public void createTables() { - String sql_query1 = - """ + return false; + } + + /** + * Creates the {@code charities} table if it does not already exist. + * + *The table structure is based on fields from {@link APICharityData}.
+ *
+ * @throws RuntimeException if a {@link SQLException} occurs while creating the table
+ */
+ public void createTables() {
+ String sql_query1 =
+ """
-- -----------------------------------------------------
-- Table `HelpMeHelp`.`Charities`
-- -----------------------------------------------------
@@ -75,10 +72,10 @@ status VARCHAR(255) NOT NULL,
UNIQUE KEY unique_org_number (org_number)
) ENGINE=InnoDB;
-
+
""";
- String sql_query2 =
- """
+ String sql_query2 =
+ """
-- -----------------------------------------------------
-- Table `HelpMeHelp`.`Donations`
-- -----------------------------------------------------
@@ -91,74 +88,117 @@ PRIMARY KEY (`UUID_Donations`),
INDEX `fk_Donations_Charities_idx` (`Charities_UUID_charities` ASC) VISIBLE,
CONSTRAINT `fk_Donations_Charities`
FOREIGN KEY (`Charities_UUID_charities`)
- REFERENCES `apbaluna`.`Charities` (`UUID_charities`)
+ REFERENCES Charities (`UUID_charities`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
""";
- try (Connection conn = connection.getMySqlConnection();
- Statement s = conn.createStatement()) {
+ try (Connection conn = connection.getMySqlConnection();
+ Statement s = conn.createStatement()) {
- s.execute(sql_query1);
- s.execute(sql_query2);
- } catch (SQLException e) {
- e.printStackTrace();
- throw new RuntimeException("Error creating table.");
- }
+ s.execute(sql_query1);
+ s.execute(sql_query2);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ throw new RuntimeException("Error creating table.");
}
-
- public void addAPIDataToTable(List