Tomcat7配置数据源(Oracle)

时间:2014-05-26 13:55:00   收藏:0   阅读:365

修改../conf/content.xml

bubuko.com,布布扣
<?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>
bubuko.com,布布扣

并将ojdbc6.jar拷贝到tomcat的lib目录下,以上则配置好了Oracle数据源。

在使用时需要在web.xml中加入:

bubuko.com,布布扣
<?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>
bubuko.com,布布扣

实例:

index.jsp

bubuko.com,布布扣
<%@ 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>
bubuko.com,布布扣

浏览器访问:

 bubuko.com,布布扣

Tomcat7配置数据源(Oracle),布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!