Fixed DB connection and java version issue

This commit is contained in:
2026-02-10 16:35:49 -07:00
parent a2dd913abd
commit 12020a2feb
4 changed files with 32 additions and 18 deletions

View File

@@ -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();
}
}

View File

@@ -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());