无法为连接 URL 'null' 创建类""的 JDBC 驱动程序:我不理解此异常 [英] Cannot create JDBC driver of class ' ' for connect URL 'null' : I do not understand this exception

查看:29
本文介绍了无法为连接 URL 'null' 创建类""的 JDBC 驱动程序:我不理解此异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我提供了数据库 URL 时,为什么它说 null URL 并在异常中给出一个空的""类?

Why does it say null URL and gives a empty ' ' class in the exception when I have provided the database URL?

我正在尝试在使用 Tomcat 时通过 servlet 连接到 derby 数据库.当 servlet 运行时,我得到以下异常:

I am trying to connect to a derby database via a servlet while using Tomcat. When the servlet gets run, I get the following exceptions:

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 servlets.servlet_1.doGet(servlet_1.java:23) // ---> Marked the statement in servlet

at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:507)
at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:476)
at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307)
at java.sql.DriverManager.getDriver(DriverManager.java:253)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
... 24 more

小服务程序:

package servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.http.*;
import javax.servlet.*;
import javax.sql.DataSource;

public class servlet_1 extends HttpServlet {

    @Override 
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
           // String queryString = request.getQueryString();
            System.out.println("!!!!!!!!!!!!!!!!!!!");
            Context initContext = new InitialContext();
            Context envContext = (Context)initContext.lookup("java:comp/env");
            DataSource ds = (DataSource)envContext.lookup("jdbc/PollDatasource");
            Connection connection = ds.getConnection(); // -->LINE 23
            String sqlQuery = "select * from PollResult";
            PreparedStatement statement = connection.prepareStatement(sqlQuery);
            ResultSet set = statement.executeQuery();
            System.out.println("after the final statement");
        } catch (Exception exc) {
            exc.printStackTrace();
        }
    }
}

这是什么异常?为什么我会收到此异常?

What exception is this? Why do I get this exception?

我在Tomcat的context.xml中添加了以下标签:

I have added the following tag in context.xml of Tomcat :

<Resource name="jdbc/PollDatasource" auth="Container" type="javax.sql.DataSource"
driverClassName="org.apache.derby.jdbc.EmbeddedDriver"
url="jdbc:derby://localhost:1527/poll_database;create=true"
username="suhail" password="suhail"
maxActive="20" maxIdle="10" maxWait="-1" />

web.xml 中:

<resource-ref>
  <description>my connection</description>
  <res-ref-name>jdbc/PollDatasource</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref>

我哪里出错了?

显示数据库 URL 的图像..

注意:在@Bryan Pendleton 回答之后,我将驱动程序更改为 org.apache.derby.jdbc.ClientDriver 但我得到了相同的异常.

NOTE : After the answer by @Bryan Pendleton I changed the driver to org.apache.derby.jdbc.ClientDriver but I get the same exception.

推荐答案

我看不出有什么明显的错误,但也许不同的方法可以帮助您调试它?

I can't see anything obviously wrong, but perhaps a different approach might help you debug it?

您可以尝试在每个应用程序上下文中指定您的数据源,而不是在全局 tomcat 中指定数据源.

You could try specify your datasource in the per-application-context instead of the global tomcat one.

您可以通过创建一个 src/main/webapp/META-INF/context.xml(我假设您使用的是标准的 maven 目录结构 - 如果没有,那么 META-INF 文件夹应该是一个WEB-INF 目录的兄弟).META-INF/context.xml 文件的内容类似于:

You can do this by creating a src/main/webapp/META-INF/context.xml (I'm assuming you're using the standard maven directory structure - if not, then the META-INF folder should be a sibling of your WEB-INF directory). The contents of the META-INF/context.xml file would look something like:

<?xml version="1.0" encoding="UTF-8"?>

<Context [optional other attributes as required]>

<Resource name="jdbc/PollDatasource" auth="Container"
          type="javax.sql.DataSource" driverClassName="org.apache.derby.jdbc.ClientDriver"
          url="jdbc:derby://localhost:1527/poll_database;create=true"
          username="suhail" password="suhail" maxActive="20" maxIdle="10" maxWait="-1"/>
</Context>

显然,路径和 docBase 需要与您的应用程序的具体细节相匹配.

Obviously the path and docBase would need to match your application's specific details.

使用这种方法,您不必在 Tomcat 的 context.xml 文件中指定数据源详细信息.不过,如果您有多个应用程序与同一个数据库通信,那么您的方法更有意义.

Using this approach, you don't have to specify the datasource details in Tomcat's context.xml file. Although, if you have multiple applications talking to the same database, then your approach makes more sense.

无论如何,试一试,看看它是否有什么不同.这可能会为我们提供有关您的方法出了什么问题的线索.

At any rate, give this a whirl and see if it makes any difference. It might give us a clue as to what is going wrong with your approach.

这篇关于无法为连接 URL 'null' 创建类""的 JDBC 驱动程序:我不理解此异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