⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loadldap2db.java

📁 provide a room booking system
💻 JAVA
字号:
package Wearnes;

import com.novell.ldap.*;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class LoadLdap2DB
{

    public static void printEntry(LDAPEntry entry){
              System.out.println(entry.getDN());
        System.out.println("\tAttributes: ");

        LDAPAttributeSet attributeSet = entry.getAttributeSet();
        Set sortedAttributes = new TreeSet( attributeSet );
        Iterator allAttributes = sortedAttributes.iterator();

        while(allAttributes.hasNext()) {
            LDAPAttribute attribute =
                    (LDAPAttribute)allAttributes.next();
            String attributeName = attribute.getName();

            System.out.println("\t\t" + attributeName);

            Enumeration allValues = attribute.getStringValues();

            if( allValues != null) {
                while(allValues.hasMoreElements()) {
                    String Value = (String) allValues.nextElement();
                    System.out.println("\t\t\t" + Value);
                }
            }
        }
        return;
    }
    public static void LoadLdapIntoDB(LDAPEntry entry)
    {
    	 System.out.println(entry.getDN());
         System.out.println("\tAttributes: ");

         LDAPAttributeSet attributeSet = entry.getAttributeSet();
         Set sortedAttributes = new TreeSet( attributeSet );
         Iterator allAttributes = sortedAttributes.iterator();

         WearnesDB db=new WearnesDB();
         String insertSql="";
         String uid="",name="",department="",telephone="",title="",email="";
         while(allAttributes.hasNext())
         {

             LDAPAttribute attribute =(LDAPAttribute)allAttributes.next();
             String attributeName = attribute.getName();

             System.out.println("\t\t" + attributeName);

             Enumeration allValues = attribute.getStringValues();

             if( allValues != null)
             {
            	 String value="";
                 while(allValues.hasMoreElements())
                 {
                     value = (String) allValues.nextElement();
                     System.out.println("\t\t\t" + value);
                 }

                 	if(attributeName.equals("cn"))
                 		name=value;
                 	if(attributeName.equals("mail"))
                 	{
                 	    email=value;
                 	    int idx=email.indexOf("@");
                 	    uid=email.substring(0, idx);
                 	}
                 	if(attributeName.equals("ou"))
                        department=value;
                 	if(attributeName.equals("telephoneNumber"))
                 	    telephone=value;
                 	if(attributeName.equals("title"))
                 		title=value;

             }

         }
         insertSql="insert into employee(name,password,department,email,telephone,title,securitylevel,fullName) " +
   		"values('"+uid+"','password','"+department+"','"+email+"','"+telephone+"','"+title+"','1','"+name+"')";
	     try
	     {
	      db.insert(insertSql);
	     }catch(Exception e)
	     {
	     	System.out.println(e.getMessage());
	     }

         return;
    }
     public static void main(String[] args)
     {
         int ldapPort = LDAPConnection.DEFAULT_PORT;
         int searchScope = LDAPConnection.SCOPE_ONE;
         int ldapVersion  = LDAPConnection.LDAP_V3;

         String ldapHost = "ldap.wearnes.com.sg";
         String loginDN  = "";
         String password = "";
         String searchBase = "ou=contacts,dc=wearnes,dc=com,dc=sg";
         String searchFilter = "uid=*";
         LDAPConnection conn = new LDAPConnection();

         try {
            // connect to the server
             conn.connect( ldapHost, ldapPort );
            // bind to the server
             conn.bind( ldapVersion, loginDN, password.getBytes("UTF8"));

             LDAPSearchResults searchResults =
                 conn.search(  searchBase,
                             searchScope,
                             searchFilter,
                             new String[] { "cn", "mail","telephoneNumber","ou","title"},//attributes
                             false);       // return attrs and values


             TreeSet sortedResults = new TreeSet();
             while ( searchResults.hasMore()) {
                 try {
                   //  sortedResults.add( searchResults.next() );
                     LoadLdapIntoDB((LDAPEntry)searchResults.next() );
                     }
                 catch(LDAPException e) {
                     System.out.println("Error3: " + e.toString());
                    // Exception is thrown, go for next entry
                     continue;
                 }
             }

            // print the sorted results
             System.out.println( "\n"+
                     "****************************\n"+
                     "Search Wearnes LDAP results sorted by DN:\n"+
                     "****************************");
             Iterator i = sortedResults.iterator();
             while (i.hasNext()){
                 printEntry( (LDAPEntry) i.next() );

             }

             /* resort the results an an array using a specific comparator */
             String namesToSortBy[]  = { "sn", "uid", "cn"  };
             boolean sortAscending[] = { true, false, true };
             LDAPCompareAttrNames myComparator = new LDAPCompareAttrNames(
                     namesToSortBy, sortAscending);

             Object sortedSpecial[] = sortedResults.toArray();
             Arrays.sort(sortedSpecial, myComparator);

            // print the re-sorted results
             System.out.println( "\n" +
                     "*****************************************************\n" +
                     "Search results sorted by sn, uid(Descending), and cn:\n" +
                     "*****************************************************");
             for(int j=0; j< sortedSpecial.length; j++){
                 printEntry( (LDAPEntry) sortedSpecial[j] );
             }
            // disconnect with the server
             conn.disconnect();
         }
         catch( LDAPException e ) {
             System.out.println( "Error1: " + e.toString() );
         }
         catch( UnsupportedEncodingException e ) {
             System.out.println( "Error2: " + e.toString() );
         }
         System.exit(0);
     }


}

⌨️ 快捷键说明

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