diff --git a/pom.xml b/pom.xml
index cdb2276..78d3e5c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -20,6 +20,11 @@
javafx-controls
17.0.6
+
+ org.apache.derby
+ derby
+ 10.17.1.0
+
org.openjfx
javafx-fxml
diff --git a/src/main/java/at/ionas999/health/services/DatabaseUtilsBase.java b/src/main/java/at/ionas999/health/services/DatabaseUtilsBase.java
new file mode 100644
index 0000000..3336356
--- /dev/null
+++ b/src/main/java/at/ionas999/health/services/DatabaseUtilsBase.java
@@ -0,0 +1,39 @@
+package at.ionas999.health.services;
+
+import java.sql.*;
+import java.util.ArrayList;
+
+public abstract class DatabaseUtilsBase {
+
+ private static final String connectionString = "jdbc:derby:DerbyDb;create=true";
+ protected ArrayList tableCreationArrayList = new ArrayList<>();
+
+ protected Connection GetDbConnection() throws SQLException {
+ Connection connection = DriverManager.getConnection(DatabaseUtilsBase.connectionString);
+
+ if(!ExistAnyTables(connection))
+ {
+ CreateTables(connection);
+ }
+
+ return connection;
+ }
+
+ private void CreateTables(Connection connection) throws SQLException {
+ for (TableCreation tc : tableCreationArrayList) {
+ tc.CreateTable(connection);
+ }
+ }
+
+ private boolean ExistAnyTables(Connection connection) throws SQLException {
+ try (Statement stmt = connection.createStatement()) {
+ ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM SYS.SYSTABLES WHERE TABLETYPE='T'");
+ if (rs.next()) {
+ return rs.getInt(1) > 0;
+ }
+ }
+ return false;
+ }
+
+
+}
diff --git a/src/main/java/at/ionas999/health/services/PatientService.java b/src/main/java/at/ionas999/health/services/PatientService.java
new file mode 100644
index 0000000..a6327f1
--- /dev/null
+++ b/src/main/java/at/ionas999/health/services/PatientService.java
@@ -0,0 +1,77 @@
+package at.ionas999.health.services;
+
+import at.ionas999.health.model.Patient;
+
+import java.sql.*;
+import java.util.ArrayList;
+
+public class PatientService extends DatabaseUtilsBase implements TableCreation {
+ public PatientService() {
+ this.tableCreationArrayList.add(this);
+ }
+
+
+ @Override
+ public void CreateTable(Connection connection) throws SQLException {
+ String createTableSmt = "CREATE TABLE Patient (\n" +
+ " name VARCHAR(255) NOT NULL PRIMARY KEY,\n" +
+ " appointment TIMESTAMP NOT NULL,\n" +
+ " isEmergency BOOLEAN NOT NULL\n" +
+ ");";
+ connection.createStatement().executeQuery(createTableSmt);
+ }
+
+ public boolean AddPatient(Patient patient) {
+ try (Connection connection = this.GetDbConnection()) {
+ String insertStmt = "INSERT INTO Patient (name, appointment, isEmergency) VALUES (?, ?, ?);";
+
+ PreparedStatement prepStmt = connection.prepareStatement(insertStmt);
+
+ prepStmt.setString(1, patient.getName());
+ prepStmt.setTimestamp(2, Timestamp.valueOf(patient.getAppointment()));
+ prepStmt.setBoolean(3, patient.isEmergency());
+
+ int updates = prepStmt.executeUpdate();
+ prepStmt.close();
+ return updates == 1;
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void DeletePatient(String name) {
+ try (Connection connection = this.GetDbConnection()) {
+ String stmt = "DELETE FROM Patient WHERE name = ?;";
+ PreparedStatement prepStmt = connection.prepareStatement(stmt);
+ prepStmt.setString(1, name);
+ int updates = prepStmt.executeUpdate();
+ prepStmt.close();
+ if (updates != 1) {
+ throw new RuntimeException("Failed to delete patient with name: " + name);
+ }
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public ArrayList GetAllPatients() {
+ ArrayList patients = new ArrayList<>();
+ try (Connection connection = this.GetDbConnection()) {
+ String stmt = "select name, appointment, isemergency from PATIENT;";
+ ResultSet rs = connection.createStatement().executeQuery(stmt);
+
+ while (rs.next()) {
+ Patient p = new Patient(rs.getString(1),
+ rs.getTimestamp(2).toLocalDateTime(),
+ rs.getBoolean(3));
+ patients.add(p);
+ }
+
+ rs.close();
+
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ return patients;
+ }
+}
diff --git a/src/main/java/at/ionas999/health/services/TableCreation.java b/src/main/java/at/ionas999/health/services/TableCreation.java
new file mode 100644
index 0000000..192916c
--- /dev/null
+++ b/src/main/java/at/ionas999/health/services/TableCreation.java
@@ -0,0 +1,10 @@
+package at.ionas999.health.services;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+public interface TableCreation {
+ void CreateTable(Connection connection) throws SQLException;
+
+
+}
diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java
index 57fc47f..3ce42e1 100644
--- a/src/main/java/module-info.java
+++ b/src/main/java/module-info.java
@@ -1,6 +1,7 @@
module at.ionas999.solution {
requires javafx.controls;
requires javafx.fxml;
+ requires java.sql;
opens at.ionas999.health.controller to javafx.fxml;
exports at.ionas999.health;