Configure the JNDI DataSource in Tomcat by adding a declaration for your resource to $CATALINA_HOME/conf/server.xml
.
Add this in between the </Context>
tag of the examples context and the </Host>
tag closing the localhost definition
<Context path="/wfms" docBase="wfms" debug= "99" reloadable="true" crossContext="true">
<Resource name="jdbc/epmp" type="javax.sql.DataSource" auth="Container"/>
<ResourceParams name="jdbc/epmp">
<parameter>
<name>username</name>
<value>demo4bt</value>
</parameter>
<parameter>
<name>password</name>
<value>demo4bt</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:oracle:thin:@192.168.33.244:1522:db10gr2</value>
</parameter>
</ResourceParams>
</Context>
一般在web.xml中还要加入如下代码:
<resource-ref>
<res-ref-name>jdbc/epmp</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
然后写程序测试,
Context initCtx = new InitialContext();
DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/epmp");
conn = ds.getConnection();
一般这个就ok了,可是我的测试没有通过提示错误:关于 "com.sun.jndi.fscontext.RefFSContextFactory"
在jre lib中jndi下找了找,没有发现,网上搜索了一番,原来还有一个fscontext.jar 还要自己下载引
入。为什么在JRE System Lib的com.sun.jndi.下没有呢,看来要问Sun了?