db #1

Merged
PBS2H21AKA merged 4 commits from db into main 2022-12-20 15:24:59 +01:00
3 changed files with 311 additions and 32 deletions

View File

@ -0,0 +1,306 @@
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";
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{
conn = DriverManager.getConnection(dbLocation);
}catch (SQLException e){
e.printStackTrace();
}
return conn;
}
protected static void createDb(){
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,
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()){
for(int i = 0; i < sql.length; i++){
stmt.execute(sql[i]);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
protected static void fillSampleDb(){
List<String> 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()){
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 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"));
}
}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;
} */
}

View File

@ -6,10 +6,8 @@ import javafx.scene.Scene;
import javafx.stage.Stage; import javafx.stage.Stage;
import java.io.IOException; import java.io.IOException;
import java.sql.*;
public class HelloApplication extends Application { public class HelloApplication extends Application {
private static final String dbLocation = "jdbc:sqlite:"+HelloApplication.class.getResource("database/database.db");
@Override @Override
public void start(Stage stage) throws IOException { public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml")); FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
@ -20,36 +18,11 @@ public class HelloApplication extends Application {
} }
public static void main(String[] args) { public static void main(String[] args) {
/* String sql = """ Database.init();
CREATE TABLE IF NOT EXISTS user ( Database.createDb();
id integer PRIMARY KEY, Database.fillSampleDb();
name text);"""; Database.printSampleQuery();
String sql2 = "SELECT * FROM user WHERE id > ?"; //Database.deleteSample();
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;
} */
launch(); launch();
} }
private static Connection connect(){
Connection conn = null;
try{
conn = DriverManager.getConnection(dbLocation);
}catch (SQLException e){
e.printStackTrace();
}
return conn;
}
} }