要使用tomcat作为服务器连接到derby数据库,我将以下内容添加到Tomcat的conf/context.xml
:
<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'
我还没有理解这个例外的原因。
异常后,我从Tomcat的Resource
中删除了conf/context.xml
标记,并将其放在我项目的META-INF/context.xml
中。
当我再次尝试使用servlet时,它没有任何异常!
当我将Resource
标记放在全局context.xml文件(即在conf / context.xml中)但是没有得到异常时,我可能会出现异常的原因将它放在特定于我的应用程序的context.xml中的异常? (即在META-INF / context.xml内)
答案 0 :(得分:0)
将<Resource>
添加到conf/context.xml
将为您在服务器上部署的每个Web应用程序提供该资源的副本 - 可能不是您想要做的。如果您希望<Resource>
全球可用,则将conf/server.xml
放在<GlobalNamiongResources>
下更合适。
我希望你得到的错误是因为你没有把JDBC驱动程序放在正确的位置。在<Resource>
中定义conf/context.xml
可能会导致使用不同的ClassLoader来加载<Resource>
。