0

您好我正在使用以下代码使用数据源创建连接,但它给出错误 javax.naming.NoInitialContextException:无法实例化类:org.apache.naming.java.javaURLContextFactory [根异常是 java.lang.ClassNotFoundException:org.apache.naming。 java.javaURLContextFactory]

这是我在java类中的代码

             System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                 "org.apache.naming.java.javaURLContextFactory");
             System.setProperty(Context.URL_PKG_PREFIXES, 
                 "org.apache.naming");     
        Properties  properties = new Properties ();
        properties.setProperty("url", "jdbc:mysql://localhost:3306/test");
        properties.setProperty("maxActive", "10");
        properties.setProperty("maxIdle", "8");
        properties.setProperty("minIdle", "10");
        properties.setProperty("maxWait", "10");
        properties.setProperty("testOnBorrow", "true");
        properties.setProperty("username", "snefouser");
        properties.setProperty("password", "snefouser");
        properties.setProperty("validationQuery", "SELECT 1");
        properties.setProperty("removeAbandoned", "true");
        properties.setProperty("removeAbandonedTimeout", "1");
        properties.setProperty("logAbandoned", "true");

        InitialContext ic = new InitialContext();
        ic.createSubcontext("java:");
            ic.createSubcontext("java:/comp");
            ic.createSubcontext("java:/comp/env");
        ic.createSubcontext("java:/comp/env/jdbc");

   DataSource ds = BasicDataSourceFactory.createDataSource(properties);

    ic.bind("java:/comp/env/jdbc/mydb", ds);
4

1 回答 1

2

请参阅此链接。你可能会得到解决方案。您使用的是哪个容器?您需要提供

System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
             "org.apache.naming.java.javaURLContextFactory");

因此。

于 2013-08-08T08:09:13.390 回答