为什么下面的代码抛出异常,同时使用数据源建立连接mysql到java

问题描述 投票:0回答:1

您好我正在使用以下代码使用数据源创建连接但它的给出错误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);
java datasource
1个回答
2
投票

Refer this link。你可能会得到解决方案。你正在使用哪个容器?你需要提供

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

因此。

© www.soinside.com 2019 - 2024. All rights reserved.