Java Tutorial/J2EE Application/Attributes

Материал из Java эксперт
Перейти к: навигация, поиск

Add an e-mail address and an empty website address attribute

import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class Modify1Example {
  public static void main(String args[]) {
    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");
    try {
      DirContext dctx = new InitialDirContext(env);
      Attributes attrs = new BasicAttributes(true);
      attrs.put(new BasicAttribute("email", "name@site.ru"));
      attrs.put(new BasicAttribute("website"));
      dctx.modifyAttributes("cn=Name, ou=People", DirContext.ADD_ATTRIBUTE, attrs);
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}