如何在字符串数组中存储组成员

时间:2013-07-18 18:21:35

标签: java sdk ldap unboundid-ldap-sdk

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

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

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

提前致谢。

  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) {
                  }
              }
          }
      }
  }

1 个答案:

答案 0 :(得分:0)

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

//create the arraylist
ArrayList<String> memberNames = new ArrayList<String>();
// add the memeber name to the array
memberNames.add(attributes.get("member"));