Newer
Older
package com.example.scheduler_server;
Rafi Zereselasie (raz070)
committed
import java.sql.*;
Rafi Zereselasie (raz070)
committed
/*
Name: Staff
Description: Responsible for sending queries and receiving responses from the database that deal with the
Rafi Zereselasie (raz070)
committed
Employee table.
*/
Rafi Zereselasie (raz070)
committed
// Object that facilitates the connection to the database
Rafi Zereselasie (raz070)
committed
private final Connection dbConnection;
Rafi Zereselasie (raz070)
committed
Rafi Zereselasie (raz070)
committed
Name: Staff Constructor
Parameters: None
Description: Establishes connections to the local database. If running on a different computer
make sure that the port number (replace '3306'), database name (replace 'ScheduleApp'), user, and password are
set correctly.
Return: Staff Object
protected Staff(Connection c) {
this.dbConnection = c;
Rafi Zereselasie (raz070)
committed
Name: addEmployee
Parameters:
String firstName: First name of the employee,
String lastName: Last name of the employee .
Description: Adds an employee by querying the database. Also, assigns a unique ID.
Rafi Zereselasie (raz070)
committed
Return:
String newEmployee: If the employee was added successfully, then it returns their new details, else returns an empty string.
protected String addEmployee(String firstName, String lastName, String isManager, String email, String phoneNumber,
float wage){
Rafi Zereselasie (raz070)
committed
System.out.println(firstName + " " + lastName + " " + isManager + " " + email + " " + phoneNumber + " " + wage);
Rafi Zereselasie (raz070)
committed
String newEmployee = "";
Rafi Zereselasie (raz070)
committed
try {
Rafi Zereselasie (raz070)
committed
Statement myStatement = dbConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
myStatement.executeUpdate("insert into Employees (first_name,last_name,isManager,email,phoneNumber,wage)" +
" values ('" + firstName + "','" + lastName + "'," + isManager + ",'" + email + "','" +
phoneNumber + "'," + wage + ")");
Rafi Zereselasie (raz070)
committed
ResultSet myRs = myStatement.executeQuery("select * from Employees where first_name='" + firstName
+ "' and last_name='" + lastName + "'");
Rafi Zereselasie (raz070)
committed
if (myRs.last()) {
newEmployee = myRs.getString("employee_ID") + "," + myRs.getString("first_name") +
"," + myRs.getString("last_name") + "," + myRs.getString("isManager") +
"," + myRs.getString("email") + "," + myRs.getString("phoneNumber") +
Rafi Zereselasie (raz070)
committed
"," + myRs.getString("wage") + ", ";
Rafi Zereselasie (raz070)
committed
}
} catch (Exception exception) {
exception.printStackTrace();
return newEmployee;
Rafi Zereselasie (raz070)
committed
System.out.println("Employee Added");
Rafi Zereselasie (raz070)
committed
return newEmployee;
Rafi Zereselasie (raz070)
committed
/*
Name: removeEmployee
Parameters:
int ID: The employees unique ID.
Description: Removes the employee with the same ID by querying the database.
Return: none
*/
Rafi Zereselasie (raz070)
committed
protected int removeEmployee(int ID) {
Rafi Zereselasie (raz070)
committed
try {
Statement myStatement = dbConnection.createStatement();
myStatement.executeUpdate("delete from Employees where employee_id = " + ID);
Rafi Zereselasie (raz070)
committed
} catch (Exception exception) {
exception.printStackTrace();
return -1;
Rafi Zereselasie (raz070)
committed
System.out.println("Employee Removed");
Rafi Zereselasie (raz070)
committed
return ID;
Rafi Zereselasie (raz070)
committed
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
protected String addPosition(int ID, String position) {
try {
System.out.println(ID + " " + position);
Statement myStatement = dbConnection.createStatement();
ResultSet myRs = myStatement.executeQuery("select positions from Employees where employee_ID=" + ID);
myRs.next();
if (myRs.getString("positions").isEmpty()) {
myStatement.executeUpdate("update Employees set positions='" + position + "' where employee_ID = " + ID);
} else {
myStatement.executeUpdate("update Employees set positions=concat(positions,'." + position + "') where employee_ID = " + ID);
}
} catch (Exception exception) {
exception.printStackTrace();
return "";
}
return ID + "/" + position;
}
protected String removePosition(int ID, String position) {
String removedPosition = "";
StringBuilder newPositions = new StringBuilder();
try {
Statement myStatement = dbConnection.createStatement();
ResultSet myRs = myStatement.executeQuery("select positions from Employees where employee_ID=" + ID);
myRs.next();
for (String s : myRs.getString("positions").split("\\.")) {
if (!s.equalsIgnoreCase(position)) {
if (newPositions.isEmpty()) {
newPositions.append(s);
} else {
newPositions.append(".").append(s);
}
} else {
removedPosition = ID + "/" + position;
}
}
myStatement.executeUpdate("update Employees set positions='" + newPositions + "' where employee_ID = " + ID);
} catch (Exception exception) {
exception.printStackTrace();
return removedPosition;
}
return removedPosition;
}
Rafi Zereselasie (raz070)
committed
Name: printEmployees
Parameters: none
Description: prints the id, and names of each employee
Return: none
protected String allEmployees(){
StringBuilder response = new StringBuilder("allEmployees");
Rafi Zereselasie (raz070)
committed
try {
Statement myStatement = dbConnection.createStatement();
ResultSet myRs = myStatement.executeQuery("select * from Employees");
while (myRs.next()) {
response.append("/").append(myRs.getString("employee_ID")).append(",")
.append(myRs.getString("first_name")).append(",")
.append(myRs.getString("last_name")).append(",")
.append(myRs.getString("isManager")).append(",")
.append(myRs.getString("email")).append(",")
.append(myRs.getString("phoneNumber")).append(",")
Rafi Zereselasie (raz070)
committed
.append(myRs.getString("wage")).append(",")
.append(myRs.getString("positions"));
Rafi Zereselasie (raz070)
committed
} catch (Exception exception) {
exception.printStackTrace();
return "Error retrieving employees from database";
Rafi Zereselasie (raz070)
committed
return response.toString();