repl.it
@Corki64/

Repl>IT_HospitalDataJavaInterface

Java

testVersion of 5122 assignment

fork
loading
Files
  • Main.java
  • jdt.ls-java-project
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DateTimeException;

import com.mysql.jbdc.Connection;
import com.mysql.jbdc.Statement;
/**
 * Nearly a total rewrite of last program turn in. I hope I am doing this correctly. CPSC_5133
 *
 *
 * @author  Luis Cortez
 * @version 0911192030
 */
public class HospitalDataJavaInterface {
    public static void main(String[] args) throws IOException {
        HospitalDataJavaInterface app = new HospitalDataJavaInterface();


        String location = "D://personTableData.txt";
        app.readPersonTableData(location);


        String patientDoctorListLocation = "D:\\patientDoctorList.txt";
        app.readPatientDoctorListData(patientDoctorListLocation);

        String treatmentDataListLocation = "D:\\treatmentData.txt";
        app.readTreatmentData(treatmentDataListLocation);
    }

    private void readTreatmentData(String treatmentDataListLocation) throws IOException {
        String patient_last_name;
        String doctor_last_name;
        String treatment_type;
        String treatment_timestamp;

        BufferedReader buffReader = new BufferedReader(new FileReader(treatmentDataListLocation));
        String line;

        while ((line = buffReader.readLine()) != null) {
            String[] values = line.split(",", -1);
            patient_last_name = values[0];
            doctor_last_name = values[1];
            treatment_type = values[2];
            treatment_timestamp = values[3];

            treatmentListInsertMethod(patient_last_name,doctor_last_name,treatment_type,treatment_timestamp);
        }
        buffReader.close();
    }

    private void treatmentListInsertMethod(String patient_last_name, String doctor_last_name, String treatment_type, String treatment_timestamp) {
        String sql = "insert into treatment_table(patient_last_name, doctor_last_name, treatment_type, treatment_timestamp) values (?,?,?,?)";
        try (Connection conn = this.connect()) {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,patient_last_name);
            ps.setString(2,doctor_last_name);
            ps.setString(3,treatment_type);
            Timestamp sqlTimeStamp = new java.sql.Timestamp(new SimpleDateFormat("MM-dd-yyyy HH:mm").parse(treatment_timestamp).getTime());
            ps.setTimestamp(4, sqlTimeStamp);
            ps.executeUpdate();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void readPatientDoctorListData(String patientDoctorListLocation) throws IOException {
        String patient_last_name;
        String doctor_last_name;

        BufferedReader buffReader = new BufferedReader(new FileReader(patientDoctorListLocation));
        String line;

        while ((line = buffReader.readLine()) != null) {
            String[] values = line.split(",", -1);
            patient_last_name = values[0];
            doctor_last_name = values[1];

            patientDoctorListInsertMethod(patient_last_name, doctor_last_name);
        }
        buffReader.close();
    }

    private void patientDoctorListInsertMethod(String patient_last_name, String doctor_last_name) {
        String sql = "insert into patient_doctor_list(patient_name, doctor_name) values (?,?)";
        try (Connection conn = this.connect()){
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, patient_last_name);
            ps.setString(2, doctor_last_name);
            ps.executeUpdate();
            ps.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * This method will  read the data from the text file and call our personTableInsertMethod.
     * @param location - file location of *.txt
     * @throws IOException - for file input exception
     */
    private void readPersonTableData(String location) throws IOException {
        // declare all variables for prepared statement
        String person_type;
        String person_first_name;
        String person_last_name;
        String doctor_privilege;
        String patient_id;
        String room_id;
        String emergency_contact_name;
        String emergency_contact_number;
        String insurance_policy_number;
        String insurance_policy_company;
        String patient_doctor_last_name;
        String initial_dx;
        String admission_date;
        String discharge_date;
        // implement buffered reader for text file
        BufferedReader buffReader = new BufferedReader(new FileReader(location));
        String line;

        while ((line = buffReader.readLine()) != null) {
            String[] values = line.split(",",-1);
            person_type = values[0];
            person_first_name = values[1];
            person_last_name = values[2];
            doctor_privilege = values[3];
            patient_id = values[4];
            room_id = values[5];
            emergency_contact_name = values[6];
            emergency_contact_number = values[7];
            insurance_policy_number = values[8];
            insurance_policy_company = values[9];
            patient_doctor_last_name = values[10];
            initial_dx = values[11];
            admission_date = values[12];
            discharge_date = values[13];

            personTableInsertMethod(person_type, person_first_name, person_last_name, doctor_privilege, patient_id,
                    room_id, emergency_contact_name, emergency_contact_number, insurance_policy_number,
                    insurance_policy_company, patient_doctor_last_name, initial_dx, admission_date, discharge_date);
        }
        buffReader.close();
    }

    /**
     * This method will accept arguments from readPersonTableData method and connect to the database and insert
     * values via a prepared statement.
     * @param person_type  - values received from *.txt
     * @param person_first_name  - values received from *.txt
     * @param person_last_name  - values received from *.txt
     * @param doctor_privilege  - values received from *.txt
     * @param patient_id  - values received from *.txt
     * @param room_id  - values received from *.txt
     * @param emergency_contact_name  - values received from *.txt
     * @param emergency_contact_number  - values received from *.txt
     * @param insurance_policy_number  - values received from *.txt
     * @param insurance_policy_company  - values received from *.txt
     * @param patient_doctor_last_name  - values received from *.txt
     * @param initial_dx  - values received from *.txt
     * @param admission_date  - values received from *.txt
     * @param discharge_date  - values received from *.txt
     */
    private void personTableInsertMethod(String person_type, String person_first_name, String person_last_name,
                                         String doctor_privilege, String patient_id, String room_id,
                                         String emergency_contact_name, String emergency_contact_number,
                                         String insurance_policy_number, String insurance_policy_company,
                                         String patient_doctor_last_name, String initial_dx, String admission_date,
                                         String discharge_date) {
        String sql = "insert into person(person_type,person_first_name,person_last_name,doctor_privilege,patient_id," +
                "room_id,emergency_contact_name,emergency_contact_number,insurance_policy_number," +
                "insurance_policy_company,patient_doctor_last_name,initial_dx,admission_date,discharge_date)" +
                " values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

        try(Connection conn = this.connect()) {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,person_type);
            ps.setString(2,person_first_name);
            ps.setString(3,person_last_name);
            ps.setString(4,doctor_privilege);
            try {
                if(patient_id != null) {
                    ps.setInt(5, Integer.parseInt(patient_id));
                }
            } catch (NumberFormatException e) {
                ps.setNull(5, Types.INTEGER);
            }
            try {
                if(patient_id != null) {
                    ps.setInt(6, Integer.parseInt(room_id));
                }
            } catch (NumberFormatException e) {
                ps.setNull(6, Types.INTEGER);
            }
            ps.setString(7,emergency_contact_name);
            try {
                if(emergency_contact_number != null) {
                    ps.setInt(8, Integer.parseInt(emergency_contact_number));
                }
            } catch (NumberFormatException e) {
                //int tempInt = 0;
                ps.setNull(8, Types.INTEGER);
            }
            ps.setString(9,insurance_policy_number);
            ps.setString(10,insurance_policy_company);
            ps.setString(11,patient_doctor_last_name);
            ps.setString(12,initial_dx);
            try {
                if (admission_date != null) {
                    Date inDate = new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse(admission_date).getTime());
                    ps.setDate(13,inDate);
                }
            } catch (DateTimeException | ParseException e) {
                ps.setNull(13, Types.DATE);
            }
            try {
                if (discharge_date != null) {
                    Date disDate = new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse(discharge_date).getTime());
                    ps.setDate(14,disDate);
                }
            } catch (DateTimeException | ParseException e) {
                ps.setNull(14, Types.DATE);
            }
            ps.executeUpdate();
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

        /**
         * Connection method to establish jbdc:sqlite connection.
         */
        private Connection connect () {
            //SQLite connection string
            String url = "jdbc:sqlite:D:/___finalVersion/final.db/hospital.db";
            Connection conn = null;
            try {
                conn = DriverManager.getConnection(url);
                //turn on FOREIGN KEY enforcement
                conn.createStatement().executeUpdate("PRAGMA FOREIGN_KEYS = ON");
            } catch (Exception e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
            return conn;
        }

    /**
     * Test sample selector... testing method.
     */
    private void selectSample(){
        String sql = "SELECT person_first_name, person_last_name FROM person;";
        try (Connection conn = this.connect();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)){
            while (rs.next()) {
                System.out.println(rs.getString("person_first_name") + "\t" +
                        rs.getString("person_last_name") + "\t");
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
	
	
	
	

    /**
     * Table update method. Wil distribute all data from person table to all other tables.
     */
    public void tableUpdate(){
        String patientUpdate = "insert into patient(person_id, patient_id, patient_type)  " +
                "select person_id, patient_id, person_type) from person where person_type = 'I' or person_type = 'O';";
        String outPatientUpdate = "insert into out_patient(patient_id, person_type) " +
                "select patient_id, person_type from person where person_type = 'O'";
        String inPatientUpdate = "insert into in_patient(patient_id, person_type, room_id, emergency_contact_name, " +
                "emergency_contact_number, insurance_policy_number, insurance_policy_company, patient_doctor_last_name, initial_dx," +
                "admission_date, discharge_date) select patient_id, person_type, room_id, emergency_contact_name, " +
                "emergency_contact_number, insurance_policy_number, insurance_policy_company, patient_doctor_last_name," +
                "initial_dx, admission_date, discharge_date from person where person_type = 'I' and " +
                "emergency_contact_name = not null and emergency_contact_number = not null and " +
                "insurance_policy_number = not null and insurance_policy_company = not null and " +
                "patient_doctor_last_name = not null and admission_date = not null;";
		String employeeUpdate = "insert into employee(employee_first_name, employee_last_name, person_id, employee_id) from person " +
				"where person_type = 'V' or person_type = 'D' or person_type = 'A' or person_type = 'N' or person_type = 'T';";
		String volunteerUpdate = "insert into volunteer(person_first_name,person_last_name,person_id) from person " +
				"where person_type = 'V';";
		String volunteerUpdate = "insert into doctor(person_first_name,person_last_name,person_id) from person " +
				"where person_type = 'D';";
		String volunteerUpdate = "insert into administrator(person_first_name,person_last_name,person_id) from person " +
				"where person_type = 'A';";
		String volunteerUpdate = "insert into nurse(person_first_name,person_last_name,person_id) from person " +
				"where person_type = 'N';";
		String volunteerUpdate = "insert into technician(person_first_name,person_last_name,person_id) from person " +
				"where person_type = 'T';";
		
        try (Connection conn = this.connect();
        Statement stmt = conn.createStatement();
       ){
            stmt.executeUpdate(patientUpdate);
            stmt.executeUpdate(outPatientUpdate);
            stmt.executeUpdate(inPatientUpdate);
			stmt.executeUpdate(employee);
            stmt.executeUpdate(volunteer);
            stmt.executeUpdate(doctor);
            stmt.executeUpdate(administrator);
            stmt.executeUpdate(nurse);
            stmt.executeUpdate(technician);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }














}