6.4. Sending a Search Request

As shown in the following excerpt with a synchronous connection, you get a Connection to the directory server from an LDAPConnectionFactory.

final LDAPConnectionFactory factory = new LDAPConnectionFactory(host,
        port);
Connection connection = null;
try {
    connection = factory.getConnection();

    // Do something with the connection...
} catch (Exception e) {
    // Handle exceptions...
} finally {
    if (connection != null) {
        connection.close();
    }
}

The Connection gives you search() methods that either take parameters in the style of the ldapsearch command, or that take a SearchRequest object. If you are sure that the search only returns a single entry, you can read the entry with the searchSingleEntry() methods. If you have the distinguished name, you can use readEntry() directly.

For a complete example in context, see Search.java, one of the OpenDJ LDAP SDK examples.