簡體   English   中英

JNDI無法在Tomcat 6上創建JDBC連接

[英]JNDI Can't Create a JDBC Connection on Tomcat 6

嘗試查看頁面時出現此錯誤:

SQLException: Cannot create JDBC driver of class '' for connect URL 'null'

我有以下/WEB-INF/web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
    <display-name>WSwartzendruber.net</display-name>
    <description>Personal Website</description>
    <!-- Servlet stuff -->
    <servlet>
        <servlet-name>PostgresTest</servlet-name>
        <servlet-class>PostgresTest</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>PostgresTest</servlet-name>
        <url-pattern>/servlets/postgrestest</url-pattern>
    </servlet-mapping>
    <!-- JNDI -->
    <resource-ref>
        <description>PostgreSQL Data Source</description>
        <res-ref-name>jdbc/db_website</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

我在/META-INF/context.xml中有這個:

<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true">
    <Resource name="jdbc/db_website" auth="Container" type="javax.sql.DataSource" driverClassName="org.postgresql.Driver" url="jdbc:postgresql://localhost:5432/db_website" username="website"/>
</Context>

這是測試代碼:

import java.io.*;
import java.sql.*;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;
import org.wswartzendruber.website.access.*;

public class PostgresTest extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        response.setContentType("text/plain");

        try
        {
            Context initialContext = new InitialContext();
            Context context = (Context)initialContext.lookup("java:comp/env");
            DataSource dataSource = (DataSource)context.lookup("jdbc/db_website");
            Connection connection = dataSource.getConnection();
            NewsCategory[] newsCategory = NewsCategory.getNewsCategories(connection);

            connection.close();
        }
        catch (NamingException e)
        {
            response.getWriter().println("ERROR: We have naming problems!");
        }
        catch (SQLException se)
        {
            response.getWriter().println("ERROR: SQLException: " + se.getMessage());
        }
        catch (AccessException ae)
        {
            response.getWriter().println("ERROR: AccessException: " + ae.getMessage());
        }
    }
}

我不知所措。 我已經有/usr/share/tomcat-6/lib/postgresql-8.4-702.jdbc4.jar了。 將其移動到/ WEB-INF / lib沒什么區別。 在我看來,它可以在WEB-INF / web.xml中看到資源條目,但無法從META-INF / context.xml中提取詳細信息。 在此分配密碼似乎也沒有什么區別。

你們有什么我都願意。


更新 :這是完整的堆棧跟蹤:

Sep 19, 2010 7:01:36 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet PostgresTest threw exception
java.io.IOException: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
    at PostgresTest.doGet(PostgresTest.java:23)
    at javax.servlet.http.HttpServlet.service(Unknown Source)
    at javax.servlet.http.HttpServlet.service(Unknown Source)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Unknown Source)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(Unknown Source)
    at org.apache.catalina.core.StandardWrapperValve.invoke(Unknown Source)
    at org.apache.catalina.core.StandardContextValve.invoke(Unknown Source)
    at org.apache.catalina.core.StandardHostValve.invoke(Unknown Source)
    at org.apache.catalina.valves.ErrorReportValve.invoke(Unknown Source)
    at org.apache.catalina.core.StandardEngineValve.invoke(Unknown Source)
    at org.apache.catalina.connector.CoyoteAdapter.service(Unknown Source)
    at org.apache.jk.server.JkCoyoteHandler.invoke(Unknown Source)
    at org.apache.jk.common.HandlerRequest.invoke(Unknown Source)
    at org.apache.jk.common.ChannelSocket.invoke(Unknown Source)
    at org.apache.jk.common.ChannelSocket.processConnection(Unknown Source)
    at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(Unknown Source)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:636)
Caused by: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
    at PostgresTest.doGet(PostgresTest.java:17)
    ... 17 more
Caused by: java.lang.NullPointerException
    at org.postgresql.Driver.parseURL(Driver.java:567)
    at org.postgresql.Driver.acceptsURL(Driver.java:412)
    at java.sql.DriverManager.getDriver(DriverManager.java:268)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
    ... 20 more

異常的“由...引起” /“根本原因”部分包含有關失敗原因的信息。 您不應僅打印異常消息,而忽略異常詳細信息的剩余部分。 您應該拋出整個異常,或者至少打印整個stacktrace。

快速解決方案:將您的try替換為

try {
    Context initialContext = new InitialContext();
    Context context = (Context)initialContext.lookup("java:comp/env");
    DataSource dataSource = (DataSource)context.lookup("jdbc/db_website");
    Connection connection = dataSource.getConnection();
    NewsCategory[] newsCategory = NewsCategory.getNewsCategories(connection);
    connection.close();
} catch (Exception e) {
    throw new ServletException(e);
}

然后重試。 整個堆棧跟蹤應該顯示在服務器的默認錯誤頁面中。 或者,如果您已自定義了不顯示跟蹤的錯誤頁面,請在服務器日志中查找該頁面。


按照注釋進行更新 :您可以僅將ServletException添加到throws子句中。 根據stacktrace的說明, context.xml的資源已正確定位,驅動程序已正確加載,但是未傳遞URL,就像未在<Resource>指定URL一樣。

您確定要提供您認為正在提供的<Resource>嗎? 您是否沒有另一個具有相同名稱的<Resource> ,但是在Tomcat/conf/context.xml沒有url

是的,我也遇到了這個問題。如果我將該段放入tomcat / conf / context.xml,它可以工作。我不知道為什么

在WebRoot / META-INF文件夾中添加context.xml並放入

<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="jdbc/ecms" auth="Container" type="javax.sql.DataSource"
    maxActive="100" maxIdle="30" maxWait="10000"
    username="root" password="root123#" driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/ecms"/>

在web.xml頁面中寫

<resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/ecms</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

形成一個新的Java類並編寫

package com.common;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.sql.DataSource;

public class DataSourceq {

    public Connection dbc() throws NamingException, SQLException, ServletException{
        try {
            InitialContext initialContext = new InitialContext();
            Context context = (Context)initialContext.lookup("java:comp/env");
            //The JDBC Data source that we just created
            DataSource ds = (DataSource) context.lookup("jdbc/ecms");
            Connection connection = ds.getConnection();
            return connection;
        }

        catch (Exception e) {
            throw new ServletException(e);
        }
    }
}

為您的數據源類創建對象並調用

DataSourceq dq = new DataSourceq();
Connection con =  dq.dbc(); 

並執行查詢,它將成功運行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM