created tables

This commit is contained in:
Malte Schulze Hobeling 2022-12-15 09:26:08 +01:00
parent 83507bef98
commit 8478b80407
2 changed files with 113 additions and 7 deletions

View File

@ -26,12 +26,119 @@ public class Database {
} }
protected static void createDb(){ 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 ( CREATE TABLE IF NOT EXISTS user (
id integer PRIMARY KEY, 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()){ 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) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -56,8 +163,7 @@ public class Database {
pstmt.setInt(1,0); pstmt.setInt(1,0);
ResultSet rs = pstmt.executeQuery(); ResultSet rs = pstmt.executeQuery();
while (rs.next()){ while (rs.next()){
System.out.println(rs.getInt("id")); System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name"));
System.out.println(rs.getString("name"));
} }
}catch (SQLException e){ }catch (SQLException e){
e.printStackTrace(); e.printStackTrace();

View File

@ -20,9 +20,9 @@ public class HelloApplication extends Application {
public static void main(String[] args) { public static void main(String[] args) {
Database.init(); Database.init();
Database.createDb(); Database.createDb();
Database.fillSampleDb(); //Database.fillSampleDb();
Database.printSampleQuery(); Database.printSampleQuery();
Database.deleteSample(); //Database.deleteSample();
launch(); launch();
} }
} }