Modify2Example.java Source code

Java tutorial

Introduction

Here is the source code for Modify2Example.java

Source

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.ModificationItem;

public class Modify2Example {
    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);

            ModificationItem[] mods = new ModificationItem[3];
            mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("department", "sales"));
            mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("quota", "$1"));
            mods[2] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute("assistant"));

            dctx.modifyAttributes("cn=Name, ou=People", mods);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}