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 args[]) {

    Hashtable<Integer, String> htable = new Hashtable<Integer, String>();

    // put values into the table
    htable.put(1, "A");
    htable.put(2, "B");
    htable.put(3, "C");
    htable.put(4, "from java2s.com");

    // check table content
    System.out.println("Hash table content: " + htable);

    // clear the table
    htable.clear();//from   w w w.  j av  a 2  s. c  o m

    // check content after clear
    System.out.println("Hash table content after clear: " + htable);
}

From source file:Main.java

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

    // put values into the table
    htable.put(1, "A");
    htable.put(2, "B");
    htable.put(3, "C");
    htable.put(4, "from java2s.com");

    // create enumeration
    Enumeration<String> e = htable.elements();

    System.out.println("Display result:");

    // display search result
    while (e.hasMoreElements()) {
        System.out.println(e.nextElement());
    }/* w  ww .j  av  a 2s.  co  m*/
}

From source file:Main.java

public static void main(String args[]) {
    // create two hash tables 
    Hashtable<Integer, String> htableclone = new Hashtable<Integer, String>();
    Hashtable<Integer, String> htable = new Hashtable<Integer, String>();

    // put values into the table
    htable.put(1, "A");
    htable.put(2, "B");
    htable.put(3, "C");
    htable.put(4, "from java2s.com");

    // check table content
    System.out.println("Original hash table content: " + htable);

    // clone hash table
    htableclone = (Hashtable) htable.clone();

    // check content after clone
    System.out.println("Clone table content: " + htableclone);
}

From source file:Main.java

public static void main(String args[]) {

    Hashtable<Integer, String> htable = new Hashtable<Integer, String>(10);

    // put values into the table
    htable.put(1, "A");
    htable.put(2, "B");
    htable.put(3, "C");
    htable.put(4, "from java2s.com");

    // check table content
    System.out.println("Hash table content: " + htable);

    // clear the table
    htable.clear();//from  w ww.  j  a v a 2  s .com

    // check content after clear
    System.out.println("Hash table content after clear: " + htable);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    // Set up the environment for creating the initial context
    Hashtable env = new Hashtable(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
    env.put(Context.PROVIDER_URL, "file:/jdbc");

    Context context = new InitialContext(env);
    NamingEnumeration list = context.list("jdbc");

    while (list.hasMore()) {
        NameClassPair nc = (NameClassPair) list.next();
        System.out.println(nc);/*from  w  w w .  j  a v a2 s  .c o m*/
    }

    OracleDataSource ods = new OracleDataSource();
    ods.setDriverType("thin");
    ods.setServerName("localhost");
    ods.setNetworkProtocol("tcp");
    ods.setDatabaseName("databaseName");
    ods.setPortNumber(1521);
    ods.setUser("userName");
    ods.setPassword("Password");

    Context ctx = new InitialContext();
    ctx.bind("file:/jdbc/mydb", ods);

    // Get the initial context of JNDI and lookup the datasource.
    InitialContext ic = new InitialContext();
    javax.sql.DataSource ds = (javax.sql.DataSource) ic.lookup("file:/jdbc/mydb");
    // Set the optional printwriter where the trace log is to be directed.
    ds.setLogWriter(new PrintWriter(new FileOutputStream("c:/datasource.log")));
    Connection con1 = ds.getConnection();
    Connection con2 = ds.getConnection("userName", "password");
    conn.close();

}

From source file:Lookup.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  ww .j  a  v  a  2 s.c  o  m
        // Create the initial context
        Context ctx = new InitialContext(env);

        // Perform lookup and cast to target type
        LdapContext b = (LdapContext) ctx.lookup("cn=Rosanna Lee,ou=People");

        System.out.println(b);

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

From source file:Timeout.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");

    // Specify timeout to be 5 seconds
    env.put("com.sun.jndi.ldap.connect.timeout", "5000");

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

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

public static void main(String args[]) {

    Hashtable<Integer, String> htable = new Hashtable<Integer, String>(10, 0.75F);

    // put values into the table
    htable.put(1, "A");
    htable.put(2, "B");
    htable.put(3, "C");
    htable.put(4, "from java2s.com");

    // check table content
    System.out.println("Hash table content: " + htable);

    // clear the table
    htable.clear();/*from w  w  w.j  ava 2 s. c om*/

    // check content after clear
    System.out.println("Hash table content after clear: " + htable);
}

From source file:LookupLdapName.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/");

    try {/*from w w  w .j av  a 2  s . c o m*/
        // Create the initial context
        Context ctx = new InitialContext(env);

        // A string representation of an LDAP name
        LdapName dn = new LdapName("ou=People,o=JNDITutorial");

        // Perform the lookup using the dn
        Object obj = ctx.lookup(dn);

        System.out.println(obj);

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

From source file:ReadTimeoutTest.java

public static void main(String[] args) throws Exception {

    boolean passed = false;

    // Set up the environment for creating the initial context
    Hashtable env = new Hashtable(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put("com.sun.jndi.ldap.read.timeout", "1000");
    env.put(Context.PROVIDER_URL, "ldap://localhost:2001");

    Server s = new Server();

    try {/*from  w w  w. j  a v  a2  s .c o m*/

        // start the server
        s.start();

        // Create initial context
        DirContext ctx = new InitialDirContext(env);
        System.out.println("LDAP Client: Connected to the Server");

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