Advertisement
Marat-KZN

Untitled

Jan 23rd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. public List<Employee> getEmployeeSubordinate(int employee_id) throws SQLException {
  2.         List<Employee> employees = new ArrayList<>();
  3.         connection = dataSource.getConnection();
  4.         PreparedStatement statement = connection.prepareStatement("WITH RECURSIVE r AS (\n" +
  5.                 "  SELECT id, parent_id, name, position\n" +
  6.                 "  FROM employee\n" +
  7.                 "  WHERE id = ?\n" +
  8.                 "  UNION\n" +
  9.                 "  SELECT employee.id, employee.parent_id, employee.name , employee.position\n" +
  10.                 "  FROM employee\n" +
  11.                 "    JOIN r\n" +
  12.                 "      ON employee.parent_id = r.id\n" +
  13.                 ")\n" +
  14.                 "SELECT * FROM r;");
  15.         int parent_id = employee_id;
  16.         statement.setInt(1, parent_id);
  17.         statement.executeQuery();
  18.         ResultSet resultSet = statement.getResultSet();
  19.         while (resultSet.next()) {
  20.             employees.add(new Employee(resultSet.getInt("id"),
  21.                     resultSet.getString("name"),
  22.                     resultSet.getString("position"),
  23.                     resultSet.getInt("parent_id")));
  24.         }
  25.         return employees;
  26.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement