Modify.. /conf/content.xml
<?XML version= ' 1.0 ' encoding= ' utf-8 '?><!--The contents of this file is loaded for each Web application -<Context> <!--Default set of monitored resources - <Watchedresource>Web-inf/web.xml</Watchedresource> <!--uncomment this to disable session persistence across Tomcat restarts - <!--<manager pathname= ""/> - <!--uncomment this to enable Comet connection tacking (provides events in session expiration as well as WebApp Li Fecycle) - <!--<valve classname= "Org.apache.catalina.valves.CometConnectionManagerValve"/> - <Resourcename= "Jdbc/oracle"Auth= "Container"type= "Javax.sql.DataSource"Driverclassname= "Oracle.jdbc.OracleDriver"URL= "Jdbc:oracle:thin: @IP: 1521:orcl"username= "username"Password= "Password"maxactive= "5"Maxidle= "2"maxwait= "10000" /></Context>
and copy the Ojdbc6.jar to the Tomcat Lib directory, and the above is configured with the Oracle data source.
When you use it, you need to include it in Web. xml:
<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:web= "Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"ID= "webapp_id"version= "2.5"> <Display-name>Hpms</Display-name> <welcome-file-list> <Welcome-file>index.jsp</Welcome-file> </welcome-file-list>
<!--Add the following content to Web. xml<Resource-ref> <Description>Java JDBC</Description> <Res-ref-name>Jdbc/oracle</Res-ref-name> <Res-type>Javax.sql.DataSource</Res-type> <Res-auth>Container</Res-auth> </Resource-ref></Web-app>
Instance:
index.jsp
<%@ Page Language="Java"Import="java.util.*"pageencoding="UTF-8"%><%@ Page Import="java.sql.*,javax.sql.*,javax.naming.*"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML><Head><title>My JSP ' tomcattest.jsp ' starting page</title></Head><Body> <%Context CTX= NewInitialContext (); DataSource DS=(DataSource) ctx. Lookup ("java:comp/env/jdbc/oracle"); Connection Con=ds.getconnection (); Statement St=con.createstatement (); ResultSet RS=St.executequery ("SELECT * from VendorClass"); while(Rs.next ()) {out.println ("" +rs.getstring (2) + ""); } rs.close (); St.close (); Con.close (); %></Body>
Browser access: