Tomcat5.0.19與oracle8.1.7連接池配置指南
發表于:2007-07-01來源:作者:點擊數:
標簽:
作者:Junsan Jin 日期:2003-12-22 版本:1.2 2005-4-4 信箱:junsan21@126.com ; junnef21@sohu.com Blog:http://blog.csdn.net/junnef 聲明:本人保留本文的所有權利。 第一部分:準備工作: 第一步: 正確安裝 Oracle 8.1.7 數據庫 ,正確創建一個 開發
作者:Junsan Jin
日期:2003-12-22
版本:1.2 2005-4-4
信箱:junsan21@126.com ; junnef21@sohu.com
Blog:http://blog.csdn.net/junnef
聲明:本人保留本文的所有權利。
第一部分:準備工作:
第一步:
正確安裝
Oracle8.1.7
數據庫,正確創建一個
開發使用的數據庫,如當前所使用的數據庫為192.168.0.1:1521中的dbserver數據庫,用戶erp。
第二步:
正確安裝
tomcat5.0.19。
第二部分:正式配置工作
第一步:
找到數據庫的驅動程序classes12.zip包,將其重命名為classes12.jar,放到%TOMCAT_HOME%/common/lib下。
第二步:
在%TOMCAT_HOME%/webapps目錄下新建一個DBTest目錄,并在其下新建一個WEB-INF目錄。
第三步:
在%TOMCAT_HOME%/conf目錄下找到server.xml文件,找到類似如下的配置部分:
<Host name="localhost" de
bug="0" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
在與之對應的</host>標志之中加入如下配置信息:
<Context path="/DBTest" docBase="DBTest"
debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_DBTest_log." suffix=".txt"
timestamp="true"/>
<Resource name="jdbc/my
oracle" auth="Container"
type="javax.
sql.DataSource"/>
<ResourceParams name="jdbc/myoracle">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:oracle:thin:@192.168.0.1:1521:dbserver</value>
</parameter>
<parameter>
<name>username</name>
<value>erp</value>
</parameter>
<parameter>
<name>password</name>
<value>erp</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>-1</value>
</parameter>
</ResourceParams>
</Context>
第四步:
在當前DBTest的目錄下的WEB-INF文件夾下,新建一個web.xml文件,在<web-app></web-app>標記之間加入如下代碼:
<resource-ref>
<description>Oracle Datasource example</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
新建的文件需要有:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//D
TD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
說明。
完整文件如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<resource-ref>
<description>Oracle Datasource example</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
第五步:
創建一個
測試文件放到DBTest目錄下:DBTest.jsp
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%
String foo = "Not Connected";
System.out.println("###"+foo);
int bar = -1;
try{
Context initContext = new InitialContext();
System.out.println("###" + initContext);
Context envContext = (Context)initContext.lookup("java:/comp/env");
System.out.println("###"+ envContext);
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
System.out.println("###" + ds);
if (ds != null) {
Connection conn = ds.getConnection();
System.out.println("###" + conn);
if(conn != null) {
foo = "Got Connection "+conn.toString();
Statement stmt = conn.createStatement();
ResultSet rst =
stmt.executeQuery(
"select * from
clearcase/" target="_blank" >cc_tab_kucun");// cc_tab_kucun可以是任意一個表名
if(rst.next()) {
foo=rst.getString(1);
//bar=rst.getInt(3);
}
conn.close();
}
}
}catch(Exception e) {
e.printStackTrace();
}
%>
<html>
<head>
<title>DB Test</title>
</head>
<body>
<h2>Results</h2>
Foo <%= foo %><br/>
Bar <%=bar %>
</body>
</html>
在IE中輸入http://localhost:8080/DBTest/DBTest.jsp,如果正常運行顯示類似如下所示:
Results
Foo 3980Bar –1
則說明成功。
原文轉自:http://www.anti-gravitydesign.com