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/ .vscode/
### Mac OS ### ### Mac OS ###
.DS_Store .DS_Store
## Database related
connectionpetstore.properties

View File

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

View File

@@ -111,7 +111,8 @@ public class MainLayoutController {
spContentArea.getChildren().clear(); spContentArea.getChildren().clear();
spContentArea.getChildren().add(view); spContentArea.getChildren().add(view);
} catch (Exception e) { } 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.FileInputStream;
import java.io.IOException; 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.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
@@ -9,8 +12,7 @@ import java.util.Properties;
public class ConnectionDB { public class ConnectionDB {
/** /**
* Method to try and connect to the database sing cnnection.properties located in the * Method to try and connect to the database using connectionpetstore.properties.
* root of C drive
* @return Connection to the database * @return Connection to the database
*/ */
public static Connection getConnection(){ public static Connection getConnection(){
@@ -18,10 +20,23 @@ public class ConnectionDB {
String user = ""; String user = "";
String password = ""; String password = "";
try{ Properties prop = new Properties();
//Read connection.properties file Path propsPath;
FileInputStream fis = new FileInputStream("c:\\connectionpetstore.properties"); //location of connection can be changed here
Properties prop = new Properties(); 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); prop.load(fis);
url = prop.getProperty("url"); url = prop.getProperty("url");
user = prop.getProperty("user"); user = prop.getProperty("user");
@@ -31,12 +46,8 @@ public class ConnectionDB {
throw new RuntimeException("Problem with reading connection info: "+e.getMessage()); throw new RuntimeException("Problem with reading connection info: "+e.getMessage());
} }
Connection conn = null;
try{ try{
//try to get connection with the data taken from connection.properties return DriverManager.getConnection(url,user,password);
conn = DriverManager.getConnection(url,user,password);
return conn;
} }
catch (SQLException e) { catch (SQLException e) {
throw new RuntimeException("Problem with database connection: "+e.getMessage()); throw new RuntimeException("Problem with database connection: "+e.getMessage());