JNDI在Java Web工程中的配置
第一步:配置WEB工程的WEB.XML
<resource-ref>
<description>DB Connection</description>
<res-ref-name>TEST_DATASOURCE</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
第三步:贴上我工程JNDI部分示例代码
static DataSource getDataSource(String dsName) {
DataSource ds = null;
try {
if (ServerDetector.isTomcat()) {
dsName = "java:comp/env/" + dsName;
}
ds = (DataSource)JNDIUtil.getInitialContext().lookup(dsName);
} catch (NamingException e) {
throw new DataAccessException(e);
}
if (null == ds) {
throw new DataAccessException("data source[" + dsName + "] is null");
}
return ds;
}
注意事项:
1、上述的数据源名称TEST_DATASOURCE一定要一致!
2、server.xml中配置的内容,会在myeclipse执行clean操作的时候强制执行还原server.xml内容的操作,一定要记得重新配置!