diff --git a/src/main/java/com/napier/sem/App.java b/src/main/java/com/napier/sem/App.java index a6f5435..e109a0b 100644 --- a/src/main/java/com/napier/sem/App.java +++ b/src/main/java/com/napier/sem/App.java @@ -90,6 +90,9 @@ public static void main(String[] args) ArrayList emp1 = employeeSQL.getAllSalaries(); employeeDisplay.printSalaries(emp1); + DepartmentSQL departmentSQL = new DepartmentSQL(a.con); + + // Disconnect from database a.disconnect(); } diff --git a/src/main/java/com/napier/sem/Department.java b/src/main/java/com/napier/sem/Department.java new file mode 100644 index 0000000..85173a1 --- /dev/null +++ b/src/main/java/com/napier/sem/Department.java @@ -0,0 +1,37 @@ +package com.napier.sem; + +public class Department { + public String dept_name; + public String dept_no; + public Employee manager; + + public Department(String dept_name, String dept_no, Employee manager) { + this.dept_name = dept_name; + this.dept_no = dept_no; + this.manager = manager; + } + + public String getDept_name() { + return dept_name; + } + + public void setDept_name(String dept_name) { + this.dept_name = dept_name; + } + + public String getDept_no() { + return dept_no; + } + + public void setDept_no(String dept_no) { + this.dept_no = dept_no; + } + + public Employee getManager() { + return manager; + } + + public void setManager(Employee manager) { + this.manager = manager; + } +} \ No newline at end of file diff --git a/src/main/java/com/napier/sem/DepartmentSQL.java b/src/main/java/com/napier/sem/DepartmentSQL.java new file mode 100644 index 0000000..ace5ab4 --- /dev/null +++ b/src/main/java/com/napier/sem/DepartmentSQL.java @@ -0,0 +1,44 @@ +package com.napier.sem; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + + +public class DepartmentSQL { + + private Connection con; + + public DepartmentSQL(Connection conn) { + + this.con = conn; + } + + public Department getDepartment(String dept_name) { + + try { + // Prepare SQL statement to prevent SQL injection + String strSelect = "SELECT dept_no, dept_name FROM departments WHERE dept_name = ?"; + PreparedStatement pstmt = con.prepareStatement(strSelect); + pstmt.setString(1, dept_name); + ResultSet rset = pstmt.executeQuery(); + + + if (rset.next()) { + // Here, we fetch department number as a string (like "d005") + String dept_no = rset.getString("dept_no"); + // Assume that fetching the manager is handled separately + Employee manager = null; // Placeholder for manager if needed + String dep_name = rset.getString("dept_name"); + return new Department(dep_name, dept_no, manager); + } else { + return null; + } + } catch (SQLException e) { + System.out.println(e.getMessage()); + System.out.println("Failed to get department details"); + return null; + } + } +} diff --git a/src/main/java/com/napier/sem/EmployeeSQL.java b/src/main/java/com/napier/sem/EmployeeSQL.java index fead920..d9b4fbe 100644 --- a/src/main/java/com/napier/sem/EmployeeSQL.java +++ b/src/main/java/com/napier/sem/EmployeeSQL.java @@ -81,4 +81,37 @@ public ArrayList getAllSalaries() { return null; } } + + public ArrayList getSalariesByDepartment(Department dept) { + ArrayList employees = new ArrayList<>(); + try{ + String strSelect = "SELECT employees.emp_no, employees.first_name, employees.last_name, salaries.salary " + + "FROM employees, salaries, dept_emp, departments " + + "WHERE employees.emp_no = salaries.emp_no " + + "AND employees.emp_no = dept_emp.emp_no " + + "AND dept_emp.dept_no = departments.dept_no " + + "AND salaries.to_date = '9999-01-01' " + + "AND departments.dept_no = '" + dept.getDept_no() + "' " + // Dept no is treated as string + "ORDER BY employees.emp_no ASC"; + + Statement stmt = con.createStatement(); + ResultSet rset = stmt.executeQuery(strSelect); + + while (rset.next()) { + Employee emp = new Employee(); + emp.emp_no = rset.getInt("emp_no"); + emp.first_name = rset.getString("first_name"); + emp.last_name = rset.getString("last_name"); + emp.salary = rset.getInt("salary"); + employees.add(emp); + } + rset.close(); + stmt.close(); + return employees; + } catch (Exception e) { + e.printStackTrace(); + System.out.println("Failed to get salary details"); + return null; + } + } }