diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/Database.java b/src/main/java/com/bib/essensbestellungsverwaltung/Database.java index c3b8f39..35ef73a 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/Database.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/Database.java @@ -1,9 +1,15 @@ package com.bib.essensbestellungsverwaltung; +/** + * @author Malte Schulze Hobeling + */ import java.io.File; import java.io.IOException; import java.nio.file.Path; import java.sql.*; +import java.util.ArrayList; +import java.util.List; + public class Database { private static final String dbLocation = "jdbc:sqlite:"+Path.of("").toAbsolutePath()+"/database.db"; @@ -145,23 +151,119 @@ public class Database { } protected static void fillSampleDb(){ - String sql = """ - INSERT INTO user (id,name) - VALUES (1,'test1');"""; + List sqls = new ArrayList<>(); + sqls.add(""" + INSERT OR IGNORE INTO food_type (id,name) + VALUES ('1','Vegan');"""); + sqls.add(""" + INSERT OR IGNORE INTO food_type (id,name) + VALUES ('2','Vegetarisch');"""); + sqls.add(""" + INSERT OR IGNORE INTO food_type (id,name) + VALUES ('3','Fleischhaltig');"""); + sqls.add(""" + INSERT OR IGNORE INTO allergy (id,name) + VALUES('1','Eier');"""); + sqls.add(""" + INSERT OR IGNORE INTO allergy (id,name) + VALUES('2','Soja');"""); + sqls.add(""" + INSERT OR IGNORE INTO allergy (id,name) + VALUES('3','Milch');"""); + sqls.add(""" + INSERT OR IGNORE INTO allergy (id,name) + VALUES('4','Erdnüsse');"""); + sqls.add(""" + INSERT OR IGNORE INTO allergy (id,name) + VALUES('5','Weichtiere');"""); + sqls.add(""" + INSERT OR IGNORE INTO allergy (id,name) + VALUES('6','Krebstiere');"""); + sqls.add(""" + INSERT OR IGNORE INTO allergy (id,name) + VALUES('7','Getreide');"""); + sqls.add(""" + INSERT OR IGNORE INTO allergy (id,name) + VALUES('8','Senf');"""); + sqls.add(""" + INSERT OR IGNORE INTO allergy (id,name) + VALUES('9','Sesam');"""); + sqls.add(""" + INSERT OR IGNORE INTO allergy (id,name) + VALUES('10','Schwefeldioxid und Sulfit');"""); + sqls.add(""" + INSERT OR IGNORE INTO allergy (id,name) + VALUES('11','Nüsse');"""); + sqls.add(""" + INSERT OR IGNORE INTO allergy (id,name) + VALUES('12','Sellerie');"""); + sqls.add(""" + INSERT OR IGNORE INTO allergy (id,name) + VALUES('13','Fische');"""); + sqls.add(""" + INSERT OR IGNORE INTO allergy (id,name) + VALUES('14','Lupinen');"""); + sqls.add(""" + INSERT OR IGNORE INTO allergy (id,name) + VALUES('15','Nitrit-Pökelsalz');"""); + sqls.add(""" + INSERT OR IGNORE INTO allergy (id,name) + VALUES('16','Phosphat');"""); + sqls.add(""" + INSERT OR IGNORE INTO allergy (id,name) + VALUES('17','Nitrat');"""); + sqls.add(""" + INSERT OR IGNORE INTO allergy (id,name) + VALUES('18','Antioxidationsmittel');"""); + sqls.add(""" + INSERT OR IGNORE INTO allergy (id,name) + VALUES('19','Farbstoff');"""); + sqls.add(""" + INSERT OR IGNORE INTO allergy (id,name) + VALUES('20','Geschmacksverstärker');"""); + sqls.add(""" + INSERT OR IGNORE INTO allergy (id,name) + VALUES('21','Süßungsmittel');"""); + sqls.add(""" + INSERT OR IGNORE INTO allergy (id,name) + VALUES('22','Konservierungsstoff');"""); try(Connection conn = connect(); Statement stmt = conn.createStatement()){ - stmt.execute(sql); + for (String sql : sqls) { + stmt.execute(sql); + } }catch (SQLException e){ e.printStackTrace(); } } + protected static boolean insert(String table, String header, String values){ + try (Connection conn = connect(); Statement stmt = conn.createStatement()){ + String sql = "INSERT OR IGNORE INTO " + table + " (" + header + ") VALUES(" + values + ");"; + stmt.execute(sql); + } catch (SQLException e) { + return false; + } + return true; + } + + protected static void printSampleQuery(){ String sql = """ - SELECT * FROM user WHERE id > ?;"""; + SELECT * FROM food_type WHERE id > ?;"""; + String sql1 = """ + SELECT * FROM allergy WHERE id > ?;"""; try(Connection conn = connect()){ PreparedStatement pstmt = conn.prepareStatement(sql); + PreparedStatement pstmt1 = conn.prepareStatement(sql1); pstmt.setInt(1,0); ResultSet rs = pstmt.executeQuery(); + System.out.println("food_type"); + while (rs.next()){ + System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name")); + } + System.out.println("allergy"); + pstmt1.setInt(1,0); + rs = pstmt1.executeQuery(); while (rs.next()){ System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name")); } diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/HelloApplication.java b/src/main/java/com/bib/essensbestellungsverwaltung/HelloApplication.java index 19a1574..cb64c3a 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/HelloApplication.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/HelloApplication.java @@ -20,7 +20,7 @@ public class HelloApplication extends Application { public static void main(String[] args) { Database.init(); Database.createDb(); - //Database.fillSampleDb(); + Database.fillSampleDb(); Database.printSampleQuery(); //Database.deleteSample(); launch();