oracle tomcat配置
修改../conf/content.xml
<?xml version='1.0' encoding='utf-8'?>
<!-- The contents of this file will be 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
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<Resource name="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>
并将ojdbc6.jar拷贝到tomcat的lib目录下,以上则配置好了Oracle数据源。
在使用时需要在web.xml中加入:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns: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>
<!-- 在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>
实例:
index.jsp
按 Ctrl+C 复制代码
<%@ 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 = new InitialContext();
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("<h1>" + rs.getString(2) + "</h1>");
}
rs.close();
st.close();
con.close();
%>
</body>
按 Ctrl+C 复制代码
浏览器访问:
转载请注明出处,谢谢