Im Rahmen der Veranstaltung "CS3330 - Projektpraktikum MedizinischeInformatik" an der Universität zu Lübeck entstandenes Krankenhausinformationssystem (KIS).
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

222 行
8.8 KiB

  1. package de.uniluebeck.mi.projmi6.db;
  2. import de.uniluebeck.mi.projmi6.model.*;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. /**
  10. * Created by nils on 15.11.2015.
  11. */
  12. public class DBHandler {
  13. private static final String SELECT_ALL_PATIENTS = "SELECT * FROM `patient`";
  14. private static final String SELECT_PATIENT_BY_ID = "SELECT * FROM `patient` WHERE `id` = ?";
  15. private static final String UPDATE_PATIENT = "UPDATE `patient` SET `CAVE`=?, `Familienstand`=?, " +
  16. "`Geburtsdatum`=?, `Geburtsname`=?, `Geschlecht`=?, `KassenID`=?, `LetzterBearbeiter`=?, `Nachname`=?, " +
  17. "`Ort`=?, `PLZ`=?, `Strasse`=?, `Telefon`=?, `Versichertennummer`=?, `Vorname`=? WHERE `ID`=?";
  18. private static final String SELECT_ALL_STATIONEN = "SELECT * FROM `stammstation`";
  19. private static final String SELECT_FAELLE_BY_PATID = "SELECT * FROM `fall` WHERE `patientid` = ?";
  20. private static final String SELECT_DIAGNOSE_BY_ID = "SELECT * FROM `diagnose` WHERE `diagid` = ?";
  21. private static final String SELECT_ALL_ICD10CODES = "SELECT * FROM `stammicd10`";
  22. private static final String SELECT_ALL_OPSCODES = "SELECT * FROM `stammops`";
  23. private static final String SELECT_ICD10CODE_BY_ID = "SELECT * FROM `stammicd10` WHERE `icd10code` = '?' AND `version` = ?";
  24. private static final String SELECT_OPSCODE_BY_ID = "SELECT * FROM `stammops` WHERE `opscode` = '?' AND `version` = ?";
  25. public static List<Patient> getAllPatients() throws SQLException {
  26. Statement statement = MySqlConnFactory.getConnection().createStatement();
  27. ResultSet rs = statement.executeQuery(SELECT_ALL_PATIENTS);
  28. List<Patient> patients = new ArrayList<>();
  29. while (rs.next()) {
  30. patients.add(getPatient(rs));
  31. }
  32. return patients;
  33. }
  34. public static Patient getPatient(int id) throws SQLException {
  35. PreparedStatement statement = MySqlConnFactory.getConnection().prepareStatement(SELECT_PATIENT_BY_ID);
  36. ResultSet rs;
  37. statement.setInt(1, id);
  38. rs = statement.executeQuery();
  39. return getPatient(rs);
  40. }
  41. private static Patient getPatient(ResultSet rs) throws SQLException {
  42. Patient patient = new Patient();
  43. patient.setPatID(rs.getInt("id"));
  44. patient.setGeburtsname(rs.getString("geburtsname"));
  45. patient.setVorname(rs.getString("vorname"));
  46. patient.setNachname(rs.getString("nachname"));
  47. if (rs.getDate("geburtsdatum") != null)
  48. patient.setGeburtsdatum(rs.getDate("geburtsdatum").toLocalDate());
  49. patient.setStrasse(rs.getString("strasse"));
  50. patient.setHausnummer(rs.getString("hausnummer"));
  51. patient.setPlz(rs.getString("plz"));
  52. patient.setTelefon(rs.getString("telefon"));
  53. // patient.setFamilienstand(rs.getString("familienstand")); TODO
  54. // patient.setGeschlecht(Patient.Geschlecht.valueOf(rs.getString("geschlecht"))); TODO
  55. patient.setVersichertennummer(rs.getString("versichertennummer"));
  56. patient.setKassenID(rs.getInt("kassenid"));
  57. setVersionInformation(patient, rs);
  58. return patient;
  59. }
  60. public static void updatePatient(Patient patient) throws SQLException {
  61. PreparedStatement statement = MySqlConnFactory.getConnection().prepareStatement(UPDATE_PATIENT);
  62. statement.setString(1, patient.getCave());
  63. statement.setString(2, patient.getFamilienstand().toString());
  64. // TODO
  65. }
  66. public static List<Station> getAllStationen() throws SQLException {
  67. Statement statement = MySqlConnFactory.getConnection().createStatement();
  68. ResultSet rs = statement.executeQuery(SELECT_ALL_STATIONEN);
  69. List<Station> stationen = new ArrayList<>();
  70. while (rs.next()) {
  71. stationen.add(getStation(rs));
  72. }
  73. return stationen;
  74. }
  75. private static Station getStation(ResultSet rs) throws SQLException {
  76. Station station = new Station();
  77. station.setStation(rs.getString("station"));
  78. station.setBezeichnung(rs.getString("bezeichnung"));
  79. station.setBezeichnungLang(rs.getString("bezeichnunglang"));
  80. station.setStationstyp(rs.getInt("stationstyp"));
  81. return station;
  82. }
  83. public static List<Fall> getFaelleByPatID(int id) throws SQLException {
  84. PreparedStatement statement = MySqlConnFactory.getConnection().prepareStatement(SELECT_FAELLE_BY_PATID);
  85. statement.setInt(1, id);
  86. ResultSet rs = statement.executeQuery();
  87. List<Fall> faelle = new ArrayList<>();
  88. while (rs.next()) {
  89. faelle.add(getFall(rs));
  90. }
  91. return faelle;
  92. }
  93. private static Fall getFall(ResultSet rs) throws SQLException {
  94. Fall fall = new Fall();
  95. fall.setFallID(rs.getInt("fallid"));
  96. if (rs.getTimestamp("aufnahmedatum") != null)
  97. fall.setAufnahmeDatum(rs.getTimestamp("aufnahmedatum").toLocalDateTime());
  98. if (rs.getTimestamp("entlassungsdatum") != null)
  99. fall.setEntlassungsDatum(rs.getTimestamp("entlassungsdatum").toLocalDateTime());
  100. if (rs.getInt("hauptdiagnose") != 0)
  101. fall.setHauptDiagnose(getDiagnose(rs.getInt("hauptdiagnose"), fall));
  102. return fall;
  103. }
  104. private static Diagnose getDiagnose(int hauptdiagnose, Fall fall) throws SQLException {
  105. Diagnose diagnose = getDiagnose(hauptdiagnose);
  106. diagnose.setFall(fall);
  107. return diagnose;
  108. }
  109. private static Diagnose getDiagnose(int id) throws SQLException {
  110. PreparedStatement statement = MySqlConnFactory.getConnection().prepareStatement(SELECT_DIAGNOSE_BY_ID);
  111. ResultSet rs = statement.executeQuery();
  112. return getDiagnose(rs);
  113. }
  114. private static Diagnose getDiagnose(ResultSet rs) throws SQLException {
  115. Diagnose diagnose = new Diagnose();
  116. diagnose.setFreiText(rs.getString("freitext"));
  117. diagnose.setArzt(new Mitarbeiter(rs.getInt("arzt")));
  118. diagnose.setDiagArt(DiagArt.parseInt(rs.getInt("diagart")));
  119. setVersionInformation(diagnose, rs);
  120. diagnose.setIcd10code(getIcd10Code(rs.getString("icd10code"), rs.getInt("icd10version")));
  121. return diagnose;
  122. }
  123. private static void setVersionInformation(Version version, ResultSet rs) throws SQLException {
  124. version.setErsteller(rs.getInt("ersteller"));
  125. if (rs.getTimestamp("erstelldatum") != null)
  126. version.setErstellDatumZeit(rs.getTimestamp("erstelldatum").toLocalDateTime());
  127. version.setBearbeiter(rs.getInt("letzterbearbeiter"));
  128. if (rs.getTimestamp("letztesbearbeitungsdatum") != null)
  129. version.setBearbeitetDatumZeit(rs.getTimestamp("letztesbearbeitungsdatum").toLocalDateTime());
  130. }
  131. public static List<Icd10Code> getAllIcd10Codes() throws SQLException {
  132. Statement statement = MySqlConnFactory.getConnection().createStatement();
  133. ResultSet rs = statement.executeQuery(SELECT_ALL_ICD10CODES);
  134. List<Icd10Code> icd10codes = new ArrayList<>();
  135. while (rs.next()) {
  136. icd10codes.add(getIcd10Code(rs));
  137. }
  138. return icd10codes;
  139. }
  140. private static Icd10Code getIcd10Code(String code, int version) throws SQLException {
  141. PreparedStatement statement = MySqlConnFactory.getConnection().prepareStatement(SELECT_ICD10CODE_BY_ID);
  142. ResultSet rs;
  143. statement.setString(1, code);
  144. statement.setInt(2, version);
  145. rs = statement.executeQuery();
  146. return getIcd10Code(rs);
  147. }
  148. private static Icd10Code getIcd10Code(ResultSet rs) throws SQLException {
  149. String code = rs.getString("icd10code");
  150. String text = rs.getString("text");
  151. int version = rs.getInt("version");
  152. return new Icd10Code(code, text, version);
  153. }
  154. public static List<OpsCode> getAllOpsCodes() throws SQLException {
  155. Statement statement = MySqlConnFactory.getConnection().createStatement();
  156. ResultSet rs = statement.executeQuery(SELECT_ALL_OPSCODES);
  157. List<OpsCode> opscodes = new ArrayList<>();
  158. while (rs.next()) {
  159. opscodes.add(getOpsCode(rs));
  160. }
  161. return opscodes;
  162. }
  163. public static OpsCode getOpsCode(String code, int version) throws SQLException {
  164. PreparedStatement statement = MySqlConnFactory.getConnection().prepareStatement(SELECT_OPSCODE_BY_ID);
  165. ResultSet rs;
  166. statement.setString(1, code);
  167. statement.setInt(2, version);
  168. rs = statement.executeQuery();
  169. return getOpsCode(rs);
  170. }
  171. private static OpsCode getOpsCode(ResultSet rs) throws SQLException {
  172. String code = rs.getString("opscode");
  173. String text = rs.getString("text");
  174. int version = rs.getInt("version");
  175. return new OpsCode(code, text, version);
  176. }
  177. }