0
  • 我能够在执行以下代码时获得结果,但我想将组的成员存储在字符串数组中,但我没有这样做。我是java新手,有人可以帮助我。
  • 以下代码

    System.out.println(" Person Common Name = " + attributes.get("member")+"\t");

    能够给我组成员,但我想将它存储在字符串数组中。

提前致谢。

  package com.ankit.ldap;

  import javax.naming.Context;
  import javax.naming.NamingEnumeration;
  import javax.naming.directory.*;
  import java.util.Hashtable;

  /**
   * User: gmc
   * Date: 16/02/11
   */
  public class SimpleQuery {

      public static void main(String[] args) {
          String userName = "xxxxxxxxxxxxxxxxx";
          Hashtable env = new Hashtable();
          env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
          env.put(Context.PROVIDER_URL, "xxxxxxxxxxxxxxx");
          env.put(Context.SECURITY_AUTHENTICATION, "simple");
          env.put(Context.SECURITY_PRINCIPAL, new String(userName));
          env.put(Context.SECURITY_CREDENTIALS, "xxxxxx");

          DirContext ctx = null;
          NamingEnumeration results = null;
          try {
              ctx = new InitialDirContext(env);
              SearchControls controls = new SearchControls();
              controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
              results = ctx.search("", "(cn=CompetencyGroup)", controls);
              while (results.hasMore()) {
                  SearchResult searchResult = (SearchResult) results.next();
                  Attributes attributes = searchResult.getAttributes();
                  Attribute attr = attributes.get("cn");
                  String cn = (String) attr.get();
                    System.out.println(" Person Common Name = " + attributes.get("member")+"\t");
                    System.out.println(" Person Display Name = " + attributes.get("displayName"));

                    System.out.println(" Person MemberOf = " + attributes.get("memberOf"));
              }
          } catch (Throwable e) {
              e.printStackTrace();
          } finally {
              if (results != null) {
                  try {
                      results.close();
                  } catch (Exception e) {
                  }
              }
              if (ctx != null) {
                  try {
                      ctx.close();
                  } catch (Exception e) {
                  }
              }
          }
      }
  }
4

1 回答 1

0

如果您只想存储字符串数组而不知道要存储的字符串数量,请使用 ArrayList。

//create the arraylist
ArrayList<String> memberNames = new ArrayList<String>();
// add the memeber name to the array
memberNames.add(attributes.get("member"));
于 2013-07-18T18:27:26.780 回答