为了使用 tomcat 作为服务器连接到 derby 数据库,我最初在conf/context.xml
Tomcat 中添加了以下内容:
<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" />
以及项目Resource-ref
的 web.xml 中的标签WEB-INF/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>
但是当我运行一个必须连接到数据库的 servlet 时,语句:
connection = dataSource.getConnection();
导致异常:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
我还不明白这个异常的原因。
在异常之后,我Resource
从 Tomcat 中删除了标签conf/context.xml
并将其放在META-INF/context.xml
我的项目中。
当我再次尝试 servlet 时,它毫无例外地工作了!
Resource
当我将标签放在全局 context.xml 文件中(即在 conf/context.xml 中)时出现异常的原因可能是什么,但是当我将标签放在特定于我的应用程序的 context.xml 中时却没有出现异常?. (即在 META-INF/context.xml 中)