Skip to content
Snippets Groups Projects
Staff.java 2.93 KiB
Newer Older
/*
This class facilitates the communication between the model and the mysql database, for staff purposes.
*/

eyan_'s avatar
eyan_ committed
package com.example.schedulerapp;

eyan_'s avatar
eyan_ committed
public class Staff {

    // Object that facilitates the connection to the database
    private Connection dbConnection;

    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 String connectDataBase(){
        try {
            dbConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ScheduleApp", "root", "password");
        } catch (Exception e) {
            System.out.println(e.fillInStackTrace());
            return "Couldn't connect to database.";
        return "Successfully connected to database.";
    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
    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());
    /*
    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 Employees where employee_id = " + ID);
        } catch (Exception e) {
            System.out.println(e.fillInStackTrace());
    Name: printEmployees
    Parameters: none
    Description: prints the id, and names of each employee
    Return: none
    protected String printEmployees(){
        try {
            Statement myStatement = dbConnection.createStatement();
            ResultSet myRs = myStatement.executeQuery("select * from Employees");
            while (myRs.next()) {
                System.out.println("id: " + myRs.getString("employee_ID") + ", " + myRs.getString("last_name") + ", " + myRs.getString("first_name"));
        } catch (Exception e) {
            System.out.println(e.fillInStackTrace());
            return "fail";
eyan_'s avatar
eyan_ committed
}