Example usage for java.util Hashtable Hashtable

List of usage examples for java.util Hashtable Hashtable

Introduction

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

Prototype

Hashtable(Void dummy) 

Source Link

Document

A constructor chained from Properties keeps Hashtable fields uninitialized since they are not used.

Usage

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 va 2s .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: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 {/* w w w. ja  va2 s  .com*/
        // 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: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 {//from w  ww  .ja v a 2s.  com
        // 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: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  ww  . j  a v a  2  s .c om*/
        // 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:Shared.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 va 2 s. c o  m*/
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        // Get a copy of the same context
        Context ctx2 = (Context) ctx.lookup("");

        // Get a child context
        Context ctx3 = (Context) ctx.lookup("ou=NewHires");

        // do something useful with ctx, ctx2, ctx3

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

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 {//from  www .j av a 2  s.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:RenameInterior.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 {/*from  w w  w .ja  v a2  s  . c  o m*/
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        // Perform rename
        ctx.rename("ou=NewHires", "ou=OldHires");

        // Check that it worked
        System.out.println(ctx.lookup("ou=OldHires"));

        // Revert change
        ctx.rename("ou=OldHires", "ou=NewHires");

        // Check that we are back at our original setup
        System.out.println(ctx.lookup("ou=NewHIres"));

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

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   w w w .  j a  v a 2 s .  c  o  m*/
        // 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:BadPasswd.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 give incorrect password
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "cn=S. User, ou=NewHires, o=JNDITutorial");
    env.put(Context.SECURITY_CREDENTIALS, "notmysecret");

    try {/*from  w  w  w .  ja v a2s  .  c om*/
        // 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:SerObj.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  ww.j ava 2 s . c om*/
        // Create the initial context
        Context ctx = new InitialContext(env);

        // Create object to be bound
        Button b = new Button("Push me");

        // Perform bind
        ctx.bind("cn=Button", b);

        // Check that it is bound
        Button b2 = (Button) ctx.lookup("cn=Button");
        System.out.println(b2);

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