#32: Refactor Database Usage, to be more consistent and understandable #33

Merged
SZUT-Dominik merged 6 commits from task/refactor-dao into main 2024-05-06 12:23:41 +00:00
7 changed files with 115 additions and 100 deletions
Showing only changes of commit afe306a364 - Show all commits

View file

@ -6,6 +6,7 @@ import de.hitec.nhplus.nurse.Nurse;
import de.hitec.nhplus.nurse.database.NurseDao; import de.hitec.nhplus.nurse.database.NurseDao;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.*; import java.util.*;
@ -20,7 +21,7 @@ public class NurseFixture implements Fixture<Nurse> {
public void setupTable(Connection connection) throws SQLException { public void setupTable(Connection connection) throws SQLException {
final InputStream schema = Main.class.getResourceAsStream("/de/hitec/nhplus/nurse/database/Nurse.sql"); final InputStream schema = Main.class.getResourceAsStream("/de/hitec/nhplus/nurse/database/Nurse.sql");
assert schema != null; assert schema != null;
final String SQL = new Scanner(schema, "UTF-8").useDelimiter("\\A").next(); final String SQL = new Scanner(schema, StandardCharsets.UTF_8).useDelimiter("\\A").next();
connection.createStatement().execute(SQL); connection.createStatement().execute(SQL);
} }

View file

@ -6,6 +6,7 @@ import de.hitec.nhplus.patient.Patient;
import de.hitec.nhplus.patient.database.PatientDao; import de.hitec.nhplus.patient.database.PatientDao;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.*; import java.util.*;
@ -23,7 +24,7 @@ public class PatientFixture implements Fixture<Patient> {
// @SuppressWarnings("checkstyle:LineLength") // @SuppressWarnings("checkstyle:LineLength")
final InputStream schema = Main.class.getResourceAsStream("/de/hitec/nhplus/patient/database/Patient.sql"); final InputStream schema = Main.class.getResourceAsStream("/de/hitec/nhplus/patient/database/Patient.sql");
assert schema != null; assert schema != null;
final String SQL = new Scanner(schema, "UTF-8").useDelimiter("\\A").next(); final String SQL = new Scanner(schema, StandardCharsets.UTF_8).useDelimiter("\\A").next();
connection.createStatement().execute(SQL); connection.createStatement().execute(SQL);
} }

View file

