【问题标题】:JNDI not to find DataSource from SQLServerJNDI 不从 SQLServer 中找到 DataSource
【发布时间】:2014-03-26 13:52:41
【问题描述】:

我有一个 MS SQLServer 2008 数据库,名为:“conpool”。并在那里创建了一个带有 id、fullName 和 age 的表。 现在我在 Netbeans 7.3 和 Tomcat 7.0.30 中用 Java 实现了一个连接池,每次与我的数据库建立连接时都不会绑定,我可以为我的数据库进行查询。

我的连接池:

//------------开始 JDBC 连接池 ----- ------------------

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

    Context initialContext = new InitialContext();
    System.out.println("Test1: a object from InitialContext has been created");

    Context context = (Context) initialContext.lookup("java:/comp/env");
    System.out.println("Test2: Context lookup was OKAY");

    //The JDBC Data source that was created in SQLServer
    DataSource datasource = (DataSource) context.lookup("jdbc/conpool");
    System.out.println("Test3: DataSource lookup with JNDI was successfully");
    System.out.println("JDBC Connecction Pool is created successfully");

    //-------------------------Ende JDBC Connection Pool--------------------------------------

    //Using a connection from the pool
    Connection connection = datasource.getConnection();
    System.out.println("Test4: Datasource Connection was successfully");

    if (connection == null) {
        throw new SQLException("Error establishing connection!");
        //System.out.println("DBConnection Failed!!! ");
    }

    //Using the connection to access the database

    //--------------------------------Beginn der Queryprocess------------------------------------------

    String query = "SELECT * FROM conpool.dbo.Personen";
    PreparedStatement statement = connection.prepareStatement(query);
    ResultSet rs = statement.executeQuery();

    System.out.println("Test5: All Object for a single Query are created");
    while (rs.next()) {
        System.out.println(rs.getString(query));
    }
    //Connection will be closed
    connection.close();

    //-------------------------------Ende der Queryprocess--------------------------------------

   }

我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
     version="2.4">
<description>Tomcat-ConnectionPooling with MS SQLServer</description>
<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/conpool</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>
</web-app>

我的 context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context  antiJARLocking="true" path="/ITWService">

<!-- Specify a JDBC datasource -->
<Resource name="jdbc/conpool" auth="Container" type="javax.sql.DataSource"

          maxActive="100" maxIdle="30" maxWait="10000"

          username="dbo" password="" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"

          url="jdbc:sqlserver://localhost:1433;DatabaseName=conpool"/>
</Context>

现在我遇到了这种错误:

运行:

线程“main”中的异常 javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或作为小程序参数,或在应用程序资源文件中:java.naming.factory.initial 在 javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645) 在 javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288) 在 javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325) 在 javax.naming.InitialContext.lookup(InitialContext.java:392) 在 com.akapoor.itwservice.server.RestServer.main(RestServer.java:136) Java 结果:1 构建成功(总时间:1 秒)

请帮助我解决我的问题。我会非常感谢你我很困惑。我尝试了一切,但没有帮助..请告诉我问题出在哪里,为什么我没有连接到我的数据库

【问题讨论】:

    标签: java sql-server xml jndi connection-pooling


    【解决方案1】:

    您需要处于托管环境(如应用服务器)中才能创建不带参数的 InitialContext。

    由于您发布的代码有一个 main 方法,我假设您已将代码作为独立应用程序执行。但是在您的问题中,您写道您的应用程序在 Tomcat 上运行,因此您需要将其部署到您的容器并在那里调用它。

    或者您有什么原因要远程访问 JNDI?

    【讨论】:

    • 不知道它的样子,你在说什么..你能写一段代码作为例子吗..这对我有很大帮助
    • 您发布的错误消息意味着 InitialContext 创建失败。如果您在 Tomcat 中执行相同的代码,它将成功。
    • 如何在 Tomcat 中执行此代码?代码看起来如何,我应该在哪里执行此操作?
    • 你能为我的解决方案写一个例子吗?那么我就可以理解它了 -.-
    猜你喜欢
    • 2014-04-24
    • 2015-12-11
    • 2014-06-10
    • 2019-05-28
    • 2015-09-11
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多