tomcat数据源配置终极方案

测试平台:tomcat5.5.20

步骤:

1、打开%CATALINA_HOME%/conf/server.xml,在GlobalNamingResources中间增加如下内容(红色字体部分)

  <GlobalNamingResources>
    <!-- Test entry for demonstration purposes -->
    <Environment name="simpleValue" type="java.lang.Integer" value="30"/>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users -->
    <Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
    <Resource auth="Container" driverClassName="com.ibm.db2.jcc.DB2Driver" maxActive="100" maxIdle="30" maxWait="5000" name="jdbc/police" password="111111" scope="Shareable" type="javax.sql.DataSource" url="jdbc:db2://192.168.5.251:50000/BZ" username="db2admin"/><!--针对db2数据库-->
    <Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" maxActive="100" maxIdle="30" maxWait="5000" name="jdbc/lt" password="123456" scope="Shareable" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/police" username="root"/><!--针对mysql-->
    <Resource auth="Container" driverClassName="oracle.jdbc.OracleDriver" maxActive="100" maxIdle="30" maxWait="5000" name="jdbc/gsj" password="111111" scope="Shareable" type="javax.sql.DataSource" url="jdbc:oracle:thin:@localhost:1521:oracle9" username="simon"/><!--针对oracle-->

  </GlobalNamingResources>

2、打开%CATALINA_HOME%/conf/context.xml,增加如下内容(红色字体部分)

<!-- 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="" />
    -->
    <ResourceLink name="jdbc/police" global="jdbc/police" type="javax.sql.DataSourcer"/>
    <ResourceLink name="jdbc/lt" global="jdbc/lt" type="javax.sql.DataSourcer"/>
    <ResourceLink name="jdbc/gsj" global="jdbc/gsj" type="javax.sql.DataSourcer"/>
</Context>

3、将相应数据库的jdbc驱动拷贝到%CATALINA_HOME%/common/lib下

4、打开你自己WEB-INF目录下的web.xml文件,增加如下内容:

  <resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/gsj</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref> 

5、建立你自己的数据库连接类,提供源程序如下:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
/**
   类介绍:数据库连接类
   开发人员:xjxhd
   创建日期:
   修改日期:
 */

public class ConnDataBase {

   static Logger logger =Logger.getRootLogger();
   public Connection getConnection()
   {

   Connection conn=null;
   Context ctx=null;
   DataSource ds=null;
   try {

        ctx=new InitialContext();
        ds=(DataSource)ctx.lookup("java:comp/env/jdbc/gsj");
       logger.debug("datapools initial success!");

   } catch (NamingException ex1) {
       logger.debug("datapools initial fails!");
       logger.debug(ex1.getMessage());
       ex1.printStackTrace();
   }

   try {
       conn = ds.getConnection();
       logger.debug("database connect success!");
   } catch (SQLException ex) {
       logger.debug("database connect fails!");
       logger.debug(ex.getMessage());
       ex.printStackTrace();
   }
     return conn;
    }
注意红色字体部分,必须要和web.xml中的res-ref-name内容对应。

上述关于tomcat数据源的配置过程不会有任何问题,如果还配置不出来,那就找找你自己的问题

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值