在Tomcat中配配置数据源汇总

Tomcat本身不具备处理提供数据源的能力。借助于一些开源数据源实现,如:DBCP和C3P0等。

一、在http://commons.apache.org/可下载这些是Tomcat提供配置数据源所需的类库。
注意:Tomcat5.5以上标准版本自带dbcp,放在$Tomcat\common\lib目录;

下载三个文件后,将三个文件解压到Tomcat/common/lib,
1.The DBCP Component 
commons-dbcp-1.2.1.jar
http://commons.apache.org/dbcp/

2.The Pool Component 1.2.jar
commons-pool-1.2.jar
http://commons.apache.org/pool/

3.Commons Collections 3.1
commons-collections-3.1.jar
http://commons.apache.org/collections/


There are several examples  of using DBCP available.
http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/doc/

4、把数据库JDBC驱动拷贝到%TOMCAT_HOME%/common/lib和
                           %TOMCAT_HOME%/webapps/yourweb/WEB-INF/lib下(我的web文件夹名字叫quickstart)

二、

    1.  SQL server2000
 <Resource 
name="jdbc/quickstart" 
type="javax.sql.DataSource" 
password="123456" 
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver" 
maxIdle="2" 
maxWait="5000" 
username="sa" 
url="jdbc:microsoft:sqlserver://localhost;DatabaseName=quickstart"   maxActive="4"/>


 2.MySQL
     <Resource    
                 name="jdbc/hnport" 
                type="javax.sql.DataSource" 
                password="sharker" 
                driverClassName="com.mysql.jdbc.Driver" 
                maxIdle="2" 
                maxWait="5000" 
                username="root" 
                url="jdbc:mysql://localhost:3306/hnport" 
                maxActive="4"/>


三、按数据源使用范围来分数据源配置两个方法:

方法一:所用Web可使用(全局数据源)
  
  1、修改%TOMCAT_HOME%/conf/server.xml文件,在<GlobalNamingResources></GlobalNamingResources>之间加入如下代码:
              < Resource    
                 name ="jdbc/hnport" 
                 type ="javax.sql.DataSource" 
                 password ="sharker" 
                 driverClassName ="com.mysql.jdbc.Driver" 
                 maxIdle ="2" 
                 maxWait ="5000" 
                 username ="root" 
                 url ="jdbc:mysql://localhost:3306/hnport" 
                 maxActive ="4" />

  2、修改%TOMCAT_HOME%/webapps/yourweb/WEB-INF下的web.xml文件,在<web-app></web-app>之间添加以下内容
 
   < resource-ref > 
< description >mysql Connection </ description > 
< res-ref-name >jdbc/hnport </ res-ref-name > 
< res-type >javax.sql.DataSource </ res-type > 
< res-auth >Container </ res-auth > 
</ resource-ref >


3、在%TOMCAT_HOME%/conf/Catalina/localhost下新建一个与你web文件夹同名的xml文件(我的是quickstart.xml)

这一步非常重要,如果没有这步就会出错,会出现org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'

<? xml  version ="1.0"  encoding ="UTF-8" ?> 
< Context  path ="/hnport"  reloadable ="true"  docBase ="d:\hnport\WebRoot"  workDir ="d:\hnport\work" > 
     < Resource  name ="jdbc/hnport" 
      type ="javax.sql.DataSource" 
      password ="sharker" 
      driverClassName ="com.mysql.jdbc.Driver" 
      maxIdle ="2" 
      maxWait ="5000" 
      username ="root" 
      url ="jdbc:mysql://localhost:3306/hnport" 
      maxActive ="4" /> 
</ Context > 

方法二:只有某个Web可用(局部数据源
        1.该方法不会造成系统混乱,只需修改%TOMCAT_CAT%\conf\Cattalin\localhost下的Web配置文件。如:hnport.xml

<? xml  version ="1.0"  encoding ="UTF-8" ?> 
< Context  path ="/hnport"  reloadable ="true"  docBase ="d:\hnport\WebRoot"  workDir ="d:\hnport\work" > 
     < Resource  name ="jdbc/hnport" 
      type ="javax.sql.DataSource" 
      password ="sharker" 
      driverClassName ="com.mysql.jdbc.Driver" 
      maxIdle ="2" 
      maxWait ="5000" 
      username ="root" 
      url ="jdbc:mysql://localhost:3306/hnport" 
      maxActive ="4" /> 
</ Context >



 2 、修改 %TOMCAT_HOME%/webapps/yourweb/WEB-INF 下的 web.xml 文件 , 在 <web-app> </web-app> 之间添加以下内容
  
   < resource-ref > 
< description >mysql Connection </ description > 
< res-ref-name >jdbc/hnport </ res-ref-name > 
< res-type >javax.sql.DataSource </ res-type > 
< res-auth >Container </ res-auth > 
</ resource-ref > 

<!-- [if !supportLineBreakNewLine]--> 
<!-- [endif]-->





四.测试
<%@page contentType="text/html;charset=big5"%>    
<%@page import="java.sql.*"%>    
<%@page import="javax.sql.*" %>    
<%@page import="javax.naming.*" %>    
<%    
        try {    
                Context initContext = new InitialContext();    
                Context envContext    =    
                        (Context)initContext.lookup("java:/comp/env");    
                DataSource ds =    
                        (DataSource)envContext.lookup("jdbc/hnport");    
                Connection conn = ds.getConnection();    
    
                if(!conn.isClosed())    
                         
                        out.println("数据库连接测试成功");    
                conn.close();    
        }    
        catch(SQLException e) {    
                out.println(e.toString());    
        } 
%>



本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/360048,如需转载请自行联系原作者
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值