import java.io.*;
import java.util.*;
import java.lang.*;
import java.sql.*;
import javax.naming.*;
//import psi.*;
import netscape.ldap.*;public class LDAPTestConnect {
     public static void main(String args[]){
   String ldaphost="10.168.62.187";
   int ldapport=389;
   String userDN="cn=Directory Manager";
   String password="1234";
   LDAPConnection ld=new LDAPConnection();;
   try{
     ld.connect(ldaphost,ldapport);
     ld.authenticate(userDN,password);
     System.out.println("Connect ok=");
    }catch(LDAPException e){
      System.out.println("M:initLdapConn() Exception="+e.getMessage());
};
   if (ld.isConnected()){
     System.out.println("Version:"+ld.LDAP_VERSION);
     try{
       ld.disconnect();
     }catch(LDAPException e){
        System.out.println("M:initLdapConn() Exception="+e.getMessage());
      }
     
   }
  }}
这个程序用了netscape的jar包免费的,用jdk带的sun的包也是可以的。

解决方案 »

  1.   

    import javax.naming.Context;
    import javax.naming.directory.InitialDirContext;
    import javax.naming.directory.DirContext;
    import javax.naming.directory.Attributes;
    import javax.naming.NamingException;
    import java.util.Hashtable;class GetAttrib {
       public static void main(String[] argv) {
          Hashtable env = new Hashtable();
          env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
          // specify where the ldap server is running
          env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=javacourses.com,c=Canada");
          // use simple authenticate to authenticate the user
          env.put(Context.SECURITY_AUTHENTICATION, "simple");
          env.put(Context.SECURITY_PRINCIPAL, "cn=Directory Manager");
          env.put(Context.SECURITY_CREDENTIALS, "password");      try {
             // Create the initial directory context
             DirContext ctx = new InitialDirContext(env);         // Ask for all attributes of the object
             Attributes attrs = ctx.getAttributes("cn=Qusay Mahmoud");         // Find the surname ("sn") attribute of this object and print it
             System.out.println("Last Name: " + attrs.get("sn").get());         // Close the context
             ctx.close();
          } catch (NamingException e) {
             System.err.println("Problem getting attribute: " + e);
          }
       }
    }再不行就没有办法了。