#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 java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
@ -20,7 +21,7 @@ public class NurseFixture implements Fixture<Nurse> {
public void setupTable(Connection connection) throws SQLException {
final InputStream schema = Main.class.getResourceAsStream("/de/hitec/nhplus/nurse/database/Nurse.sql");
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);
}

View file

@ -6,6 +6,7 @@ import de.hitec.nhplus.patient.Patient;
import de.hitec.nhplus.patient.database.PatientDao;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
@ -23,7 +24,7 @@ public class PatientFixture implements Fixture<Patient> {
// @SuppressWarnings("checkstyle:LineLength")
final InputStream schema = Main.class.getResourceAsStream("/de/hitec/nhplus/patient/database/Patient.sql");
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);
}

View file

@ -7,6 +7,7 @@ import de.hitec.nhplus.treatment.Treatment;
import de.hitec.nhplus.treatment.database.TreatmentDao;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
@ -30,7 +31,7 @@ public class TreatmentFixture implements Fixture<Treatment> {
public void setupTable(Connection connection) throws SQLException {
final InputStream schema = Main.class.getResourceAsStream("/de/hitec/nhplus/treatment/database/Treatment.sql");
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);
}

View file

@ -9,6 +9,7 @@ import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import java.io.IOException;
import java.util.Objects;
public class MainWindowController {
@FXML
@ -40,7 +41,7 @@ public class MainWindowController {
private void loadPatientPage() {
try {
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);
AnchorPane.setTopAnchor(patientsPane, 0d);
@ -55,7 +56,7 @@ public class MainWindowController {
private void loadTreatmentsPage() {
try {
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);
AnchorPane.setTopAnchor(treatmentsPane, 0d);
@ -70,7 +71,7 @@ public class MainWindowController {
private void loadNursePage() {
try {
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);
AnchorPane.setTopAnchor(nursePane, 0d);

View file

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

View file

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

View file

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