This is a simple LDAP program in java. Here we are using Netscape Directory SDK.To know more about Mozilla Netscape click here. To run this program you need to download ldapsdk.jar For Example ldapsdk-4.1.jar
To download ldapsdk-4.1.jar, click here. Provide appropriate data and see the output.
/* TestLDAP.java */
import java.util.Enumeration;
import netscape.ldap.LDAPAttribute;
import netscape.ldap.LDAPAttributeSet;
import netscape.ldap.LDAPConnection;
import netscape.ldap.LDAPEntry;
import netscape.ldap.LDAPException;
import netscape.ldap.LDAPSearchResults;
public class TestLDAP {
public static void main(String[] args) {
LDAPConnection ldConnection = null;
LDAPEntry ldEntry = null;
try {
ldConnection = new LDAPConnection();
String HOSTNAME = "HOSTNAME";
int PORT = 389;
String TREEBASE = "ou=PEOPLE,o=ha";//your DN
//String QUERY = "(objectclass=* )";
String QUERY = "(&(objectclass=objclass )(attribute=*value*))";
ldConnection.connect( HOSTNAME, PORT );
//String[] attrNames = {"*"};
String[] attributeNames = {"val1","val2","val3","val4"};
LDAPSearchResults ldSearchResult = ldConnection.search(TREEBASE,LDAPConnection.SCOPE_ONE,QUERY,attributeNames,false);
if(ldSearchResult.hasMoreElements()) {
ldEntry = ldSearchResult.next();
}
LDAPAttributeSet ldAttributeSet = ldEntry.getAttributeSet();
Enumeration enumAttributeSet = ldAttributeSet.getAttributes();
while(enumAttributeSet.hasMoreElements()) {
LDAPAttribute ldAttribute = enumAttributeSet.nextElement();
String attrName = ldAttribute.getName();
String[] aVal = ldAttribute.getStringValueArray();
System.out.println("----------------------");
System.out.println("Key : "+attrName);
for(String val : aVal) {
System.out.println("Value : "+val);
}
}
}
catch(LDAPException LDAPex) {
LDAPex.printStackTrace();
}
}
}
import netscape.ldap.LDAPAttribute;
import netscape.ldap.LDAPAttributeSet;
import netscape.ldap.LDAPConnection;
import netscape.ldap.LDAPEntry;
import netscape.ldap.LDAPException;
import netscape.ldap.LDAPSearchResults;
public class TestLDAP {
public static void main(String[] args) {
LDAPConnection ldConnection = null;
LDAPEntry ldEntry = null;
try {
ldConnection = new LDAPConnection();
String HOSTNAME = "HOSTNAME";
int PORT = 389;
String TREEBASE = "ou=PEOPLE,o=ha";//your DN
//String QUERY = "(objectclass=* )";
String QUERY = "(&(objectclass=objclass )(attribute=*value*))";
ldConnection.connect( HOSTNAME, PORT );
//String[] attrNames = {"*"};
String[] attributeNames = {"val1","val2","val3","val4"};
LDAPSearchResults ldSearchResult = ldConnection.search(TREEBASE,LDAPConnection.SCOPE_ONE,QUERY,attributeNames,false);
if(ldSearchResult.hasMoreElements()) {
ldEntry = ldSearchResult.next();
}
LDAPAttributeSet ldAttributeSet = ldEntry.getAttributeSet();
Enumeration
while(enumAttributeSet.hasMoreElements()) {
LDAPAttribute ldAttribute = enumAttributeSet.nextElement();
String attrName = ldAttribute.getName();
String[] aVal = ldAttribute.getStringValueArray();
System.out.println("----------------------");
System.out.println("Key : "+attrName);
for(String val : aVal) {
System.out.println("Value : "+val);
}
}
}
catch(LDAPException LDAPex) {
LDAPex.printStackTrace();
}
}
}