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:Main.java

public static void main(String[] argv) throws Exception {
    Attributes attrs = new BasicAttributes(true);
    Attribute objclass = new BasicAttribute("objectclass");
    objclass.add("top");
    objclass.add("extensible");
    attrs.put(objclass);/*from   w  w w  .  j a v a  2  s  . com*/

    Object obj = "yourObject";
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "yourURL");

    DirContext ctx = new InitialDirContext(env);

    ctx.bind("cn=Sample", obj, attrs);
}

From source file:Main.java

public static void main(String[] args) {
    Hashtable<String, String> ht = new Hashtable<String, String>();
    ht.put("1", "One");
    ht.put("2", "Two");
    ht.put("3", "Three");

    Collection c = ht.values();/*from www  . j  a va  2s. co m*/
    Iterator itr = c.iterator();
    while (itr.hasNext()) {
        System.out.println(itr.next());
    }
    c.remove("One");

    Enumeration e = ht.elements();

    while (e.hasMoreElements()) {
        System.out.println(e.nextElement());
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String url = "iiop://localhost/";
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
    env.put(Context.PROVIDER_URL, url);

    Context ctx = new InitialContext(env);
    // Add a binding.
    ctx.bind("Name", null);

    // Replace a binding.
    ctx.rebind("Name", null);

    // Remove a binding.
    ctx.unbind("Name");

    // Rename a binding.
    ctx.rename("Name", "NewSample");
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String url = "ldap://localhost/o=JNDITutorial";
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, url);

    DirContext ctx = new InitialDirContext(env);
    String[] attrIDs = { "sn", "number", "value", "mail" };

    Attributes answer = ctx.getAttributes("cn=yourName, ou=People", attrIDs);

    NamingEnumeration e = answer.getAll();
    while (e.hasMore()) {
        Attribute attr = (Attribute) e.next();
        System.out.println(attr.getID());
    }//  www  . j a  v  a 2 s . co m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    SearchControls ctls = new SearchControls();
    ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);

    String filter = "(&(sn=YourName)(mail=*))";

    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "yourURL");

    DirContext ctx = new InitialDirContext(env);

    NamingEnumeration e = ctx.search("", filter, ctls);

    while (e.hasMore()) {
        SearchResult entry = (SearchResult) e.next();
        System.out.println(entry.getName());
    }//from w  ww  .  j av a2s. c  om
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
    env.put("java.naming.provider.url", "dns://123.123.123.123/");

    String dns_attributes[] = { "MX", "A", "HINFO" };

    DirContext ctx = new InitialDirContext(env);
    Attributes attrsl = ctx.getAttributes("http://www.yourserver.com", dns_attributes);
    for (int z = 0; z < dns_attributes.length; z++) {
        Attribute attr = attrsl.get(dns_attributes[z]);

        if (attr != null) {
            System.out.print(dns_attributes[z] + ": ");
            for (Enumeration vals = attr.getAll(); vals.hasMoreElements();) {
                System.out.println(vals.nextElement());
            }/*  ww w  .j  av a  2 s. c  om*/
        }
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);

    env.put(Context.PROVIDER_URL, MY_HOST);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, MGR_DN);
    env.put(Context.SECURITY_CREDENTIALS, MGR_PW);

    DirContext ctx = new InitialDirContext(env);

    Person p = new Person();
    ctx.bind("uid=mewilcox, ou=People, o=airius.com", p);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Attributes matchAttrs = new BasicAttributes(true);
    matchAttrs.put(new BasicAttribute("sn", "YourName"));
    matchAttrs.put(new BasicAttribute("mail"));

    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "yourURL");

    DirContext ctx = new InitialDirContext(env);

    NamingEnumeration e = ctx.search("ou=People", matchAttrs);

    if (e.hasMore()) {
        SearchResult entry = (SearchResult) e.next();
        // Abandon rest of results
        e.close();/*  w  ww. ja  va  2s.c o  m*/
    }
}

From source file:Main.java

public static void main(String[] args) {

    HashMap<String, String> hMap = new HashMap<String, String>();

    hMap.put("1", "One");
    hMap.put("2", "Two");
    hMap.put("3", "Three");

    Hashtable<String, String> ht = new Hashtable<String, String>();
    ht.put("1", "REPLACED !!");
    ht.put("4", "Four");

    Enumeration e = ht.elements();
    while (e.hasMoreElements()) {
        System.out.println(e.nextElement());
    }//from w  ww .jav  a  2s.  com

    ht.putAll(hMap);
    e = ht.elements();

    while (e.hasMoreElements()) {
        System.out.println(e.nextElement());
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);
    env.put(Context.PROVIDER_URL, MY_HOST);
    DirContext ctx = new InitialDirContext(env);
    SearchControls constraints = new SearchControls();
    constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
    NamingEnumeration results = ctx.search(MY_SEARCHBASE, MY_FILTER, constraints);
    while (results != null && results.hasMore()) {
        SearchResult sr = (SearchResult) results.next();
        String dn = sr.getName() + ", " + MY_SEARCHBASE;

        System.out.println("Distinguished Name is " + dn);
        Attributes ar = ctx.getAttributes(dn, MY_ATTRS);
        if (ar == null) {
            System.out.println("Entry " + dn + " has none of the specified attributes\n");
            return;
        }//from  www  .ja va 2  s . c o  m
        for (int i = 0; i < MY_ATTRS.length; i++) {
            Attribute attr = ar.get(MY_ATTRS[i]);
            if (attr == null) {
                continue;
            }
            System.out.println(MY_ATTRS[i] + ":");
            for (Enumeration vals = attr.getAll(); vals.hasMoreElements();) {
                System.out.println("\t" + vals.nextElement());

            }
        }
    }
}