Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public List<Employee> getEmployeeSubordinate(int employee_id) throws SQLException {
- List<Employee> employees = new ArrayList<>();
- connection = dataSource.getConnection();
- PreparedStatement statement = connection.prepareStatement("WITH RECURSIVE r AS (\n" +
- " SELECT id, parent_id, name, position\n" +
- " FROM employee\n" +
- " WHERE id = ?\n" +
- " UNION\n" +
- " SELECT employee.id, employee.parent_id, employee.name , employee.position\n" +
- " FROM employee\n" +
- " JOIN r\n" +
- " ON employee.parent_id = r.id\n" +
- ")\n" +
- "SELECT * FROM r;");
- int parent_id = employee_id;
- statement.setInt(1, parent_id);
- statement.executeQuery();
- ResultSet resultSet = statement.getResultSet();
- while (resultSet.next()) {
- employees.add(new Employee(resultSet.getInt("id"),
- resultSet.getString("name"),
- resultSet.getString("position"),
- resultSet.getInt("parent_id")));
- }
- return employees;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement