added some basic methods

This commit is contained in:
Jonas Hinterdorfer 2025-03-27 12:20:24 +01:00
parent aaff15ede7
commit 6bd8fda834
5 changed files with 132 additions and 0 deletions

View File

@ -20,6 +20,11 @@
<artifactId>javafx-controls</artifactId>
<version>17.0.6</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.17.1.0</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>

View File

@ -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<TableCreation> 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;
}
}

View File

@ -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<Patient> GetAllPatients() {
ArrayList<Patient> 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;
}
}

View File

@ -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;
}

View File

@ -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;