From 18d812e1a2bfcd80c3009eda82a789589ae873f3 Mon Sep 17 00:00:00 2001 From: pbs2h21ash Date: Mon, 12 Dec 2022 14:03:49 +0100 Subject: [PATCH 1/4] moved dbconnection to own class --- .../essensbestellungsverwaltung/Database.java | 87 +++++++++++++++++++ .../HelloApplication.java | 36 +------- 2 files changed, 91 insertions(+), 32 deletions(-) create mode 100644 src/main/java/com/bib/essensbestellungsverwaltung/Database.java diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/Database.java b/src/main/java/com/bib/essensbestellungsverwaltung/Database.java new file mode 100644 index 0000000..19e3951 --- /dev/null +++ b/src/main/java/com/bib/essensbestellungsverwaltung/Database.java @@ -0,0 +1,87 @@ +package com.bib.essensbestellungsverwaltung; + +import java.sql.*; + +public class Database { + private static final String dbLocation = "jdbc:sqlite:"+Database.class.getResource("database/database.db"); + protected static Connection connect(){ + Connection conn = null; + try{ + conn = DriverManager.getConnection(dbLocation); + }catch (SQLException e){ + e.printStackTrace(); + } + return conn; + } + + protected static void createDb(){ + String sql = """ + CREATE TABLE IF NOT EXISTS user ( + id integer PRIMARY KEY, + name text);"""; + try(Connection conn = connect(); Statement stmt = conn.createStatement()){ + stmt.execute(sql); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + protected static void fillSampleDb(){ + String sql = """ + INSERT INTO user (id,name) + VALUES (1,'test1');"""; + try(Connection conn = connect(); Statement stmt = conn.createStatement()){ + stmt.execute(sql); + }catch (SQLException e){ + e.printStackTrace(); + } + } + + protected static void printSampleQuery(){ + String sql = """ + SELECT * FROM user WHERE id > ?;"""; + try(Connection conn = connect()){ + PreparedStatement pstmt = conn.prepareStatement(sql); + pstmt.setInt(1,0); + ResultSet rs = pstmt.executeQuery(); + while (rs.next()){ + System.out.println(rs.getInt("id")); + System.out.println(rs.getString("name")); + } + }catch (SQLException e){ + e.printStackTrace(); + } + } + + protected static void deleteSample(){ + String sql = """ + DELETE FROM user WHERE id = ?;"""; + try(Connection conn = connect();PreparedStatement pstmt = conn.prepareStatement(sql)){ + pstmt.setInt(1,1); + pstmt.executeUpdate(); + }catch (SQLException e){ + e.printStackTrace(); + } + } + + /* String sql = """ + CREATE TABLE IF NOT EXISTS user ( + id integer PRIMARY KEY, + name text);"""; + String sql2 = "SELECT * FROM user WHERE id > ?"; + String sql3 = "INSERT INTO user (id,name) VALUES (1,'test1')"; + try(Connection conn = connect(); + Statement stmt = conn.createStatement()){ + stmt.execute(sql); + stmt.execute(sql3); + PreparedStatement pstmt = conn.prepareStatement(sql2); + pstmt.setInt(1,0); + ResultSet rs = pstmt.executeQuery(); + while (rs.next()){ + System.out.println(rs.getInt("id")); + } + }catch (SQLException e){ + e.printStackTrace(); + return; + } */ +} diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/HelloApplication.java b/src/main/java/com/bib/essensbestellungsverwaltung/HelloApplication.java index 163a374..dcab265 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/HelloApplication.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/HelloApplication.java @@ -6,10 +6,8 @@ import javafx.scene.Scene; import javafx.stage.Stage; import java.io.IOException; -import java.sql.*; public class HelloApplication extends Application { - private static final String dbLocation = "jdbc:sqlite:"+HelloApplication.class.getResource("database/database.db"); @Override public void start(Stage stage) throws IOException { FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml")); @@ -20,36 +18,10 @@ public class HelloApplication extends Application { } public static void main(String[] args) { -/* String sql = """ - CREATE TABLE IF NOT EXISTS user ( - id integer PRIMARY KEY, - name text);"""; - String sql2 = "SELECT * FROM user WHERE id > ?"; - String sql3 = "INSERT INTO user (id,name) VALUES (1,'test1')"; - try(Connection conn = connect(); - Statement stmt = conn.createStatement()){ - stmt.execute(sql); - stmt.execute(sql3); - PreparedStatement pstmt = conn.prepareStatement(sql2); - pstmt.setInt(1,0); - ResultSet rs = pstmt.executeQuery(); - while (rs.next()){ - System.out.println(rs.getInt("id")); - } - }catch (SQLException e){ - e.printStackTrace(); - return; - } */ + Database.createDb(); + Database.fillSampleDb(); + Database.printSampleQuery(); + Database.deleteSample(); launch(); } - - private static Connection connect(){ - Connection conn = null; - try{ - conn = DriverManager.getConnection(dbLocation); - }catch (SQLException e){ - e.printStackTrace(); - } - return conn; - } } \ No newline at end of file From 83507bef98dd9a7f048f08291c879aefc3ac555b Mon Sep 17 00:00:00 2001 From: Malte Schulze Hobeling Date: Tue, 13 Dec 2022 03:35:52 +0100 Subject: [PATCH 2/4] made program executable --- .../bib/essensbestellungsverwaltung/Database.java | 13 ++++++++++++- .../HelloApplication.java | 1 + .../database/database.db | 0 3 files changed, 13 insertions(+), 1 deletion(-) delete mode 100644 src/main/resources/com/bib/essensbestellungsverwaltung/database/database.db diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/Database.java b/src/main/java/com/bib/essensbestellungsverwaltung/Database.java index 19e3951..452b21f 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/Database.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/Database.java @@ -1,9 +1,20 @@ package com.bib.essensbestellungsverwaltung; +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; import java.sql.*; public class Database { - private static final String dbLocation = "jdbc:sqlite:"+Database.class.getResource("database/database.db"); + private static final String dbLocation = "jdbc:sqlite:"+Path.of("").toAbsolutePath()+"/database.db"; + protected static void init(){ + File db = new File(Path.of("").toAbsolutePath()+"/database.db"); + try { + db.createNewFile(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } protected static Connection connect(){ Connection conn = null; try{ diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/HelloApplication.java b/src/main/java/com/bib/essensbestellungsverwaltung/HelloApplication.java index dcab265..60c6057 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/HelloApplication.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/HelloApplication.java @@ -18,6 +18,7 @@ public class HelloApplication extends Application { } public static void main(String[] args) { + Database.init(); Database.createDb(); Database.fillSampleDb(); Database.printSampleQuery(); diff --git a/src/main/resources/com/bib/essensbestellungsverwaltung/database/database.db b/src/main/resources/com/bib/essensbestellungsverwaltung/database/database.db deleted file mode 100644 index e69de29..0000000 From 8478b80407be7f91efceef60edc840be548ba21c Mon Sep 17 00:00:00 2001 From: Malte Schulze Hobeling Date: Thu, 15 Dec 2022 09:26:08 +0100 Subject: [PATCH 3/4] created tables --- .../essensbestellungsverwaltung/Database.java | 116 +++++++++++++++++- .../HelloApplication.java | 4 +- 2 files changed, 113 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/Database.java b/src/main/java/com/bib/essensbestellungsverwaltung/Database.java index 452b21f..c3b8f39 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/Database.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/Database.java @@ -26,12 +26,119 @@ public class Database { } protected static void createDb(){ - String sql = """ + String[] sql = new String[14]; + sql[0] = """ + CREATE TABLE IF NOT EXISTS address ( + id integer PRIMARY KEY, + street text, + number text, + plz text, + city text + );"""; + sql[1] = """ + CREATE TABLE IF NOT EXISTS food_type ( + id integer PRIMARY KEY, + name text + );"""; + sql[2] = """ + CREATE TABLE IF NOT EXISTS allergy ( + id integer PRIMARY KEY, + name text + );"""; + sql[3] = """ + CREATE TABLE IF NOT EXISTS severity ( + id integer PRIMARY KEY, + severity integer + );"""; + sql[4] = """ CREATE TABLE IF NOT EXISTS user ( id integer PRIMARY KEY, - name text);"""; + name text, + firstname text, + addressid integer, + password text, + email text UNIQUE, + FOREIGN KEY(addressid) REFERENCES address(id) + );"""; + sql[5] = """ + CREATE TABLE IF NOT EXISTS child ( + id integer PRIMARY KEY, + name text, + firstname text, + addressid integer, + FOREIGN KEY(addressid) REFERENCES address(id) + );"""; + sql[6] = """ + CREATE TABLE IF NOT EXISTS worker ( + userid integer PRIMARY KEY, + FOREIGN KEY(userid) REFERENCES user(id) + );"""; + sql[7] = """ + CREATE TABLE IF NOT EXISTS parent ( + userid integer PRIMARY KEY, + FOREIGN KEY(userid) REFERENCES user(id) + );"""; + sql[8] = """ + CREATE TABLE IF NOT EXISTS parent_child ( + id integer PRIMARY KEY, + parentuserid integer, + childid integer, + FOREIGN KEY(parentuserid) REFERENCES parent(userid), + FOREIGN KEY(childid) REFERENCES child(id) + );"""; + sql[9] = """ + CREATE TABLE IF NOT EXISTS child_allergy ( + id integer PRIMARY KEY, + childid integer, + allergyid integer, + severityid integer, + FOREIGN KEY(childid) REFERENCES child(id), + FOREIGN KEY(allergyid) REFERENCES allergy(id), + FOREIGN KEY(severityid) REFERENCES severity(id) + );"""; + sql[10] = """ + CREATE TABLE IF NOT EXISTS food ( + id integer PRIMARY KEY, + name text, + description text, + isdessert integer, + food_typeid integer, + FOREIGN KEY(food_typeid) REFERENCES food_type(id) + );"""; + sql[11] = """ + CREATE TABLE IF NOT EXISTS food_plan ( + id integer PRIMARY KEY, + date text, + food1 integer, + food2 integer, + dessert1 integer, + dessert2 integer, + FOREIGN KEY(food1) REFERENCES food(id), + FOREIGN KEY(food2) REFERENCES food(id), + FOREIGN KEY(dessert1) REFERENCES food(id), + FOREIGN KEY(dessert2) REFERENCES food(id) + );"""; + sql[12] = """ + CREATE TABLE IF NOT EXISTS food_restrictions ( + id integer PRIMARY KEY, + foodid integer, + allergyid integer, + FOREIGN KEY(foodid) REFERENCES food(id), + FOREIGN KEY(allergyid) REFERENCES allergy(id) + );"""; + sql[13] = """ + CREATE TABLE IF NOT EXISTS food_selection ( + id integer PRIMARY KEY, + childid integer, + food_planid integer, + selection integer, + FOREIGN KEY(childid) REFERENCES child(id), + FOREIGN KEY(food_planid) REFERENCES food_plan(id) + );"""; try(Connection conn = connect(); Statement stmt = conn.createStatement()){ - stmt.execute(sql); + for(int i = 0; i < sql.length; i++){ + stmt.execute(sql[i]); + } } catch (SQLException e) { e.printStackTrace(); } @@ -56,8 +163,7 @@ public class Database { pstmt.setInt(1,0); ResultSet rs = pstmt.executeQuery(); while (rs.next()){ - System.out.println(rs.getInt("id")); - System.out.println(rs.getString("name")); + System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name")); } }catch (SQLException e){ e.printStackTrace(); diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/HelloApplication.java b/src/main/java/com/bib/essensbestellungsverwaltung/HelloApplication.java index 60c6057..19a1574 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/HelloApplication.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/HelloApplication.java @@ -20,9 +20,9 @@ public class HelloApplication extends Application { public static void main(String[] args) { Database.init(); Database.createDb(); - Database.fillSampleDb(); + //Database.fillSampleDb(); Database.printSampleQuery(); - Database.deleteSample(); + //Database.deleteSample(); launch(); } } \ No newline at end of file From 0a8e052582941b40f5a610f1e471eeddb7dacd0e Mon Sep 17 00:00:00 2001 From: Malte Schulze Hobeling Date: Mon, 19 Dec 2022 17:31:06 +0100 Subject: [PATCH 4/4] filled food_type and allergy created generic insert --- .../essensbestellungsverwaltung/Database.java | 112 +++++++++++++++++- .../HelloApplication.java | 2 +- 2 files changed, 108 insertions(+), 6 deletions(-) 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();