Lisätty laskutustieto tietokantaan

Signed-off-by: laurimaaninka <lauri.maaninka@gmail.com>
This commit is contained in:
2026-04-12 17:19:11 +03:00
parent 0732db2365
commit cd194527e8
2 changed files with 25 additions and 17 deletions
@@ -1,10 +1,8 @@
package fi.lpam.ruokamanageri.dataluokat;
import fi.lpam.ruokamanageri.gui.virheDialogit.SiirtoVirhe;
import fi.lpam.ruokamanageri.gui.virheDialogit.YhteysVirhe;
import fi.lpam.gui.virheDialogit.SiirtoVirhe;
import fi.lpam.ruokamanageri.Main;
import javafx.scene.control.Alert;
import fi.lpam.Main;
import org.apache.commons.lang3.SystemUtils;
import java.io.*;
@@ -15,16 +13,14 @@ public class Tietokanta {
private static String osoite;
private static String dbPath = SystemUtils.getUserHome().getAbsolutePath() + "\\OneDrive\\Kuljetusruokasovellus\\tietokanta.db";
public Tietokanta(boolean dev) throws IOException {
public Tietokanta(boolean dev) {
if (dev) dbPath = "testikanta.db";
File file = new File(dbPath);
try {
//noinspection ResultOfMethodCallIgnored
file.getParentFile().mkdirs();
boolean _ = file.getParentFile().mkdirs();
boolean _ = file.createNewFile();
}
catch (Exception _) {}
//noinspection ResultOfMethodCallIgnored
file.createNewFile();
osoite = "jdbc:sqlite:" + file.getAbsolutePath();
@@ -37,24 +33,35 @@ public class Tietokanta {
for (String query : sql.split(";")) {
conn.createStatement().execute(query);
}
}
catch (Exception e) {
SiirtoVirhe virhe = new SiirtoVirhe(e);
virhe.showAndWait();
catch (SQLException | IOException e) {
handleException(e);
}
//Lisää laskutustietokenttä Asiakkaat tauluun
try (Connection conn = haeYhteys()) {
Statement stmt = conn.createStatement();
stmt.execute("alter table asiakkaat add column laskutustieto TEXT");
}
catch (SQLException e) {
if (!e.getMessage().contains("duplicate column name: laskutustieto")) {
handleException(e);
}
}
}
public static Connection haeYhteys() {
Connection connection = null;
try {
connection = DriverManager.getConnection(osoite);
} catch (Exception e) {
Alert virhe = new YhteysVirhe(e);
virhe.showAndWait();
} catch (SQLException e) {
handleException(e);
}
return connection;
}
private static void handleException(Exception e) {
SiirtoVirhe virhe = new SiirtoVirhe(e);
virhe.showAndWait();
}
}