Update attribute in LDAP using JNDI

So far in  we have discussed the fundamental concepts of JNDI. We discussed the naming service with an example. In the just  previous discussion we have seen how to access LDAP  from  Java with JNDI. In this chapter we are going to discuss how to update attribute in LDAP using JNDI . LDAP support is inbuilt in JDK . So no need to use any third party API in our application.

Setting up LDAP server and directory entries

Before doing any attribute update in directory , we should have LDAP server running. We are using Apache Directory Studio for creating and managing LDAP related things. The procedure is explained in the previous chapter. Please  do the steps from 1 to 15.(Click here to visit the page) .

Update attribute in LDAP using JNDI

Here we are updating the attribute value of ‘phoneNumber’.The Java code is shown below.

DirectoryUpdateSample.java

import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.ModificationItem;
public class DirectoryUpdateSample {

public DirectoryUpdateSample() {

}
public void updateEntry() {
Properties properties = new Properties();

properties.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
properties.put(Context.PROVIDER_URL, "ldap://localhost:10389");
try {
DirContext context = new InitialDirContext(properties);
Attributes attrs = context
.getAttributes("employeeNumber=112233,ou=users,ou=system");
System.out.println("Before update..");
displayAttributes(attrs);
System.out.println("Updating..");
Attribute attribute = new BasicAttribute("telephoneNumber",
"88888888");
ModificationItem[] item = new ModificationItem[1];
item[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
attribute);
context.modifyAttributes(
"employeeNumber=112233,ou=users,ou=system", item);
System.out.println("After update ..");
Attributes attrs1 = context
.getAttributes("employeeNumber=112233,ou=users,ou=system");
displayAttributes(attrs1);
} catch (NamingException e) {
e.printStackTrace();
}
}
public void displayAttributes(Attributes attributes) {
try {
System.out.println("Surname: " + attributes.get("sn").get());
System.out.println("Common name : " + attributes.get("cn").get());
System.out.println("Telephone number : "
+ attributes.get("telephoneNumber").get());
} catch (NamingException e) {

e.printStackTrace();
}
}
public static void main(String[] args) {
DirectoryUpdateSample sample = new DirectoryUpdateSample();
sample.updateEntry();
}

}

The first argument while creating the ModificationItem object is important in this example. For updating an existing attribute entry , the value should be 2.

Output

Compile and run the above class. The output  is shown below.

Before update..

Surname: Bijoy

Common name : Bijoy

Telephone number : 09999999999

Updating..

After update ..

Surname: Bijoy

Common name : Bijoy

Telephone number : 88888888

So the attribute value ‘telephoneNumber’ is successfully updated in LDAP.

See also:

Overview to JNDI

JNDI example -Naming service

Accessing LDAP

Adding LDAP attribute to an entry

Deleting attribute from LDAP entry

Adding LDAP entry using JNDI

Deleting LDAP entry using JNDI

Displaying user entries in LDAP using JNDI