tomcat配置数据源案例

来源:互联网 发布:网络推编辑 编辑:程序博客网 时间:2024/05/02 02:35

tomcat配置数据源要比webilogic配置数据源要麻烦一点。详情如下

1、我们先建一个web项目,结构如下,项目名:webLogic_Test


2、index.jsp文件内容如下

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" %>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
   InitialContext ctx = new InitialContext();
   DataSource ds = (DataSource) ctx.lookup("
java:comp/env/ksacct");
   Connection conn = ds.getConnection();
   Statement sm = conn.createStatement();
   ResultSet rs = sm.executeQuery("select * from ACTION_INFO where id=1059");
   while (rs.next()) {
    out.println("action:"+rs.getString(1));//用户id
   }
   sm.close();
   rs.close();
   conn.close();
  %>
</body>
</html>

本示例代码运用了Tomcat服务器,所以在查找时需要对名称进行定位:java:comp/env/

(如果是weblogic不需要加java:comp/env,直接ctx.lookup("ksacct");

3、配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


    <resource-ref>
        <description>Oracle Datasource ksacct</description>
        <res-ref-name>ksacct</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

    
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <session-config>
        <session-timeout>10</session-timeout>
    </session-config>
</web-app>

在web.xml文件中加入<resource-ref>(红色字体)<res-ref-name>ksacct</res-ref-name>.

ksacct名字要与tomcat配置一致。这里项目完成,接下来配置tomcat

4、server.xml文件<Host>中加入

      <Context docBase="E:\360Downloads\account\webLogic_Test" path="/webLogic_Test" reloadable="true">  
        <Resource name="ksacct" auth="Container" type="javax.sql.DataSource"  
        maxActive="100" maxIdle="30" maxWait="10000"  
        username="ylpay" password="aaa111" driverClassName="oracle.jdbc.driver.OracleDriver"  
        url="jdbc:oracle:thin:@192.168.1.183:1521:eskdb"/>  
      </Context>

我这里用的是oracle,根据自己的参数配置

docBase:项目地址

path:页面访问的名字

name:放到JNDI里的名字

type:类型与web.xml一致

其它的都懂的

5、在tomcat的lib目录里加ojdbc14.jar


weblogic配置数据源

URL:http://blog.csdn.net/q260996583/article/details/48625753

weblogic与tomcat为hibernate管理数据源

URL:





0 0