前两天在tomcat配置jndi上花费了挺长时间,后来才发现原来很简单,只是自己在获取Connection连接时方法不对,汗~~
1.在server.xml中的配置:
<GlobalNamingResources>
<Resource
auth="Container"
description="User database that can be updated and saved"
name="UserDatabase"
type="org.apache.catalina.UserDatabase"
pathname="conf/tomcat-users.xml"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"/>
</GlobalNamingResources>
<Host appBase="webapps" autoDeploy="flase" name="localhost" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false">
<Context
docBase="E:\workspace\**\webapp"
path="/echannel"
reloadable="true"
debug="5"
crossContext="true">
<Resource
name="jdbc/ds0"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
maxIdle="2"
maxWait="5000"
username="test"
password="test"
url="jdbc:oracle:thin:@192.168.1.103:1521:orcl"
maxActive="4"/>
<Resource
name="jdbc/ds1"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
maxIdle="2"
maxWait="5000"
username="test1"
password="test1"
url="jdbc:oracle:thin:@192.168.1.104:1521:orcl"
maxActive="4"/>
</Context>
</Host>
2.页面中获取连接:
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/ReportTest");
Conection conn = ds.getConnection();