A. 将jdbc driver放到 tomcat-5.0.28\common\lib 下 (Oracle9i的driver为classes12.jar)
unpackWARs ="true" autoDeploy ="true"
xmlValidation ="false" xmlNamespaceAware ="false" >
</ HOST >
将下面的Context信息加到<HOST>中
<!-- Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_sync4j_log." suffix=".txt" timestamp="true" / -->
< Resource name ="jdbc/appDS" auth ="Container" type ="javax.sql.DataSource" />
< ResourceParams name ="jdbc/appDS" >
< 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:@youripaddress:DBName </ value >
</ parameter >
< parameter >
< name > username </ name >
< value > username </ value >
</ parameter >
< parameter >
< name > password </ name >
< value > pwd </ value >
</ parameter >
< parameter >
< name > maxActive </ name >
< value > 20 </ value >
</ parameter >
< parameter >
< name > maxIdle </ name >
< value > 10 </ value >
</ parameter >
< parameter >
< name > maxWait </ name >
< value > 10000 </ value >
</ parameter >
</ ResourceParams >
</ Context >
< description > DB Connection </ description >
< res-ref-name > jdbc/appDS </ res-ref-name >
< res-type > javax.sql.DataSource </ res-type >
< res-auth > Container </ res-auth >
</ resource-ref >
< bean id ="dataSource" class ="org.springframework.jndi.JndiObjectFactoryBean" >
< property name ="jndiName" value ="java:comp/env/jdbc/appDS" />
</ bean >
A. 将jdbc driver放到 tomcat-5.0.28\common\lib 下 (Oracle9i的driver为classes12.jar)
B. 修改tomcat-5.0.28\conf\server.xml 配置文件
找到配置文件中的<HOST>部分
unpackWARs ="true" autoDeploy ="true"
xmlValidation ="false" xmlNamespaceAware ="false" >
</ HOST >
将下面的Context信息加到<HOST>中
< Resource name ="jdbc/appDS" scope ="Shareable" type ="javax.sql.DataSource" username ="username" password ="pwd" driverClassName ="oracle.jdbc.driver.OracleDriver" url ="jdbc:oracle:thin:@youripaddress:DBName" maxWait ="-1" maxIdle ="100" maxActive ="15" />
</ Context >
< bean id ="dataSource" class ="org.springframework.jndi.JndiObjectFactoryBean" >
< property name ="jndiName" value ="java:comp/env/jdbc/appDS" />
</ bean >
< Context >
< Resource name ="jdbc/pinyou"
type ="javax.sql.DataSource"
driverClassName ="oracle.jdbc.OracleDriver"
url ="jdbc:oracle:thin:@127.0.0.1:1521:ghyghost"
username ="sa" password ="123" maxActive ="200" maxIdle ="10"
maxWait ="-1"
/>
</ Context >
2.然后在web.xml文件中的web-app节点下加入如下代码形式:
< description > pinyou connection oracle9i </ description >
< res-ref-name > jdbc/ghyghost </ res-ref-name >
< res-type > javax.sql.DataSource </ res-type >
</ resource-ref >
3.新建一个JSP文件如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="java.sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
try {
Context envContext = (Context) new InitialContext()
.lookup("java:/comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/pinyou"); //查找配置
Connection conn = ds.getConnection();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select pyid from pyinfo");
while (rs.next()) {
out.println(rs.getString("pyid") + "<br>");
}
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
%>
</body>
</html>
4.别忘了将ORACLE的JDBC驱动放入到TOMCAT的lib目录中。
OVER!~