Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package db;
- import java.sql.*;
- import java.util.*;
- public class dbDoctor {
- private static int idDoctor;
- private static String username;
- private static String name;
- private static int phoneNumber;
- private static String password;
- private static String hospital;
- private static Connection conn;
- private static ArrayList<String> doctorList =new ArrayList<String>();
- private static ArrayList<String> patientList = new ArrayList<>();
- private static int pBirthyear;
- private static int pPhoneNumber;
- private static String pUsername;
- private static String pName;
- private static String pPassword;
- private static int idPatient;
- private static int mProteins;
- private static int plasmaCells;
- private static String date;
- //sets username and establishes connection with db
- public static void instantiateDbDoctor (String username) {
- dbDoctor.username = username;
- DBConn.connect();
- conn = DBConn.conn;
- }
- //Registers doctor in database
- public static boolean registerDoctor(String name, String username, String password, int phoneNumber, String hospital) {
- instantiateDbDoctor(username);
- dbDoctor.name = name;
- dbDoctor.username= username;
- dbDoctor.password = password;
- dbDoctor.phoneNumber=phoneNumber;
- dbDoctor.hospital = hospital;
- save(conn);
- return true;
- }
- //Gets doctor from database as strings in a list
- public static ArrayList<String> getDoctorFromDB(String username){
- instantiateDbDoctor(username);
- initialize(conn);
- doctorList.add(name);
- doctorList.add(username);
- doctorList.add(password);
- doctorList.add(String.valueOf(phoneNumber));
- doctorList.add(hospital);
- return doctorList;
- }
- //Deletes doctor from database, only used in testcases
- public static boolean deleteDoctor(String username) {
- try {
- String query = "delete from doctor where username ="+"\""+username+"\"";
- PreparedStatement stmt = conn.prepareStatement(query);
- stmt.executeUpdate();
- return true;
- } catch (Exception e) {
- System.out.println("db error during update of bruker="+e);
- return false;}
- }
- public static String getUsername() {
- return username;
- }
- public static String getName() {
- return name;
- }
- public static int getPhoneNumber() {
- return phoneNumber;
- }
- public static String getPassword() {
- return password;
- }
- public static String getHospital() {
- return hospital;
- }
- //Add patient to doctor in database. Primarily used for testing
- public static boolean addPatientDB(String name, String username, String password, int birthyear, int phoneNumber, String docUsername) {
- instantiateDbDoctor(docUsername);
- System.out.println(conn);
- initialize(conn);
- try {
- System.out.println(idDoctor);
- String query = "insert into patient (name, username, password, birthyear, phoneNumber, doctor_idDoctor) values ("+
- "\"" +name+"\""+","+"\""+username+"\""+","+"\""+password+"\""+","+ birthyear + ","+phoneNumber+","+ idDoctor+")";
- PreparedStatement stmt = conn.prepareStatement(query);
- stmt.executeUpdate();
- return true;
- } catch (Exception e) {
- System.out.println("db error during update of bruker="+e);
- return false;
- }
- }
- //helping method for getDoctorFromDB
- public static void initialize (Connection conn) {
- try {
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("select name, password, phoneNumber, hospital, idDoctor from doctor where username="+ "\""+username+"\"");
- while (rs.next()) {
- idDoctor= rs.getInt("idDoctor");
- name = rs.getString("name");
- password = rs.getString("password");
- phoneNumber = rs.getInt("phoneNumber");
- hospital = rs.getString("hospital");
- }
- } catch (Exception e) {
- System.out.println("db error during select of doctor= "+e);
- return;
- }
- }
- //Helping method for registerDoctor
- public static void save (Connection conn) {
- try {
- String query = "insert into doctor (name, username, password, phoneNumber, hospital) values ("+"\"" +name+"\""+","+"\""+username+"\""+","+"\""+password+"\""+","+phoneNumber+","+"\""+hospital+"\""+")";
- PreparedStatement stmt = conn.prepareStatement(query);
- stmt.executeUpdate();
- } catch (Exception e) {
- System.out.println("db error during update of bruker="+e);
- return;
- }
- }
- //Method that returns all patients for specific doctor as a list with strings
- public static ArrayList<String> getPatients(String username){
- instantiateDbDoctor(username);
- initialize(conn);
- try {
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("select idPatient, name, username, password, phoneNumber, birthyear from patient where doctor_idDoctor="+ idDoctor);
- while (rs.next()) {
- idPatient =rs.getInt("idPatient");
- pName = rs.getString("name");
- pUsername=rs.getString("username");
- pPassword = rs.getString("password");
- pPhoneNumber = rs.getInt("phoneNumber");
- pBirthyear = rs.getInt("birthyear");
- String s = "";
- s +=pName+","+pUsername+","+pPassword+","+pPhoneNumber+","+pBirthyear+"!";
- s+=getPatientData(idPatient);
- patientList.add(s);
- }
- } catch (Exception e) {
- System.out.println("db error during select of doctor= "+e);
- return null;
- }
- return patientList;
- }
- //Method that gives patientData as string to getPatients
- private static String getPatientData(int idPatient) {
- String st = "";
- try {
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("select mProteins, plasmaCells, date from patientData where patient_idPatient="+ idPatient);
- while (rs.next()) {
- mProteins = rs.getInt("mProteins");
- plasmaCells = rs.getInt("plasmaCells");
- date = rs.getString("date");
- st += mProteins+";"+plasmaCells+";"+date+"|";
- }
- } catch (Exception e) {
- System.out.println("db error during select doctor= "+e);
- return null;
- }
- return st;
- }
- // ToString for testing in console
- // @Override
- // public String toString() {
- // return "("+name+", " + username + ", " + idDoctor + ")";
- // }
- //Main method for testing
- // public static void main(String[] args) {
- // addPatientDB("Farmor Far", "pasientbruker", "pass", 1915, 99999999, "bruker");
- //
- // }
- // System.out.println(getDoctorFromDB("bruker"));
- // registerDoctor("Kristian hagen", "krishag@msn.no", "pass", 99999999, "skole");
- // deleteDoctor("krishag@msn.no");
- // System.out.println(getPatients("bruker"));
- //
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement