gpt4 book ai didi

java - JNDI 无法在 Tomcat 6 上创建 JDBC 连接

转载 作者:行者123 更新时间:2023-11-28 22:46:00 26 4
gpt4 key购买 nike

当我尝试查看页面时出现此错误:

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

最佳答案

异常的“原因”/“根本原因”部分包含有关失败原因的信息。您不应该只打印异常消息而忽略异常细节的剩余部分。您应该抛出整个异常或至少打印整个堆栈跟踪。

快速修复:更换您的 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);
}

然后重试。整个堆栈跟踪应该显示在服务器的默认错误页面中。或者,如果您自定义了不显示跟踪的错误页面,请查看服务器日志,它也在那里。


更新 根据评论:您可以添加 ServletExceptionthrows条款。根据堆栈跟踪,context.xml 中的资源位置正确,驱动程序已正确加载,但 URL 尚未传入,就好像它未在 <Resource> 中指定一样.

您确定要提供 <Resource>你以为你在供应?你没有另一个<Resource>吗?同名,但没有 urlTomcat/conf/context.xml

关于java - JNDI 无法在 Tomcat 6 上创建 JDBC 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3744278/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com