0
  • I am able to get the result while executing the following code but i want to store member of group in string array but i am not do it. I am new to java can some one help me.
  • the following code

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

    is able to give me group member but i want to store it in String array.

Thanks in advance.

  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) {
                  }
              }
          }
      }
  }
Smit
  • 4,685
  • 1
  • 24
  • 28
  • Do you want to store it in a specific String array? Have you declared this String array? Have you tried something and gotten an error message, or are you attempting to have some effect that you do not know how to do (and have not told us)? You have not given enough information for us to know how to help you. – arcy Jul 18 '13 at 18:25
  • I dont understand as you can access that `attribute` then where is the problem you are facing while storing it in `String array`? – Smit Jul 18 '13 at 18:27

1 Answers1

0

If you just want to store an array of Strings without knowing the number of Strings to store use an ArrayList.

//create the arraylist
ArrayList<String> memberNames = new ArrayList<String>();
// add the memeber name to the array
memberNames.add(attributes.get("member"));
ChadNC
  • 2,528
  • 4
  • 25
  • 39
  • i have tried the following code but i am getting following error The method add(String) in the type ArrayList is not applicable for the arguments (Attribute) – user1241042 Jul 19 '13 at 05:01
  • The call to attributes.get("member") must not return a String. Does it? – ChadNC Jul 19 '13 at 11:08