package fi.lpam.dataluokat; import fi.lpam.gui.virheDialogit.SiirtoVirhe; import fi.lpam.gui.virheDialogit.YhteysVirhe; import fi.lpam.Main; import javafx.scene.control.Alert; import org.apache.commons.lang3.SystemUtils; import java.io.*; import java.sql.*; import java.util.Objects; public class Tietokanta { private static String osoite; private static String dbPath = SystemUtils.getUserHome().getAbsolutePath() + "\\OneDrive\\Kuljetusruokasovellus\\tietokanta.db"; public Tietokanta(boolean dev) throws IOException { if (dev) dbPath = "tietokanta.db"; File file = new File(dbPath); try { //noinspection ResultOfMethodCallIgnored file.getParentFile().mkdirs(); } catch (Exception _) {} //noinspection ResultOfMethodCallIgnored file.createNewFile(); osoite = "jdbc:sqlite:" + file.getAbsolutePath(); try (Connection conn = haeYhteys()) { InputStream is = Objects.requireNonNull(Main.class.getResourceAsStream("tietokanta.sql")); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String sql = br.readAllAsString(); for (String query : sql.split(";")) { conn.createStatement().execute(query); } } catch (Exception e) { SiirtoVirhe virhe = new SiirtoVirhe(e); virhe.showAndWait(); } } public static Connection haeYhteys() { Connection connection = null; try { connection = DriverManager.getConnection(osoite); } catch (Exception e) { Alert virhe = new YhteysVirhe(e); virhe.showAndWait(); } return connection; } }