public ArrayList<Account> getAccountList() {
    Connection connect = null;

    try {
      ArrayList<Account> accounts = new ArrayList<>();
      connect = DBConnection.getConnection();

      String sql = "SELECT * FROM account" + "ORDER BY name desc";

      ResultSet result = Utility.queryOperation(connect, sql);
      Account temp = new Account();
      while (result.next()) {
        temp.setName(result.getString("name"));
        temp.setEmail(result.getString("email"));
        temp.setSecondaryEmail(result.getString("secondaryEmail"));
        temp.setTypeAccount(result.getString("typeAccount"));
        temp.setPassword(result.getString("password"));
        temp.setAdmin(result.getBoolean("isAdministrator"));
        accounts.add(temp);
      }
      return accounts;

    } catch (SQLException ex) {
      Logger.getLogger(AccountManager.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
      DBConnection.releaseConnection(connect);
    }
    return null;
  }
  public void updateProfile(String key, Account pAccount)
      throws SQLException, ConnectionException, MissingDataException, NullAccountException,
          ProfileException, PasswordException, EmailException {
    try (Connection connect = DBConnection.getConnection()) {
      pAccount = testAccount(pAccount);

      String sql =
          "UPDATE account"
              + "set name = '"
              + Utility.Replace(testProfileData(pAccount.getName()))
              + "', surname = '"
              + Utility.Replace(testProfileData(pAccount.getSurname()))
              + "', password = '******', secondaryEmail = '"
              + testEmail(pAccount.getSecondaryEmail())
              + "WHERE email = '"
              + key
              + "'";

      String sql2 = "UPDATE " + pAccount.getTypeAccount();

      if (pAccount instanceof PhdStudent) {
        sql2 +=
            " set telephone = '"
                + testProfileData(((PhdStudent) pAccount).getTelephone())
                + "', link =  '"
                + testProfileData(((PhdStudent) pAccount).getLink())
                + "', deparment = '"
                + testProfileData(((PhdStudent) pAccount).getDepartment())
                + "', researchInterest = '"
                + testProfileData(((PhdStudent) pAccount).getResearchInterest())
                + "' WHERE fkAccount = '"
                + testProfileData(((PhdStudent) pAccount).getSecondaryEmail());
      }

      if (pAccount instanceof Professor) {
        sql2 +=
            " set link = '"
                + ((Professor) pAccount).getLink()
                + "', set department = '"
                + ((Professor) pAccount).getDepartment()
                + "' WHERE fkAccount = '"
                + ((Professor) pAccount).getSecondaryEmail()
                + "'";
      }

      if (pAccount.getTypeAccount().equals("basic")) // aggiorna solo info base
      Utility.executeOperation(connect, sql);
      else {
        Utility.executeOperation(connect, sql);
        Utility.executeOperation(connect, sql2);
      }

      connect.commit();
    }
  }
  public ArrayList<Account> searchUser(String search, String type) throws SQLException {
    Connection connect = null;
    ArrayList<Account> accounts;

    String sql =
        "SELECT * from account WHERE "
            + "name LIKE '%"
            + search
            + "%'"
            + "AND typeAccount = '"
            + type
            + "'";
    String sql2 = "SELECT * from account WHERE " + "typeAccount ='" + type + "'";
    try {
      connect = DBConnection.getConnection();
      accounts = new ArrayList<>();
      if (search.isEmpty()) {
        ResultSet result = Utility.queryOperation(connect, sql2);
        Account temp = new Account();
        while (result.next()) {
          temp.setName(result.getString("name"));
          temp.setEmail(result.getString("email"));
          temp.setSecondaryEmail(result.getString("secondaryEmail"));
          temp.setTypeAccount(result.getString("typeAccount"));
          temp.setPassword(result.getString("password"));
          temp.setAdmin(result.getBoolean("isAdministrator"));
          accounts.add(temp);
        }
      } else {
        ResultSet result = Utility.queryOperation(connect, sql);
        Account temp = new Account();
        while (result.next()) {
          temp.setName(result.getString("name"));
          temp.setEmail(result.getString("email"));
          temp.setSecondaryEmail(result.getString("secondaryEmail"));
          temp.setTypeAccount(result.getString("typeAccount"));
          temp.setPassword(result.getString("password"));
          temp.setAdmin(result.getBoolean("isAdministrator"));
          accounts.add(temp);
        }
      }

    } finally {
      DBConnection.releaseConnection(connect);
    }
    return accounts;
  }
  public void changeType(Account pAccount, String newType)
      throws SQLException, ConnectionException, NullAccountException, EmailException {
    String demotionSql =
        "DELETE FROM " // cancella vecchie info
            + pAccount.getTypeAccount()
            + "WHERE fkAccount = '"
            + testEmail(pAccount.getSecondaryEmail())
            + "'";

    String toProfessorSql =
        "INSERT INTO professor " // se nuovo professor
            + "(fkAccount,link,department)"
            + "VALUES ('"
            + testEmail(pAccount.getSecondaryEmail())
            + "',"
            + "'"
            + "null"
            + "',"
            + "'"
            + "null"
            + "'";

    String toPhdSql =
        "INSERT INTO phdstudent "
            + "(fkAccount,telephone,link,deparment,researchInterest,fkCycle"
            + "fkCurriculum, fkProfessor )" // nuovo dottorando
            + "VALUES ('"
            + testEmail(pAccount.getSecondaryEmail())
            + "',"
            + "'"
            + "null"
            + "',"
            + "'"
            + "null"
            + "',"
            + "'"
            + "null"
            + "',"
            + "'"
            + "null"
            + "',"
            + "'"
            + "null"
            + "',"
            + "'"
            + "null"
            + "',"
            + "'"
            + "null"
            + "'";

    String changeTypeSql =
        "UPDATE account" // aggiorna il tipo
            + "set typeAccount = '"
            + newType
            + "' WHERE email = '"
            + pAccount.getEmail();

    Connection connect = null;
    try {
      connect = DBConnection.getConnection();
      pAccount = testAccount(pAccount);

      if (newType.equals("phdstudent") && pAccount.getTypeAccount().equals("basic")) {
        Utility.executeOperation(connect, toPhdSql); // diventa un dottorando
        Utility.executeOperation(connect, changeTypeSql); // cambia tipo in account
      } else if (newType.equals("phdstudent") && pAccount.getTypeAccount().equals("professor")) {
        Utility.executeOperation(connect, demotionSql); // perde info phd
        Utility.executeOperation(connect, toPhdSql); // nuove info prof
        Utility.executeOperation(connect, changeTypeSql);
      } else if (newType.equals("professor") && pAccount.getTypeAccount().equals("basic")) {
        Utility.executeOperation(connect, toProfessorSql);
        Utility.executeOperation(connect, changeTypeSql);
      } else if (newType.equals("professor") && pAccount.getTypeAccount().equals("phdstudent")) {
        Utility.executeOperation(connect, demotionSql);
        Utility.executeOperation(connect, toProfessorSql);
        Utility.executeOperation(connect, changeTypeSql);
      } else if (newType.equals("basic")) {
        Utility.executeOperation(connect, demotionSql);
        Utility.executeOperation(connect, changeTypeSql);
      }

    } finally {
      DBConnection.releaseConnection(connect);
    }
  }