2022-12-12 14:03:49 +01:00
|
|
|
package com.bib.essensbestellungsverwaltung;
|
|
|
|
|
2022-12-13 03:35:52 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Path;
|
2022-12-12 14:03:49 +01:00
|
|
|
import java.sql.*;
|
|
|
|
|
|
|
|
public class Database {
|
2022-12-13 03:35:52 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2022-12-12 14:03:49 +01:00
|
|
|
protected static Connection connect(){
|
|
|
|
Connection conn = null;
|
|
|
|
try{
|
|
|
|
conn = DriverManager.getConnection(dbLocation);
|
|
|
|
}catch (SQLException e){
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static void createDb(){
|
2022-12-15 09:26:08 +01:00
|
|
|
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] = """
|
2022-12-12 14:03:49 +01:00
|
|
|
CREATE TABLE IF NOT EXISTS user (
|
|
|
|
id integer PRIMARY KEY,
|
2022-12-15 09:26:08 +01:00
|
|
|
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)
|
|
|
|
);""";
|
2022-12-12 14:03:49 +01:00
|
|
|
try(Connection conn = connect(); Statement stmt = conn.createStatement()){
|
2022-12-15 09:26:08 +01:00
|
|
|
for(int i = 0; i < sql.length; i++){
|
|
|
|
stmt.execute(sql[i]);
|
|
|
|
}
|
2022-12-12 14:03:49 +01:00
|
|
|
} 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()){
|
2022-12-15 09:26:08 +01:00
|
|
|
System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name"));
|
2022-12-12 14:03:49 +01:00
|
|
|
}
|
|
|
|
}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;
|
|
|
|
} */
|
|
|
|
}
|