tomcat里的D:\tomcat5.5.15\conf\Catalina\localhost文件夹下.写上这样的xml文件,内容如下.
<?xml version="1.0" encoding="UTF-8"?>
<Context
docBase="工程路径\WebRoot"
path="jxhealth"
reloadable="true">
<Resource
name="jdbc/oracle"
auth="Container"
type="javax.sql.DataSource"
maxActive="20"
maxIdle="10"
maxWait="5000"
username="test"
password="test"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@192.168.0.80:1521:JXSMJK" />
</Context>
web.xml加上这样一句话.
<!-- 需要引用的JNDI资源 -->
<resource-ref>
<res-ref-name>jdbc/oracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
java文件里这样写
public class DBConnection {
private static String JNDI_NAME="jdbc/plantDB";
public DBConnection(){
}
public Connection getConnection(){
Connection cn=null;
try{
InitialContext initTxt=new InitialContext();
//Context cTxt=(Context)initTxt.lookup("java:comp/env");
//Object obj=(Object)cTxt.lookup(JNDI_NAME);
//DataSource ds=(DataSource)obj;
DataSource ds=(DataSource)initTxt.lookup("java:comp/env/"+JNDI_NAME);
cn=ds.getConnection();
//cn.setAutoCommit(true);
}catch(Exception E){
String strMsg=E.getMessage().toString();
System.out.print(strMsg);
}
return cn;
}
}