diff --git a/.gitignore b/.gitignore index 480bdf52..4a8c77b5 100644 --- a/.gitignore +++ b/.gitignore @@ -36,4 +36,7 @@ build/ .vscode/ ### Mac OS ### -.DS_Store \ No newline at end of file +.DS_Store + +## Database related +connectionpetstore.properties diff --git a/pom.xml b/pom.xml index 8cb9a1ba..6da4edce 100644 --- a/pom.xml +++ b/pom.xml @@ -8,9 +8,9 @@ PetShopDesktop 1.0-SNAPSHOT PetShopDesktop - UTF-8 + 25.0.2 5.12.1 @@ -18,12 +18,12 @@ org.openjfx javafx-controls - 21.0.6 + ${javafx.version} org.openjfx javafx-fxml - 21.0.6 + ${javafx.version} @@ -66,8 +66,7 @@ default-cli - org.example.petshopdesktop/org.example.petshopdesktop.PetShopApplication - + org.example.petshopdesktop/org.example.petshopdesktop.PetShopApplication app app app diff --git a/src/main/java/org/example/petshopdesktop/controllers/MainLayoutController.java b/src/main/java/org/example/petshopdesktop/controllers/MainLayoutController.java index a5a3060d..431d38d4 100644 --- a/src/main/java/org/example/petshopdesktop/controllers/MainLayoutController.java +++ b/src/main/java/org/example/petshopdesktop/controllers/MainLayoutController.java @@ -111,7 +111,8 @@ public class MainLayoutController { spContentArea.getChildren().clear(); spContentArea.getChildren().add(view); } catch (Exception e) { - System.out.println("Error loading view: " + fxmlFile); + System.err.println("Error loading view: " + fxmlFile); + e.printStackTrace(); } } diff --git a/src/main/java/org/example/petshopdesktop/database/ConnectionDB.java b/src/main/java/org/example/petshopdesktop/database/ConnectionDB.java index 2b0bad1d..3435c7ac 100644 --- a/src/main/java/org/example/petshopdesktop/database/ConnectionDB.java +++ b/src/main/java/org/example/petshopdesktop/database/ConnectionDB.java @@ -2,6 +2,9 @@ package org.example.petshopdesktop.database; import java.io.FileInputStream; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; @@ -9,8 +12,7 @@ import java.util.Properties; public class ConnectionDB { /** - * Method to try and connect to the database sing cnnection.properties located in the - * root of C drive + * Method to try and connect to the database using connectionpetstore.properties. * @return Connection to the database */ public static Connection getConnection(){ @@ -18,10 +20,23 @@ public class ConnectionDB { String user = ""; String password = ""; - try{ - //Read connection.properties file - FileInputStream fis = new FileInputStream("c:\\connectionpetstore.properties"); //location of connection can be changed here - Properties prop = new Properties(); + Properties prop = new Properties(); + Path propsPath; + + String explicitPath = System.getenv("PETSTORE_DB_PROPS"); + if (explicitPath != null && !explicitPath.isBlank()) { + propsPath = Paths.get(explicitPath); + } else { + Path cwd = Paths.get(System.getProperty("user.dir"), "connectionpetstore.properties"); + Path xdg = Paths.get(System.getProperty("user.home"), ".config", "petstore", "connectionpetstore.properties"); + Path legacyWindows = Paths.get("./connectionpetstore.properties"); + + if (Files.exists(cwd)) propsPath = cwd; + else if (Files.exists(xdg)) propsPath = xdg; + else propsPath = legacyWindows; + } + + try (FileInputStream fis = new FileInputStream(propsPath.toString())) { prop.load(fis); url = prop.getProperty("url"); user = prop.getProperty("user"); @@ -31,12 +46,8 @@ public class ConnectionDB { throw new RuntimeException("Problem with reading connection info: "+e.getMessage()); } - Connection conn = null; - try{ - //try to get connection with the data taken from connection.properties - conn = DriverManager.getConnection(url,user,password); - return conn; + return DriverManager.getConnection(url,user,password); } catch (SQLException e) { throw new RuntimeException("Problem with database connection: "+e.getMessage());