I.
?
%Tomcat_Home%\conf\ 下面的context.xml 在<Context>中加入下面的代码
?
<Resource name="jdbc/oracle" auth="Container" type="javax.sql.DataSource" maxActive="10" maxIdle="10" maxWait="1000" username="scott" password="tiger" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@localhost:1521:ORCL"/>
?
?
II.
?
编辑项目下的web.xml文件,在<web-app>中加入下面的代码
?
<resource-ref>
???? <description>Tomcat Data Source</description>
???? <res-ref-name>jdbc/oracle</res-ref-name>
???? <res-type>javax.sql.DataSource</res-type>
???? <res-auth>Container</res-auth>
?</resource-ref>
?
III.
?
建一个servlet测试一下:
?
Connection conn = null;
try
{
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/oracle");
conn = ds.getConnection();
if(conn != null)
{ out.println("Tomcat数据源连接成功!"); }
}
catch(Exception e)
{
e.printStackTrace();
}
?