From 2a37af85ef0dd026e9455368ccb8b697ddcd33f3 Mon Sep 17 00:00:00 2001 From: AdrianBalunan Date: Wed, 11 Mar 2026 16:54:57 +0100 Subject: [PATCH] Feat: Added a connection check in DatabaseManager --- .../team6/database/DatabaseManager.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/database/DatabaseManager.java b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/database/DatabaseManager.java index 3e5357d..9e1fd3c 100644 --- a/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/database/DatabaseManager.java +++ b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/database/DatabaseManager.java @@ -24,6 +24,31 @@ 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(); + } + + return false; + } + /** * Creates the {@code charities} table if it does not already exist. *