connectldap.java

来自「精通Java核心技术源代码」· Java 代码 · 共 42 行

JAVA
42
字号
// ==================== Program Discription ==========================
// 程序名称:示例10-2 : ConnectLDAP.java
// 程序目的:通过JNDI 连接一个LDAP数据库
// ==============================================================
import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;

public class ConnectLDAP
{
   public static void main (String[] args) throws Exception 
   {
      InitialDirContext ctx = null;
      String str;
      try
      {
         // Hashtable for environmental information
         Hashtable env = new Hashtable();

         // specify the service provider
         env.put(Context.INITIAL_CONTEXT_FACTORY,
               "com.sun.jndi.ldap.LdapCtxFactory");
         // specify host and port to use the directory service
         env.put(Context.PROVIDER_URL, "ldap://localhost:1088");
	env.put(Context.SECURITY_AUTHENTICATION,"simple");
	env.put(Context.SECURITY_PRINCIPAL,"username");
	env.put(Context.SECURITY_CREDENTIALS,"password");

         // get a reference to a directory context
         ctx = new InitialDirContext(env);

         str = "uid=myid, dc=mycompany, dc=com" + "ou=customer, o= ConnectLDAP";
         Context user = (Context) ctx.lookup(str);
         Attributes attrs = user.getAttributes("");
       }
       catch(Exception e)
       {
          e.printStackTrace();
       }
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?