如何获取所有JNDI数据库选项?

时间:2015-07-23 10:47:23

标签: java servlets jndi

我需要一些帮助。我必须将整个jndi db选项检索到html文件中的选择列表,这意味着我需要访问所有jndi db名称并获取这些值 任何的想法 ?

1 个答案:

答案 0 :(得分:0)

我在其中一个WebLogic服务器中找到JNDI名称时遇到了一些麻烦。我创建了一个遍历JDNI树结构的方法,并将所有这些结构打印到控制台。我不确定这是你需要的,但这是我用它的代码:

private void printList(String directory){
        try {
            NamingEnumeration<NameClassPair> namings = context.list(directory);
            System.out.println("************Printing " + directory + "**************");
            while(namings.hasMoreElements()){
                if(directory.equals("")) printList(namings.next().getName());
                else printList(directory+"."+namings.next().getName());
            }
        System.out.println("Done printing " + directory);
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            System.out.println(directory);
        }
    }

如果您想要根目录中的所有元素,则可以传递所需目录的字符串或空字符串。您需要导入javax.naming.*(或-NamingEnumeration / -NameClassPair

几乎忘了: 您首先需要为方法查找目录的位置定义上下文:

Context context = null;
        Hashtable<String, String> ht = new Hashtable<String, String>();
        ht.put(Context.PROVIDER_URL, "your_host");
        ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
        //this above needs to be changed to fit your desired system
        ht.put(Context.SECURITY_PRINCIPAL, "your_user");
        ht.put(Context.SECURITY_CREDENTIALS, "your_password");
        try {
            context = new InitialContext(ht);
            // Use the context in your program
        }
        catch (NamingException e) {
            e.printStackTrace();
        }