@ -7,6 +7,7 @@ import de.hitec.nhplus.treatment.Treatment;
import de.hitec.nhplus.treatment.database.TreatmentDao; import de.hitec.nhplus.treatment.database.TreatmentDao;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.*; import java.util.*;
@ -30,7 +31,7 @@ public class TreatmentFixture implements Fixture<Treatment> {
public void setupTable(Connection connection) throws SQLException { public void setupTable(Connection connection) throws SQLException {
final InputStream schema = Main.class.getResourceAsStream("/de/hitec/nhplus/treatment/database/Treatment.sql"); final InputStream schema = Main.class.getResourceAsStream("/de/hitec/nhplus/treatment/database/Treatment.sql");
assert schema != null; assert schema != null;
final String SQL = new Scanner(schema, "UTF-8").useDelimiter("\\A").next(); final String SQL = new Scanner(schema, StandardCharsets.UTF_8).useDelimiter("\\A").next();
connection.createStatement().execute(SQL); connection.createStatement().execute(SQL);
} }

View file

@ -9,6 +9,7 @@ import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import java.io.IOException; import java.io.IOException;
import java.util.Objects;
public class MainWindowController { public class MainWindowController {
@FXML @FXML
@ -40,7 +41,7 @@ public class MainWindowController {
private void loadPatientPage() { private void loadPatientPage() {
try { try {
BorderPane patientsPane = FXMLLoader.load( BorderPane patientsPane = FXMLLoader.load(
Main.class.getResource("/de/hitec/nhplus/patient/AllPatientView.fxml") Objects.requireNonNull(Main.class.getResource("/de/hitec/nhplus/patient/AllPatientView.fxml"))
); );
patientPage.getChildren().setAll(patientsPane); patientPage.getChildren().setAll(patientsPane);
AnchorPane.setTopAnchor(patientsPane, 0d); AnchorPane.setTopAnchor(patientsPane, 0d);
@ -55,7 +56,7 @@ public class MainWindowController {
private void loadTreatmentsPage() { private void loadTreatmentsPage() {
try { try {
BorderPane treatmentsPane = FXMLLoader.load( BorderPane treatmentsPane = FXMLLoader.load(
Main.class.getResource("/de/hitec/nhplus/treatment/AllTreatmentView.fxml") Objects.requireNonNull(Main.class.getResource("/de/hitec/nhplus/treatment/AllTreatmentView.fxml"))
); );
treatmentPage.getChildren().setAll(treatmentsPane); treatmentPage.getChildren().setAll(treatmentsPane);
AnchorPane.setTopAnchor(treatmentsPane, 0d); AnchorPane.setTopAnchor(treatmentsPane, 0d);
@ -70,7 +71,7 @@ public class MainWindowController {
private void loadNursePage() { private void loadNursePage() {
try { try {
BorderPane nursePane = FXMLLoader.load( BorderPane nursePane = FXMLLoader.load(
Main.class.getResource("/de/hitec/nhplus/nurse/AllNurseView.fxml") Objects.requireNonNull(Main.class.getResource("/de/hitec/nhplus/nurse/AllNurseView.fxml"))
); );
nursePage.getChildren().setAll(nursePane); nursePage.getChildren().setAll(nursePane);
AnchorPane.setTopAnchor(nursePane, 0d); AnchorPane.setTopAnchor(nursePane, 0d);

View file

@ -17,21 +17,24 @@ public class NurseDao extends DaoImp<Nurse> {
@Override @Override
protected PreparedStatement getCreateStatement(Nurse nurse) throws SQLException { protected PreparedStatement getCreateStatement(Nurse nurse) throws SQLException {
final String SQL = "INSERT INTO nurse (firstname, surname, phoneNumber)" + final String SQL = """
"VALUES (?, ?, ?)"; INSERT INTO nurse
PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); (firstname, surname, phoneNumber)
preparedStatement.setString(1, nurse.getFirstName()); VALUES (?, ?, ?)
preparedStatement.setString(2, nurse.getSurname()); """;
preparedStatement.setString(3, nurse.getPhoneNumber()); PreparedStatement statement = this.connection.prepareStatement(SQL);
return preparedStatement; statement.setString(1, nurse.getFirstName());
statement.setString(2, nurse.getSurname());
statement.setString(3, nurse.getPhoneNumber());
return statement;
} }
@Override @Override
protected PreparedStatement getReadByIDStatement(int id) throws SQLException { protected PreparedStatement getReadByIDStatement(int id) throws SQLException {
final String SQL = "SELECT * FROM nurse WHERE id = ?"; final String SQL = "SELECT * FROM nurse WHERE id = ?";
PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); PreparedStatement statement = this.connection.prepareStatement(SQL);
preparedStatement.setInt(1, id); statement.setInt(1, id);
return preparedStatement; return statement;
} }
@Override @Override
@ -59,28 +62,28 @@ public class NurseDao extends DaoImp<Nurse> {
return list; return list;
} }
@Override @Override
protected PreparedStatement getUpdateStatement(Nurse nurse) throws SQLException { protected PreparedStatement getUpdateStatement(Nurse nurse) throws SQLException {
final String SQL = final String SQL = """
"UPDATE nurse SET " + UPDATE nurse SET
"firstname = ?, " + firstname = ?,
"surname = ?, " + surname = ?,
"phoneNumber = ? " + phoneNumber = ?
"WHERE id = ?"; WHERE id = ?
PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); """;
preparedStatement.setString(1, nurse.getFirstName()); PreparedStatement statement = this.connection.prepareStatement(SQL);
preparedStatement.setString(2, nurse.getSurname()); statement.setString(1, nurse.getFirstName());
preparedStatement.setString(3, nurse.getPhoneNumber()); statement.setString(2, nurse.getSurname());
preparedStatement.setInt(4, nurse.getId()); statement.setString(3, nurse.getPhoneNumber());
return preparedStatement; statement.setInt(4, nurse.getId());
return statement;
} }
@Override @Override
protected PreparedStatement getDeleteStatement(int id) throws SQLException { protected PreparedStatement getDeleteStatement(int id) throws SQLException {
final String SQL = "DELETE FROM nurse WHERE id = ?"; final String SQL = "DELETE FROM nurse WHERE id = ?";
PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); PreparedStatement statement = this.connection.prepareStatement(SQL);
preparedStatement.setInt(1, id); statement.setInt(1, id);
return preparedStatement; return statement;
} }
} }

View file

@ -19,23 +19,26 @@ public class PatientDao extends DaoImp<Patient> {
@Override @Override
protected PreparedStatement getCreateStatement(Patient patient) throws SQLException { protected PreparedStatement getCreateStatement(Patient patient) throws SQLException {
final String SQL = "INSERT INTO patient (firstname, surname, dateOfBirth, carelevel, roomnumber) " + final String SQL = """
"VALUES (?, ?, ?, ?, ?)"; INSERT INTO patient
PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); (firstname, surname, dateOfBirth, carelevel, roomnumber)
preparedStatement.setString(1, patient.getFirstName()); VALUES (?, ?, ?, ?, ?)
preparedStatement.setString(2, patient.getSurname()); """;
preparedStatement.setString(3, patient.getDateOfBirth()); PreparedStatement statement = this.connection.prepareStatement(SQL);
preparedStatement.setString(4, patient.getCareLevel()); statement.setString(1, patient.getFirstName());
preparedStatement.setString(5, patient.getRoomNumber()); statement.setString(2, patient.getSurname());
return preparedStatement; statement.setString(3, patient.getDateOfBirth());
statement.setString(4, patient.getCareLevel());
statement.setString(5, patient.getRoomNumber());
return statement;
} }
@Override @Override
protected PreparedStatement getReadByIDStatement(int id) throws SQLException { protected PreparedStatement getReadByIDStatement(int id) throws SQLException {
final String SQL = "SELECT * FROM patient WHERE id = ?"; final String SQL = "SELECT * FROM patient WHERE id = ?";
PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); PreparedStatement statement = this.connection.prepareStatement(SQL);
preparedStatement.setInt(1, id); statement.setInt(1, id);
return preparedStatement; return statement;
} }
@Override @Override
@ -66,29 +69,30 @@ public class PatientDao extends DaoImp<Patient> {
@Override @Override
protected PreparedStatement getUpdateStatement(Patient patient) throws SQLException { protected PreparedStatement getUpdateStatement(Patient patient) throws SQLException {
final String SQL = final String SQL = """
"UPDATE patient SET " + UPDATE patient SET
"firstname = ?, " + firstname = ?,
"surname = ?, " + surname = ?,
"dateOfBirth = ?, " + dateOfBirth = ?,
"carelevel = ?, " + carelevel = ?,
"roomnumber = ?, " + roomnumber = ?,
"WHERE id = ?"; WHERE id = ?
PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); """;
preparedStatement.setString(1, patient.getFirstName()); PreparedStatement statement = this.connection.prepareStatement(SQL);
preparedStatement.setString(2, patient.getSurname()); statement.setString(1, patient.getFirstName());
preparedStatement.setString(3, patient.getDateOfBirth()); statement.setString(2, patient.getSurname());
preparedStatement.setString(4, patient.getCareLevel()); statement.setString(3, patient.getDateOfBirth());
preparedStatement.setString(5, patient.getRoomNumber()); statement.setString(4, patient.getCareLevel());
preparedStatement.setInt(6, patient.getId()); statement.setString(5, patient.getRoomNumber());
return preparedStatement; statement.setInt(6, patient.getId());
return statement;
} }
@Override @Override
protected PreparedStatement getDeleteStatement(int id) throws SQLException { protected PreparedStatement getDeleteStatement(int id) throws SQLException {
final String SQL = "DELETE FROM patient WHERE id = ?"; final String SQL = "DELETE FROM patient WHERE id = ?";
PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); PreparedStatement statement = this.connection.prepareStatement(SQL);
preparedStatement.setInt(1, id); statement.setInt(1, id);
return preparedStatement; return statement;
} }
} }

View file

@ -20,24 +20,27 @@ public class TreatmentDao extends DaoImp<Treatment> {
@Override @Override
protected PreparedStatement getCreateStatement(Treatment treatment) throws SQLException { protected PreparedStatement getCreateStatement(Treatment treatment) throws SQLException {
final String SQL = "INSERT INTO treatment (patientId, treatment_date, begin, end, description, remark) " + final String SQL = """
"VALUES (?, ?, ?, ?, ?, ?)"; INSERT INTO treatment
PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); (patientId, treatment_date, begin, end, description, remark)
preparedStatement.setInt(1, treatment.getPatient().getId()); VALUES (?, ?, ?, ?, ?, ?)
preparedStatement.setString(2, treatment.getDate()); """;
preparedStatement.setString(3, treatment.getBegin()); PreparedStatement statement = this.connection.prepareStatement(SQL);
preparedStatement.setString(4, treatment.getEnd()); statement.setInt(1, treatment.getPatient().getId());
preparedStatement.setString(5, treatment.getDescription()); statement.setString(2, treatment.getDate());
preparedStatement.setString(6, treatment.getRemarks()); statement.setString(3, treatment.getBegin());
return preparedStatement; statement.setString(4, treatment.getEnd());
statement.setString(5, treatment.getDescription());
statement.setString(6, treatment.getRemarks());
return statement;
} }
@Override @Override
protected PreparedStatement getReadByIDStatement(int id) throws SQLException { protected PreparedStatement getReadByIDStatement(int id) throws SQLException {
final String SQL = "SELECT * FROM treatment WHERE id = ?"; final String SQL = "SELECT * FROM treatment WHERE id = ?";
PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); PreparedStatement statement = this.connection.prepareStatement(SQL);
preparedStatement.setInt(1, id); statement.setInt(1, id);
return preparedStatement; return statement;
} }
@Override @Override
@ -70,9 +73,9 @@ public class TreatmentDao extends DaoImp<Treatment> {
private PreparedStatement getReadAllTreatmentsOfOnePatientByPid(int patientId) throws SQLException { private PreparedStatement getReadAllTreatmentsOfOnePatientByPid(int patientId) throws SQLException {
final String SQL = "SELECT * FROM treatment WHERE patientId = ?"; final String SQL = "SELECT * FROM treatment WHERE patientId = ?";
PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); PreparedStatement statement = this.connection.prepareStatement(SQL);
preparedStatement.setInt(1, patientId); statement.setInt(1, patientId);
return preparedStatement; return statement;
} }
public List<Treatment> readTreatmentsByPid(int patientId) throws SQLException { public List<Treatment> readTreatmentsByPid(int patientId) throws SQLException {
@ -82,32 +85,33 @@ public class TreatmentDao extends DaoImp<Treatment> {
@Override @Override
protected PreparedStatement getUpdateStatement(Treatment treatment) throws SQLException { protected PreparedStatement getUpdateStatement(Treatment treatment) throws SQLException {
final String SQL = final String SQL = """
"UPDATE treatment SET " + UPDATE treatment SET
"patientId = ?, " + patientId = ?,
"treatment_date = ?, " + treatment_date = ?,
"begin = ?, " + begin = ?,
"end = ?, " + end = ?,
"description = ?, " + description = ?,
"remark = ? " + remark = ?
"WHERE id = ?"; WHERE id = ?
PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); """;
preparedStatement.setInt(1, treatment.getPatient().getId()); PreparedStatement statement = this.connection.prepareStatement(SQL);
preparedStatement.setString(2, treatment.getDate()); statement.setInt(1, treatment.getPatient().getId());
preparedStatement.setString(3, treatment.getBegin()); statement.setString(2, treatment.getDate());
preparedStatement.setString(4, treatment.getEnd()); statement.setString(3, treatment.getBegin());
preparedStatement.setString(5, treatment.getDescription()); statement.setString(4, treatment.getEnd());
preparedStatement.setString(6, treatment.getRemarks()); statement.setString(5, treatment.getDescription());
preparedStatement.setInt(7, treatment.getId()); statement.setString(6, treatment.getRemarks());
return preparedStatement; statement.setInt(7, treatment.getId());
return statement;
} }
@Override @Override
protected PreparedStatement getDeleteStatement(int id) throws SQLException { protected PreparedStatement getDeleteStatement(int id) throws SQLException {
final String SQL = final String SQL =
"DELETE FROM treatment WHERE id = ?"; "DELETE FROM treatment WHERE id = ?";
PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); PreparedStatement statement = this.connection.prepareStatement(SQL);
preparedStatement.setInt(1, id); statement.setInt(1, id);
return preparedStatement; return statement;
} }
} }