Koodin siivousta

Signed-off-by: laurimaaninka <lauri.maaninka@gmail.com>
This commit is contained in:
2026-05-11 00:00:27 +03:00
parent 69243d9cb2
commit c2942a7455
30 changed files with 75 additions and 79 deletions
@@ -0,0 +1,69 @@
package fi.lpam.dataluokat;
import fi.lpam.gui.virheDialogit.SiirtoVirhe;
import fi.lpam.Main;
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) {
if (dev) dbPath = "testikanta.db";
File file = new File(dbPath);
try {
boolean _ = file.getParentFile().mkdirs();
boolean _ = file.createNewFile();
}
catch (Exception _) {}
osoite = "jdbc:sqlite:" + file.getAbsolutePath();
try (Connection conn = haeYhteys()) {
InputStream is = Objects.requireNonNull(Main.class.getClassLoader().getResourceAsStream("tietokanta.sql"));
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String sql = br.readAllAsString();
for (String query : sql.split(";")) {
conn.createStatement().execute(query);
}
}
catch (SQLException | IOException e) {
handleException(e);
}
//Lisää kuljetuspäivät asiakassarakkeeseen (v2.0.2->3.x.x)
String[] sarakkeet = {"lauantaiKpl", "sunnuntaiKpl"};
for (String sarake : sarakkeet) {
try (Connection conn = haeYhteys()) {
PreparedStatement stmt = conn.prepareStatement("alter table asiakkaat add column " + sarake + " int not null default 0");
stmt.execute();
} catch (SQLException e) {
if (!e.getMessage().contains("duplicate column name")) {
handleException(e);
}
}
}
}
public static Connection haeYhteys() {
Connection connection = null;
try {
connection = DriverManager.getConnection(osoite);
} catch (SQLException e) {
handleException(e);
}
return connection;
}
private static void handleException(Exception e) {
SiirtoVirhe virhe = new SiirtoVirhe(e);
virhe.showAndWait();
}
}