From 6baef72a66981c12a3211b4b7d41ef3fc2f553bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Sun, 5 May 2024 11:59:15 +0200 Subject: [PATCH] #32: Cleanup Useless Lines MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dominik Säume --- .../de/hitec/nhplus/datastorage/DaoImp.java | 16 +-- .../java/de/hitec/nhplus/nurse/NurseDao.java | 93 +++++--------- .../de/hitec/nhplus/patient/PatientDao.java | 107 ++++++---------- .../hitec/nhplus/treatment/TreatmentDao.java | 119 +++++++----------- 4 files changed, 121 insertions(+), 214 deletions(-) diff --git a/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java b/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java index 91f902c..632e21e 100644 --- a/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java +++ b/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java @@ -1,7 +1,9 @@ package de.hitec.nhplus.datastorage; -import java.sql.*; -import java.util.ArrayList; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.List; public abstract class DaoImp implements Dao { @@ -45,13 +47,13 @@ public abstract class DaoImp implements Dao { protected abstract List getListFromResultSet(ResultSet result) throws SQLException; - protected abstract PreparedStatement getCreateStatement(T t); + protected abstract PreparedStatement getCreateStatement(T t) throws SQLException; - protected abstract PreparedStatement getReadByIDStatement(int id); + protected abstract PreparedStatement getReadByIDStatement(int id) throws SQLException; - protected abstract PreparedStatement getReadAllStatement(); + protected abstract PreparedStatement getReadAllStatement() throws SQLException; - protected abstract PreparedStatement getUpdateStatement(T t); + protected abstract PreparedStatement getUpdateStatement(T t) throws SQLException; - protected abstract PreparedStatement getDeleteStatement(int id); + protected abstract PreparedStatement getDeleteStatement(int id) throws SQLException; } diff --git a/src/main/java/de/hitec/nhplus/nurse/NurseDao.java b/src/main/java/de/hitec/nhplus/nurse/NurseDao.java index f7bbbc6..1af8329 100644 --- a/src/main/java/de/hitec/nhplus/nurse/NurseDao.java +++ b/src/main/java/de/hitec/nhplus/nurse/NurseDao.java @@ -15,31 +15,21 @@ public class NurseDao extends DaoImp { } @Override - protected PreparedStatement getCreateStatement(Nurse nurse) { - PreparedStatement preparedStatement = null; - try { - final String SQL = "INSERT INTO nurse (firstname, surname, phoneNumber)" + - "VALUES (?, ?, ?)"; - preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setString(1, nurse.getFirstName()); - preparedStatement.setString(2, nurse.getSurname()); - preparedStatement.setString(3, nurse.getPhoneNumber()); - } catch (SQLException exception) { - exception.printStackTrace(); - } + 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; } @Override - protected PreparedStatement getReadByIDStatement(int id) { - PreparedStatement preparedStatement = null; - try { - final String SQL = "SELECT * FROM nurse WHERE id = ?"; - preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, id); - } catch (SQLException exception) { - exception.printStackTrace(); - } + 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; } @@ -54,63 +44,42 @@ public class NurseDao extends DaoImp { } @Override - protected PreparedStatement getReadAllStatement() { - PreparedStatement statement = null; - try { - final String SQL = "SELECT * FROM nurse"; - statement = this.connection.prepareStatement(SQL); - } catch (SQLException exception) { - exception.printStackTrace(); - } - return statement; + protected PreparedStatement getReadAllStatement() throws SQLException { + final String SQL = "SELECT * FROM nurse"; + return this.connection.prepareStatement(SQL); } @Override protected List getListFromResultSet(ResultSet result) throws SQLException { ArrayList list = new ArrayList<>(); while (result.next()) { - list.add(new Nurse( - result.getInt(1), - result.getString(2), - result.getString(3), - result.getString(4) - )); + list.add(getInstanceFromResultSet(result)); } return list; } @Override - protected PreparedStatement getUpdateStatement(Nurse nurse) { - PreparedStatement preparedStatement = null; - try { - final String SQL = - "UPDATE nurse SET " + - "firstname = ?, " + - "surname = ?, " + - "phoneNumber = ?, " + - "WHERE id = ?"; - 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()); - } catch (SQLException exception) { - exception.printStackTrace(); - } + 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; } @Override - protected PreparedStatement getDeleteStatement(int id) { - PreparedStatement preparedStatement = null; - try { - final String SQL = "DELETE FROM nurse WHERE id = ?"; - preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, id); - } catch (SQLException exception) { - exception.printStackTrace(); - } + 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; } } diff --git a/src/main/java/de/hitec/nhplus/patient/PatientDao.java b/src/main/java/de/hitec/nhplus/patient/PatientDao.java index 0648164..a4a779f 100644 --- a/src/main/java/de/hitec/nhplus/patient/PatientDao.java +++ b/src/main/java/de/hitec/nhplus/patient/PatientDao.java @@ -17,33 +17,23 @@ public class PatientDao extends DaoImp { } @Override - protected PreparedStatement getCreateStatement(Patient patient) { - PreparedStatement preparedStatement = null; - try { - final String SQL = "INSERT INTO patient (firstname, surname, dateOfBirth, carelevel, roomnumber) " + - "VALUES (?, ?, ?, ?, ?)"; - 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()); - } catch (SQLException exception) { - exception.printStackTrace(); - } + 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; } @Override - protected PreparedStatement getReadByIDStatement(int id) { - PreparedStatement preparedStatement = null; - try { - final String SQL = "SELECT * FROM patient WHERE id = ?"; - preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, id); - } catch (SQLException exception) { - exception.printStackTrace(); - } + 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; } @@ -59,68 +49,45 @@ public class PatientDao extends DaoImp { } @Override - protected PreparedStatement getReadAllStatement() { - PreparedStatement statement = null; - try { - final String SQL = "SELECT * FROM patient"; - statement = this.connection.prepareStatement(SQL); - } catch (SQLException exception) { - exception.printStackTrace(); - } - return statement; + protected PreparedStatement getReadAllStatement() throws SQLException { + final String SQL = "SELECT * FROM patient"; + return this.connection.prepareStatement(SQL); } @Override protected List getListFromResultSet(ResultSet result) throws SQLException { ArrayList list = new ArrayList<>(); while (result.next()) { - Patient patient = new Patient( - result.getInt(1), - result.getString(2), - result.getString(3), - DateConverter.convertStringToLocalDate(result.getString(4)), - result.getString(5), - result.getString(6)); - list.add(patient); + list.add(getInstanceFromResultSet(result)); } return list; } @Override - protected PreparedStatement getUpdateStatement(Patient patient) { - PreparedStatement preparedStatement = null; - try { - final String SQL = - "UPDATE patient SET " + - "firstname = ?, " + - "surname = ?, " + - "dateOfBirth = ?, " + - "carelevel = ?, " + - "roomnumber = ?, " + - "WHERE id = ?"; - 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()); - } catch (SQLException exception) { - exception.printStackTrace(); - } + 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; } @Override - protected PreparedStatement getDeleteStatement(int id) { - PreparedStatement preparedStatement = null; - try { - final String SQL = "DELETE FROM patient WHERE id = ?"; - preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, id); - } catch (SQLException exception) { - exception.printStackTrace(); - } + 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; } } diff --git a/src/main/java/de/hitec/nhplus/treatment/TreatmentDao.java b/src/main/java/de/hitec/nhplus/treatment/TreatmentDao.java index 3521e7e..710b6ad 100644 --- a/src/main/java/de/hitec/nhplus/treatment/TreatmentDao.java +++ b/src/main/java/de/hitec/nhplus/treatment/TreatmentDao.java @@ -18,34 +18,24 @@ public class TreatmentDao extends DaoImp { } @Override - protected PreparedStatement getCreateStatement(Treatment treatment) { - PreparedStatement preparedStatement = null; - try { - final String SQL = "INSERT INTO treatment (patientId, treatment_date, begin, end, description, remark) " + - "VALUES (?, ?, ?, ?, ?, ?)"; - 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()); - } catch (SQLException exception) { - exception.printStackTrace(); - } + 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; } @Override - protected PreparedStatement getReadByIDStatement(int id) { - PreparedStatement preparedStatement = null; - try { - final String SQL = "SELECT * FROM treatment WHERE id = ?"; - preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, id); - } catch (SQLException exception) { - exception.printStackTrace(); - } + 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; } @@ -63,15 +53,9 @@ public class TreatmentDao extends DaoImp { } @Override - protected PreparedStatement getReadAllStatement() { - PreparedStatement statement = null; - try { - final String SQL = "SELECT * FROM treatment"; - statement = this.connection.prepareStatement(SQL); - } catch (SQLException exception) { - exception.printStackTrace(); - } - return statement; + protected PreparedStatement getReadAllStatement() throws SQLException { + final String SQL = "SELECT * FROM treatment"; + return this.connection.prepareStatement(SQL); } @Override @@ -83,15 +67,10 @@ public class TreatmentDao extends DaoImp { return list; } - private PreparedStatement getReadAllTreatmentsOfOnePatientByPid(int patientId) { - PreparedStatement preparedStatement = null; - try { - final String SQL = "SELECT * FROM treatment WHERE patientId = ?"; - preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, patientId); - } catch (SQLException exception) { - exception.printStackTrace(); - } + 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; } @@ -101,43 +80,33 @@ public class TreatmentDao extends DaoImp { } @Override - protected PreparedStatement getUpdateStatement(Treatment treatment) { - PreparedStatement preparedStatement = null; - try { - final String SQL = - "UPDATE treatment SET " + - "patientId = ?, " + - "treatment_date = ?, " + - "begin = ?, " + - "end = ?, " + - "description = ?, " + - "remark = ? " + - "WHERE id = ?"; - 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()); - } catch (SQLException exception) { - exception.printStackTrace(); - } + 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; } @Override - protected PreparedStatement getDeleteStatement(int id) { - PreparedStatement preparedStatement = null; - try { - final String SQL = - "DELETE FROM treatment WHERE id = ?"; - preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, id); - } catch (SQLException exception) { - exception.printStackTrace(); - } + 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; } }