Tomcat6.0中配置数据源

本文介绍如何在Tomcat6.0中配置SQLServer2005数据源,包括添加JAR包、修改server.xml及web.xml文件的具体步骤,并提供测试方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tomcat6.0中配置数据源
 

利用数据源完成数据连接.可以提高程序访问速度,在前面讲Struts时,曾讲过数据源在Struts中的配置方法,今天配置的数据源中在tomcat6.0中完成的,由于不同的Tomcat版本,数据源的配置方式不同,所以在配置时参数Tomcat官方网站。

http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html

本例使用的是SQL Server2005做的案例:

具体步骤:

1. 把数据源连接相关的JAR包复制到Tomcat的lib,( $CATALINA_HOME/lib/)。

sqljdbc.jar

tomcat-dbcp.jar (已包含在Tomcat中)

2. 在$CATALINA_HOME/conf中找到服务器配置文件server.xml,建议大家在修改之前先备份一个,一旦改乱了,还可以复制回来!

3. 在server.xml中找到<Host>元素,在<Host>中添加上下文元素<Context>,注意EmployeeWeb是上下文名称。

4. 在<Context>中添加<Resource>元素,其中name属性为数据源名称,其他属性自己分析。修改结束后保存退出。
      <Host name="localhost" appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">

     <Context path="/EmployeeWeb" docBase="EmployeeWeb"
        debug="5" reloadable="true" crossContext="true">

     <Resource name="jdbc/onlineShopDataSource" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="sa" password="123456"
               driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
               url="jdbc:sqlserver://localhost:1433;databaseName=onlineShop"/>

</Context>
      </Host>

5. 打开项目EmployeeWeb,在web.xml中添加配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
    <servlet-name>DepartmentController</servlet-name>
    <servlet-class>cn.sunfengwei.employee.controller.DepartmentController</servlet-class>
</servlet>
<servlet>
    <servlet-name>EmployeeController</servlet-name>
    <servlet-class>cn.sunfengwei.employee.controller.EmployeeController</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>DepartmentController</servlet-name>
    <url-pattern>/department</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>EmployeeController</servlet-name>
    <url-pattern>/EmployeeController</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/onlineShopDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>

6. 测试:

6.1 在jsp中测试,这里为了省事,使用JSP的标准标记库与表达式语言。

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<sql:query var="rs" dataSource="jdbc/onlineShopDataSource">
select id,name from department
</sql:query>

<html>
<head>
    <title>DB Test</title>
</head>
<body>

<h2>Results</h2>
<select name="departmentId">
<c:forEach var="row" items="${rs.rows}">
   
    <option value="${row.id}">${row.name}</option>
  
</c:forEach>
</select>
</body>
</html>

6.2 在JavaBean中完成数据源连接,但有一点大家一定要注意,不能客户端使用本地方式运行数据源,因为我们是在Tomcat中配置的,只能在服务器中运行。

package cn.sunfengwei.employee.database;
/**
* @author 孙丰伟 E-mail: sunfengweimail@163.com
* @version 创建时间:Jun 11, 2008 6:19:20 PM
* @see
*/
import java.sql.*;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class ConnectDB {
private static Connection conn=null;

//
// 使用本地连接
//
public static Connection getConnection()
{
  
   String url="jdbc:sqlserver://localhost:1433;databaseName=onlineShop";
   try {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    conn=DriverManager.getConnection(url,"sa","123456");
   } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   return conn;
  
}

//
// 使用数据源连接
//


public static Connection getConnectionByDataSource() throws Exception
{
    
   InitialContext cxt;
   try {
    cxt = new InitialContext();
    if ( cxt == null ) {
       throw new Exception("Uh oh -- no context!");
     }

     DataSource ds = (DataSource) cxt.lookup( "java:/comp/env/jdbc/onlineShopDataSource" );
     conn=ds.getConnection();
     if ( ds == null ) {
       throw new Exception("Data source not found!");
     }
   } catch (NamingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   return conn;

}
}

7. 在数据访问对象DAO中,与原来的内容相同,这里不再重复。

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值