Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.32 KB | None | 0 0
  1. package db;
  2.  
  3. import java.sql.*;
  4. import java.util.*;
  5.  
  6. public class dbDoctor {
  7. private static int idDoctor;
  8. private static String username;
  9. private static String name;
  10. private static int phoneNumber;
  11. private static String password;
  12. private static String hospital;
  13. private static Connection conn;
  14. private static ArrayList<String> doctorList =new ArrayList<String>();
  15. private static ArrayList<String> patientList = new ArrayList<>();
  16. private static int pBirthyear;
  17. private static int pPhoneNumber;
  18. private static String pUsername;
  19. private static String pName;
  20. private static String pPassword;
  21. private static int idPatient;
  22. private static int mProteins;
  23. private static int plasmaCells;
  24. private static String date;
  25.  
  26. //sets username and establishes connection with db
  27. public static void instantiateDbDoctor (String username) {
  28. dbDoctor.username = username;
  29. DBConn.connect();
  30. conn = DBConn.conn;
  31. }
  32.  
  33. //Registers doctor in database
  34. public static boolean registerDoctor(String name, String username, String password, int phoneNumber, String hospital) {
  35. instantiateDbDoctor(username);
  36. dbDoctor.name = name;
  37. dbDoctor.username= username;
  38. dbDoctor.password = password;
  39. dbDoctor.phoneNumber=phoneNumber;
  40. dbDoctor.hospital = hospital;
  41. save(conn);
  42.  
  43. return true;
  44.  
  45. }
  46. //Gets doctor from database as strings in a list
  47. public static ArrayList<String> getDoctorFromDB(String username){
  48. instantiateDbDoctor(username);
  49. initialize(conn);
  50. doctorList.add(name);
  51. doctorList.add(username);
  52. doctorList.add(password);
  53. doctorList.add(String.valueOf(phoneNumber));
  54. doctorList.add(hospital);
  55. return doctorList;
  56. }
  57. //Deletes doctor from database, only used in testcases
  58. public static boolean deleteDoctor(String username) {
  59. try {
  60. String query = "delete from doctor where username ="+"\""+username+"\"";
  61. PreparedStatement stmt = conn.prepareStatement(query);
  62.  
  63.  
  64. stmt.executeUpdate();
  65. return true;
  66. } catch (Exception e) {
  67. System.out.println("db error during update of bruker="+e);
  68. return false;}
  69. }
  70.  
  71. public static String getUsername() {
  72. return username;
  73. }
  74.  
  75. public static String getName() {
  76. return name;
  77. }
  78.  
  79. public static int getPhoneNumber() {
  80. return phoneNumber;
  81. }
  82.  
  83. public static String getPassword() {
  84. return password;
  85. }
  86.  
  87. public static String getHospital() {
  88. return hospital;
  89. }
  90.  
  91. //Add patient to doctor in database. Primarily used for testing
  92. public static boolean addPatientDB(String name, String username, String password, int birthyear, int phoneNumber, String docUsername) {
  93. instantiateDbDoctor(docUsername);
  94. System.out.println(conn);
  95. initialize(conn);
  96. try {
  97. System.out.println(idDoctor);
  98. String query = "insert into patient (name, username, password, birthyear, phoneNumber, doctor_idDoctor) values ("+
  99. "\"" +name+"\""+","+"\""+username+"\""+","+"\""+password+"\""+","+ birthyear + ","+phoneNumber+","+ idDoctor+")";
  100. PreparedStatement stmt = conn.prepareStatement(query);
  101. stmt.executeUpdate();
  102. return true;
  103. } catch (Exception e) {
  104. System.out.println("db error during update of bruker="+e);
  105. return false;
  106. }
  107.  
  108. }
  109.  
  110. //helping method for getDoctorFromDB
  111. public static void initialize (Connection conn) {
  112. try {
  113. Statement stmt = conn.createStatement();
  114. ResultSet rs = stmt.executeQuery("select name, password, phoneNumber, hospital, idDoctor from doctor where username="+ "\""+username+"\"");
  115.  
  116. while (rs.next()) {
  117. idDoctor= rs.getInt("idDoctor");
  118. name = rs.getString("name");
  119. password = rs.getString("password");
  120. phoneNumber = rs.getInt("phoneNumber");
  121. hospital = rs.getString("hospital");
  122.  
  123. }
  124.  
  125. } catch (Exception e) {
  126. System.out.println("db error during select of doctor= "+e);
  127. return;
  128. }
  129.  
  130. }
  131.  
  132. //Helping method for registerDoctor
  133. public static void save (Connection conn) {
  134. try {
  135. String query = "insert into doctor (name, username, password, phoneNumber, hospital) values ("+"\"" +name+"\""+","+"\""+username+"\""+","+"\""+password+"\""+","+phoneNumber+","+"\""+hospital+"\""+")";
  136. PreparedStatement stmt = conn.prepareStatement(query);
  137.  
  138.  
  139. stmt.executeUpdate();
  140. } catch (Exception e) {
  141. System.out.println("db error during update of bruker="+e);
  142. return;
  143. }
  144. }
  145. //Method that returns all patients for specific doctor as a list with strings
  146. public static ArrayList<String> getPatients(String username){
  147. instantiateDbDoctor(username);
  148. initialize(conn);
  149. try {
  150. Statement stmt = conn.createStatement();
  151. ResultSet rs = stmt.executeQuery("select idPatient, name, username, password, phoneNumber, birthyear from patient where doctor_idDoctor="+ idDoctor);
  152. while (rs.next()) {
  153. idPatient =rs.getInt("idPatient");
  154. pName = rs.getString("name");
  155. pUsername=rs.getString("username");
  156. pPassword = rs.getString("password");
  157. pPhoneNumber = rs.getInt("phoneNumber");
  158. pBirthyear = rs.getInt("birthyear");
  159. String s = "";
  160. s +=pName+","+pUsername+","+pPassword+","+pPhoneNumber+","+pBirthyear+"!";
  161. s+=getPatientData(idPatient);
  162. patientList.add(s);
  163.  
  164.  
  165. }
  166.  
  167. } catch (Exception e) {
  168. System.out.println("db error during select of doctor= "+e);
  169. return null;
  170. }
  171. return patientList;
  172. }
  173. //Method that gives patientData as string to getPatients
  174. private static String getPatientData(int idPatient) {
  175. String st = "";
  176. try {
  177. Statement stmt = conn.createStatement();
  178. ResultSet rs = stmt.executeQuery("select mProteins, plasmaCells, date from patientData where patient_idPatient="+ idPatient);
  179. while (rs.next()) {
  180. mProteins = rs.getInt("mProteins");
  181. plasmaCells = rs.getInt("plasmaCells");
  182. date = rs.getString("date");
  183. st += mProteins+";"+plasmaCells+";"+date+"|";
  184. }
  185.  
  186. } catch (Exception e) {
  187. System.out.println("db error during select doctor= "+e);
  188. return null;
  189. }
  190. return st;
  191. }
  192. // ToString for testing in console
  193. // @Override
  194. // public String toString() {
  195. // return "("+name+", " + username + ", " + idDoctor + ")";
  196. // }
  197. //Main method for testing
  198. // public static void main(String[] args) {
  199. // addPatientDB("Farmor Far", "pasientbruker", "pass", 1915, 99999999, "bruker");
  200. //
  201. // }
  202. // System.out.println(getDoctorFromDB("bruker"));
  203. // registerDoctor("Kristian hagen", "krishag@msn.no", "pass", 99999999, "skole");
  204. // deleteDoctor("krishag@msn.no");
  205. // System.out.println(getPatients("bruker"));
  206. //
  207. // }
  208.  
  209.  
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement