-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNurse.java
More file actions
executable file
·217 lines (194 loc) · 7.7 KB
/
Nurse.java
File metadata and controls
executable file
·217 lines (194 loc) · 7.7 KB
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
import java.sql.*;
import java.util.*;
public class Nurse extends DataRecord
{
/**
* Takes as a parameter a string that contains all the info of a Nurse and populates a Nurse instance appropriately
*/
public Nurse(String s)
{
//Just call the super constructor to set info to be the supplied string
super(s);
}
/**
* Performs an update in the Nurse table using the information provided in an info string
*/
public void update(String s)
{
try
{
String[] values = s.split("\t");
if(values.length == 3)
{
String query = "UPDATE Nurse SET Fname=?,Minit=?,Lname=? WHERE Nssn = (?)";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1,values[0]);
pstmt.setString(2,values[1]);
pstmt.setString(3,values[2]);
pstmt.setString(4,info);
int affectedRows = pstmt.executeUpdate();
if(affectedRows == 0)
{ System.err.println("Failed to update to Nurse info.");}
else
{ System.out.println("\tSuccessfully updated Nurse info. \n");}
pstmt.close();
}
}
catch (SQLException e) {
System.err.println(e);
e.printStackTrace();
}
}
/**
* Inserts a new Nurse into the database using the info contained in the string
*/
public static void insert(String s)
{
try
{
String Template = "INSERT INTO Nurse (Nssn,Fname,Minit,Lname,supervisor) VALUES (?,?,?,?,?)";
String[] values = s.split("\t");
if(values.length == 5)
{
PreparedStatement pstmt = conn.prepareStatement(Template);
pstmt.setString(1,values[0]);
pstmt.setString(2,values[1]);
pstmt.setString(3,values[2]);
pstmt.setString(4,values[3]);
pstmt.setString(5,values[4]);
int affectedRows = pstmt.executeUpdate();
if(affectedRows == 0)
{ System.err.println("Failed to insert to Nurses table.");}
else
{ System.out.println("\n Successfully inserted new Nurse. \n");}
pstmt.close();
}
}
catch (SQLException e) {
System.err.println(e);
e.printStackTrace();
}
}
/**
* Deletes a Nurse from the database where the string parameter is the primary key of the nurse to be deleted
*/
public void delete(String s)
{
String delete = "delete from NURSE where Nssn = " + s;
try
{
Statement stmt = conn.createStatement();
stmt.executeUpdate(delete);
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* Searches the database to get information about a particular Nurse, including name and supervisor.
*/
private String searchNurse()
{
String result = "";
try
{
String query = "SELECT * FROM Nurse WHERE Nssn = ?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, info);
ResultSet rs = ps.executeQuery();
rs.next();
String nurseInfo = "Nurse: " + rs.getString("Fname") + " " + rs.getString("Minit") + " " + rs.getString("Lname")+ "\n";
String supervisorSsn = rs.getString("supervisor");
String supervisorQuery = "SELECT * FROM Doctor WHERE Dssn = ?";
PreparedStatement ps2 = conn.prepareStatement(supervisorQuery);
ps2.setString(1,supervisorSsn);
ResultSet rs2 = ps2.executeQuery();
rs2.next();
String supervisorInfo = "Supervisor: " + rs2.getString("Fname") + " " + rs2.getString("Minit") + " " + rs2.getString("Lname") + "\n";
result = nurseInfo + supervisorInfo;
rs.close();
rs2.close();
ps.close();
}
catch (SQLException e) {
System.err.println(e);
e.printStackTrace();
}
return result;
}
/**
* Searches the database to get information about a particular Nurse's scheduled procedures.
*/
private String searchProcedure()
{
String result = "";
try
{
String query = "SELECT * FROM Procedure WHERE Nssn = ?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, info);
ResultSet rs = ps.executeQuery();
String queryP = "SELECT Fname,Minit,Lname FROM Patient WHERE Pssn = ?";
PreparedStatement psP = conn.prepareStatement(queryP);
String queryD = "SELECT Fname, Minit, Lname FROM Doctor WHERE Dssn = ?";
PreparedStatement psD = conn.prepareStatement(queryD);
if(!rs.isBeforeFirst())
{
result = "No procedures reported for this nurse";
}
else
{
rs.next();
while(!rs.isAfterLast())
{
// Get the name of the patient undergoing the current procedure
psP.setString(1, rs.getString("Pssn"));
ResultSet p = psP.executeQuery();
p.next();
String patientName = p.getString("Fname") + " " + p.getString("Minit") + " " + p.getString("Lname");
//Get the name of the Dr involved in the procedure
psD.setString(1, rs.getString("Dssn"));
ResultSet d = psD.executeQuery();
d.next();
String docName = d.getString("Fname") + " " + d.getString("Minit") + " " + d.getString("Lname");
String procedure = "Procedure Type: " + rs.getString("procedureDescription") + "\n"
+ "Date/Time: " + rs.getDate("scheduled_Date") + " "+ rs.getInt("scheduled_Time") + ":00"+ "\n"
+ "Supervising Doctor: " + docName + "\n"
+ "Patient: " + patientName + "\n\n";
result = result + procedure;
rs.next();
p.close();
d.close();
}
}
rs.close();
ps.close();
psP.close();
psD.close();
}
catch (SQLException e) {
System.err.println(e);
e.printStackTrace();
}
return result;
}
/**
* Searches the database to get information about a particular Nurse, info will contain the primary key of NURSE
* in this case. It will return a string containing the information requested in the search.
*/
public String search(String table)
{
String result = "";
//Construct a select query using the appropriate key and table to search
if(table.equals("NURSE")) //If you're searching the Nurse table using the Nssn then you'll return all the info about the requested Nurse
{
result = searchNurse();
}
else if(table.equals("PROCEDURE")) //If you're searching the Procedure table using the Nssn then you'll return info about
{ //the procedures that the nurse is assisting with
result = searchProcedure();
}
return result;
}
}