Newer
Older
Rafi Zereselasie (raz070)
committed
/*
This class facilitates the communication between the model and the mysql database, for staff purposes.
*/
Rafi Zereselasie (raz070)
committed
import java.sql.*;
Rafi Zereselasie (raz070)
committed
// Object that facilitates the connection to the database
private Connection dbConnection;
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.
Return: none
Rafi Zereselasie (raz070)
committed
protected void addEmployee(String firstName, String lastName){
try {
Statement myStatement = dbConnection.createStatement();
myStatement.executeUpdate("insert into Employees (first_name,last_name) values ('" + firstName + "', '" + lastName + "')");
} catch (Exception e) {
System.out.println(e.fillInStackTrace());
Rafi Zereselasie (raz070)
committed
System.out.println("Employee Added");
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
*/
protected void removeEmployee(int ID){
try {
Statement myStatement = dbConnection.createStatement();
myStatement.executeUpdate("DELETE FROM shifts WHERE (employee_ID =" + ID + ")");
Rafi Zereselasie (raz070)
committed
myStatement.executeUpdate("delete from Employees where employee_id = " + ID);
} catch (Exception e) {
System.out.println(e.fillInStackTrace());
Rafi Zereselasie (raz070)
committed
System.out.println("Employee Removed");
Rafi Zereselasie (raz070)
committed
Name: printEmployees
Parameters: none
Description: prints the id, and names of each employee
Return: none
protected String getEmployees(){
String response = "allEmployees";
Rafi Zereselasie (raz070)
committed
try {
Statement myStatement = dbConnection.createStatement();
ResultSet myRs = myStatement.executeQuery("select * from Employees");
while (myRs.next()) {
response = response + "/" + myRs.getString("employee_ID") + "." + myRs.getString("first_name") + "." + myRs.getString("last_name");;
Rafi Zereselasie (raz070)
committed
} catch (Exception e) {
System.out.println(e.fillInStackTrace());
return "fail";
return response;