1.需要Tomcat6.0,Oracle的JDBC驱动程序包(放在Tomcat/lib下)
2.配置tomcat下的conf下的context.xml文件,在之间添加连接池如下:
<Resource
name="jdbc/test"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@sea:1521:seadb"
username="jcrm"
password="jcrm"
maxActive="20"
maxIdle="2"
maxWait="-1"/>
3.配置你的应用下的web.xml中的之间加入:
xml 代码
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
4.大功告成,不用在原来的server.xml里面配置了,下面就可以编写测试程序了,这个网上就很多了,主要的就上面,当然要把连接驱动程序都放到tomcat6下的lib下面.测试代码如下:
java 代码
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"
"http://www.w3.org/TR/REC-html40/strict.dtd">
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page session="false" %>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<%
out.print("MyTest");
DataSource ds = null;
try{
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
ds = (DataSource) envCtx.lookup("jdbc/test");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
String strSql = " select * from dual";
ResultSet rs = stmt.executeQuery(strSql);
while(rs.next()){
out.println("<br/>");
out.print(rs.getString(1));
out.println("<br/>");
out.println("Test Success");
out.println("<br/>");
}
}
catch(Exception ex){
out.print("Exception is :"+ex.getMessage());
ex.printStackTrace();
}
%>