Retrieving a set of attributes using a SearchControls : SearchControls « J2EE Application « Java Tutorial






import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class SearchControlsExample {
  public static void main(String[] argc) throws Exception {
    Hashtable env = new Hashtable(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://MyHost/o=JNDIExample");

    DirContext dctx = new InitialDirContext(env);

    String filter = "(&(cn=S*)(account>1005))";

    String[] attrIDs = { "cn", "email" };
    SearchControls sc = new SearchControls();
    sc.setReturningAttributes(attrIDs);

    NamingEnumeration result = dctx.search("ou=People", filter, sc);

    while (result.hasMore()) {
      SearchResult sr = (SearchResult) result.next();
      System.out.println("Result = " + sr.getName());
    }

  }
}








32.5.SearchControls
32.5.1.Retrieving a set of attributes using a SearchControls
32.5.2.Control the Scope of a Search