Example usage for java.util Hashtable put

List of usage examples for java.util Hashtable put

Introduction

In this page you can find the example usage for java.util Hashtable put.

Prototype

public synchronized V put(K key, V value) 

Source Link

Document

Maps the specified key to the specified value in this hashtable.

Usage

From source file:Destroy.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {/*from w  w w. j a v a2  s  . co  m*/
        // Create the initial context
        Context ctx = new InitialContext(env);

        // Destroy the context
        ctx.destroySubcontext("ou=NewOu");

        // Check that it has been destroyed by listing its parent
        NamingEnumeration list = ctx.list("");

        // Go through each item in list
        while (list.hasMore()) {
            NameClassPair nc = (NameClassPair) list.next();
            System.out.println(nc);
        }

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        System.out.println("destroy failed: " + e);
    }
}

From source file:Unbind.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {/*from  ww w . j a  va2  s .c  om*/
        // Create the initial context
        Context ctx = new InitialContext(env);

        // Remove the binding
        ctx.unbind("cn=Favorite Fruit");

        // Check that it is gone
        Object obj = null;
        try {
            obj = ctx.lookup("cn=Favorite Fruit");
        } catch (NameNotFoundException ne) {
            System.out.println("unbind successful");
            return;
        }

        System.out.println("unbind failed; object still there: " + obj);

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        System.out.println("Operation failed: " + e);
    }
}

From source file:Rename.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/ou=People,o=JNDITutorial");

    try {/*ww  w.  ja va 2 s . c o  m*/
        // Create the initial context
        Context ctx = new InitialContext(env);

        // Rename to Scott S
        ctx.rename("cn=Scott Seligman", "cn=Scott S");

        // Check that it is there using new name
        Object obj = ctx.lookup("cn=Scott S");
        System.out.println(obj);

        // Rename back to Scott Seligman
        ctx.rename("cn=Scott S", "cn=Scott Seligman");

        // Check that it is there with original name
        obj = ctx.lookup("cn=Scott Seligman");
        System.out.println(obj);

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        System.out.println("Rename failed: " + e);
    }
}

From source file:SearchWithFilterRetAll.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {//from ww w .ja  v a2 s  .c o  m
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        // Create default search controls
        SearchControls ctls = new SearchControls();

        // Specify the search filter to match
        // Ask for objects with attribute sn == Geisel and which have
        // the "mail" attribute.
        String filter = "(&(sn=Geisel)(mail=*))";

        // Search for objects using filter
        NamingEnumeration answer = ctx.search("ou=People", filter, ctls);

        // Print the answer
        // Search.printSearchEnumeration(answer);

        // Close the context when we're done
        ctx.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:UseDiff.java

public static void main(String[] args) {

    // Set up environment for creating initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    // Authenticate as S. User and password "mysecret"
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "cn=S. User, ou=NewHires, o=JNDITutorial");
    env.put(Context.SECURITY_CREDENTIALS, "mysecret");

    try {//w ww  . ja  va2  s .  c  om
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        // do something useful with ctx
        System.out.println(ctx.lookup("ou=NewHires"));

        // Change to using no authentication
        ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, "none");

        System.out.println(ctx.lookup("ou=NewHires"));

        // do something useful with ctx

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

From source file:SearchWithFilterObjs.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {//from   w w w. j  a va2  s  . c  om
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        // Specify the ids of the attributes to return
        String[] attrIDs = { "sn", "telephonenumber", "golfhandicap", "mail" };
        SearchControls ctls = new SearchControls();
        ctls.setReturningAttributes(attrIDs);

        // Specify the search filter to match
        // Ask for objects with attribute sn == Geisel and which have
        // the "mail" attribute.
        String filter = "(&(sn={0})(mail=*))";

        // Search for objects using filter
        NamingEnumeration answer = ctx.search("ou=People", filter, new Object[] { "Geisel" }, ctls);

        // Print the answer
        // Search.printSearchEnumeration(answer);

        // Close the context when we're done
        ctx.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    Hashtable<String, Double> balance = new Hashtable<String, Double>();

    String str;//from   w ww . ja v  a2 s .co  m
    double bal;

    balance.put("A", 4.34);
    balance.put("B", 3.22);
    balance.put("C", 8.00);
    balance.put("D", 9.22);
    balance.put("E", -9.08);

    Set<String> set = balance.keySet();

    Iterator<String> itr = set.iterator();
    while (itr.hasNext()) {
        str = itr.next();
        System.out.println(str + ": " + balance.get(str));
    }

    System.out.println();

    bal = balance.get("A");
    balance.put("A", bal + 1000);
    System.out.println("A's new balance: " + balance.get("A"));
}

From source file:Compare.java

public static void main(String[] args) {

    // Set up environment for creating initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {//  w w w .  j  a v  a 2  s  .c o m
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        // Value of attribute
        byte[] key = { (byte) 0x61, (byte) 0x62, (byte) 0x63, (byte) 0x64, (byte) 0x65, (byte) 0x66,
                (byte) 0x67 };

        // Set up search controls
        SearchControls ctls = new SearchControls();
        ctls.setReturningAttributes(new String[0]); // return no attrs
        ctls.setSearchScope(SearchControls.OBJECT_SCOPE); // search object only

        // Invoke search method that will use the LDAP "compare" operation
        NamingEnumeration answer = ctx.search("cn=S. User, ou=NewHires", "(mySpecialKey={0})",
                new Object[] { key }, ctls);

        // Print the answer
        // FilterArgs.printSearchEnumeration(answer);

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

From source file:SearchWithFilter.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {//w  w w  .  j  ava2s  .  co m
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        // Specify the ids of the attributes to return
        String[] attrIDs = { "sn", "telephonenumber", "golfhandicap", "mail" };
        SearchControls ctls = new SearchControls();
        ctls.setReturningAttributes(attrIDs);

        // Specify the search filter to match
        // Ask for objects with attribute sn == Geisel and which have
        // the "mail" attribute.
        String filter = "(&(sn=Geisel)(mail=*))";

        // Search for objects using filter
        NamingEnumeration answer = ctx.search("ou=People", filter, ctls);

        // Print the answer
        // Search.printSearchEnumeration(answer);

        // Close the context when we're done
        ctx.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ManageReferral.java

public static void main(String[] args) {

    // Set up environment for creating initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:489/o=JNDITutorial");
    // env.put(Context.REFERRAL, "follow");
    env.put(Context.REFERRAL, "ignore");

    try {/*www .j a  v a  2  s.c  o m*/
        // Create initial context
        LdapContext ctx = (LdapContext) new InitialLdapContext(env, null);
        // ctx.setRequestControls(new Control[] {
        // new ManageReferralControl() });

        // Set controls for performing subtree search
        SearchControls ctls = new SearchControls();
        ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);

        // Perform search
        NamingEnumeration answer = ctx.search("", "(objectclass=*)", ctls);

        // Print the answer
        while (answer.hasMore()) {
            System.out.println(">>>" + ((SearchResult) answer.next()).getName());
        }

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        e.printStackTrace();
    }
}