LDAP: Add an entry using LDIF and the UnboundID LDAP SDK


  1. Add Example (Java)
  2. See Also

The following example uses the standard edition of the UnboundID LDAP SDK to add an entry using LDIF. The standard edition of the LDAP SDK is free and the source code is supplied.

Using the LDAP ADD request:

  • The distinguished name must not already exist
  • The immediate superior of the distinguished name must exist
  • The server will not dereference aliases


Example Code


package samplecode.add;


import com.unboundid.ldap.sdk.AddRequest;
import com.unboundid.ldap.sdk.LDAPConnection;
import com.unboundid.ldap.sdk.LDAPConnectionOptions;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldif.LDIFException;


/** demonstrate the {@link AddRequest} */
public final class AddExample
{

  /**
   * demonstrate the {@link AddRequest}
   * 
   * @throws LDIFException
   * @throws LDAPException
   */
  public static void main(final String... args) throws LDAPException,LDIFException
  {
    /*
     * Use connection options to specify that the connection attempt
     * should be 1 second and if the ADD request times out, the request
     * should be abandoned
     */
    final LDAPConnectionOptions connectionOptions = 
       new LDAPConnectionOptions();
    connectionOptions.setAbandonOnTimeout(true);
    int connectionTimeoutMillis = 1000;
    connectionOptions.setConnectTimeoutMillis(connectionTimeoutMillis);

    /*
     * LDIF lines for the add request
     */
    final String[] ldifLines =
            {
                    "dn: uid=user,dc=example,dc=com",
                    "changetype: add",
                    "cn: Joe User",
                    "sn: User",
                    "uid: user",
                    "userPassword: password"
            };

    final String host = "ldap.example.com";
    final int port = 389;

    LDAPConnection ldapConnection = 
	   new LDAPConnection(connectionOptions,host,port);
    ldapConnection.add(new AddRequest(ldifLines));
    ldapConnection.close();
  }
}


See Also

About Terry Gardner

Terry Gardner was a leading directory services architect with experience with many large scale directory services installations and messaging server installations, and was a Subject Matter Expert in the field of Directory Services and Solaris (operating system) performance. Mr. Gardner also participated in the open-source software community. Mr. Gardner passed away in December, 2013.
This entry was posted in computing, Java, LDAP, UnboundID LDAP SDK and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s