Adding an entry involves four steps:
Create the attributes of the entry and add them to an attribute set.
Specify the DN of the entry to be created.
Create an LDAPEntry object with the DN and the attribute set.
Call the LDAPConnection add method to add it to the directory.
The following example demonstrates these procedures. For a complete sample, see AddEntry.java in the Sample Code.
Initially, attributes are created for the entry and the attributes are then added to an LDAPAttributeSet object (step 1):
LDAPAttribute attribute = null;
String cn_values[] = {"James Smith", "Jim Smith", "Jimmy Smith"};
attribute = new LDAPAttribute("cn", cn_values);
attributeSet.add(attribute);
At this point, any number of attributes may be created and added to the attribute set. This example adds only one attribute for brevity.
When the attribute set is created, the DN of the entry must be specified (step 2), and an LDAPEntry object must be created using the DN we just specified (step 3):
String dn = "cn=JSmith," + containerName; LDAPEntry newEntry = new LDAPEntry(dn, attributeSet);
Now our application needs to connect to the LDAP server and call LDAPConnection.add to add the entry (step 4):
lc.connect(ldapHost, ldapPort); lc.bind(ldapVersion, loginDN, password); lc.add(newEntry);
IMPORTANT:Entries have required attributes. These must be included in the LDAPAttributeSet in order for the add to succeed.