English 中文(简体)
• 如何具体确定分类/数据?
原标题:How to specify jdbc/DataSource?

I m 试图利用像样的编码与矿石数据库连接:

public static Connection getConnection() throws ClassNotFoundException {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Properties env = new Properties();
    //env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://192.168.1.1:389/o=myo,dc=mydc,dc=us");
    Connection connection = null;

    try {
        InitialContext context = new InitialContext(env);
        DataSource dataSource = (DataSource) context.lookup("jdbc/DataSource");
        connection = dataSource.getConnection();
    } catch (NamingException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return connection;
}

但它始终不例外:

javax.naming.InvalidNameException: jdbc: [LDAP: error code 34 - invalid DN]; remaining name  jdbc/DataSource 

哪里,以及怎样才能具体指出:jdbc/DataSource?

I m confused as I was given a working project using codes similar as above but nowhere tells what DataSource is. I m wondering if jdbc/DataSource should be a DN in LDAP, but I find nothing seem relavent in my given ldap data. Sorry if that question is stupid or not make sense, i m new to that all stuff.

感谢!

最佳回答

经过几个星期的研究,最后找到了以下解决办法。 如果其他人面临同样的问题,希望会有所帮助。

我将网上应用程序从Web Sphere转至Tomcat,用户从网上星座的名录连接起来。 Tomcat没有这种目录,但它可以具体说明,在服务器上,xml/context.xml/web.xml,但我从未能够做到! 因此,我的解决办法是使用档案目录:

public static Connection getConnection() throws ClassNotFoundException {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Properties env = new Properties();

    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
    env.put(Context.PROVIDER_URL, "file:/C://JNDI");

    Connection connection = null;

    try {
        InitialContext context = new InitialContext(env);
        DataSource dataSource = (DataSource) context.lookup("jdbc/DataSource");
        connection = dataSource.getConnection();
    } catch (NamingException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return connection;
}

仅仅修改了两条电线,并做了罚款。 但是,为创建基于档案的目录做了一些准备工作,这里是建立一个文件C:JNDI具有约束力的方案。

public static void main(String args[]) throws SQLException, NamingException {

    Context ctx = null;
    try {
        Properties prop = new Properties();
        prop.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                "com.sun.jndi.fscontext.RefFSContextFactory");
        prop.setProperty(Context.PROVIDER_URL, "file:/C:/JNDI");
        ctx = new InitialContext(prop);

        OracleDataSource ds = new OracleDataSource();
        ds.setDriverType("thin");
        ds.setServerName("123.123.123.123");
        ds.setPortNumber(1521);
        ds.setDatabaseName("xe");
        ds.setUser("username");
        ds.setPassword("password");
        ctx.bind("jdbc/DataSource", ds);

    } catch (NamingException ne) {
        System.err.println(ne.getMessage());

        ne.printStackTrace();
    }

}

使其在我的生产网目和测试目录中发挥作用,即只使用不同的INITIAL_CONTEXT_FACTORY环境取决于财产档案。

感谢奥拉夫的极大帮助!

问题回答

如果您的法典在网上发挥作用,我会猜测,大街区可能位于大街:jndi-lookup。 您能否研究“jee:jndi-lookup/jdbc/DataSource”或“java:comp/env/jdbc/DataSource”?

您应尝试一名全球倡议名录浏览器,以浏览你的开放式区域行动方案目录,以了解贵国数据来源的定义。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签