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

5
.gitignore vendored
View File

@@ -36,4 +36,7 @@ build/
.vscode/
### Mac OS ###
.DS_Store
.DS_Store
## Database related
connectionpetstore.properties

View File

@@ -8,9 +8,9 @@
<artifactId>PetShopDesktop</artifactId>
<version>1.0-SNAPSHOT</version>
<name>PetShopDesktop</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<javafx.version>25.0.2</javafx.version>
<junit.version>5.12.1</junit.version>
</properties>
@@ -18,12 +18,12 @@
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>21.0.6</version>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>21.0.6</version>
<version>${javafx.version}</version>
</dependency>
<dependency>
@@ -66,8 +66,7 @@
<!-- Default configuration for running with: mvn clean javafx:run -->
<id>default-cli</id>
<configuration>
<mainClass>org.example.petshopdesktop/org.example.petshopdesktop.PetShopApplication
</mainClass>
<mainClass>org.example.petshopdesktop/org.example.petshopdesktop.PetShopApplication</mainClass>
<launcher>app</launcher>
<jlinkZipName>app</jlinkZipName>
<jlinkImageName>app</jlinkImageName>

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