1复制 ojdbc14.jar
Tomcat目录:C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib
WebAPP目录:WebContent\WEB-INF\lib
2新建context.xml
WebAPP目录:WebContent\META-INF
<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true">
<Resource name="jdbc/cop"
type="javax.sql.DataSource"
password="apps"
driverClassName="oracle.jdbc.driver.OracleDriver"
maxIdle="30" minIdle="2" maxWait="60000" maxActive="1000"
testOnBorrow="true" testWhileIdle="true" validationQuery="select 1 from dual"
username="apps" url="jdbc:oracle:thin:@192.168.0.208:1526:SHAND"/>
</Context>
3 修改web.xml
WebApp目录:WebContent\WEB-INF
在</web-app>之前添加
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/cop</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
4 修改server.xml
C:\Program Files\Apache Software Foundation\Tomcat 6.0\conf
在<GlobalNamingResources>之间添加
<Resource name="jdbc/cop"
type="javax.sql.DataSource"
password="apps"
driverClassName="oracle.jdbc.driver.OracleDriver"
maxIdle="30" minIdle="2" maxWait="60000" maxActive="1000"
testOnBorrow="true" testWhileIdle="true" validationQuery="select 1 from dual"
username="apps" url="jdbc:oracle:thin:@192.168.0.208:1526:SHAND"/>
在<Host>之间添加
<Context docBase="D:\oracle\Java\workspace\Test\WebContent"
path="/test" reloadable="true" source="org.eclipse.jst.j2ee.server:test">
<ResourceLink global="jdbc/cop" name="jdbc/cop" type="javax.sql.DataSource"/>
</Context>
5 测试通过
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.sql.DataSource"%>
<%@ page import="javax.naming.InitialContext"%>
<%@ page import="java.sql.*"%>
<%@ page import="oracle.jdbc.*"%>
<%
DataSource ds = null;
Connection cn = null;
Statement stmt = null;
ResultSet rst = null;
try{
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
ds = (DataSource)envCtx.lookup("jdbc/cop");//这个是你的数据库连接池在Tomcat安装目录下:
//D:\Tomcat 5.0\conf\Catalina\localhost(我的是ui,ui.xml)
if(ds!=null)
{
out.println("Connection is OK!");
cn=ds.getConnection();
if(cn!=null)
{
out.println("cn is Ok!");
stmt = cn.createStatement();
rst = stmt.executeQuery("select sbc.sbc from secom.secom_sbc_lists sbc WHERE ROWNUM<10 ");//查询语句
out.println("<p>rst is Ok!" + rst.next());
int j=1;
while(rst.next())
{
out.println("<P>BOOK_CODE:" + rst.getString(1));
}
cn.close();
}
else
{
out.println("rst Fail!");
}
}
else
out.println("Fail!");
}
catch(Exception ne)
{
out.println(ne);//racle.jdbc.driver.OracleDriver
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
</body>
</html>