From cd194527e812af88091071dafa2c9cd1a5b9a5e9 Mon Sep 17 00:00:00 2001 From: laurimaaninka Date: Sun, 12 Apr 2026 17:19:11 +0300 Subject: [PATCH] =?UTF-8?q?Lis=C3=A4tty=20laskutustieto=20tietokantaan?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: laurimaaninka --- .idea/sqldialects.xml | 1 + .../ruokamanageri/dataluokat/Tietokanta.java | 41 +++++++++++-------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml index c0e01ca..2d4858e 100644 --- a/.idea/sqldialects.xml +++ b/.idea/sqldialects.xml @@ -1,6 +1,7 @@ + \ No newline at end of file diff --git a/src/main/java/fi/lpam/ruokamanageri/dataluokat/Tietokanta.java b/src/main/java/fi/lpam/ruokamanageri/dataluokat/Tietokanta.java index b3f1cce..d4a690f 100644 --- a/src/main/java/fi/lpam/ruokamanageri/dataluokat/Tietokanta.java +++ b/src/main/java/fi/lpam/ruokamanageri/dataluokat/Tietokanta.java @@ -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(); + } } \ No newline at end of file