java同时配置多个数据库连接池

java配置数据库连接池以mysql和sqlserver为例

配置tomcat下面的content.xml

 

<!-- The contents of this file will be loaded for each web application -->
<Context>
<Resource name="jdbc/mysql"   
       auth="Container"   
           
       type="javax.sql.DataSource"   
       driverClassName="com.mysql.jdbc.Driver"     
	   url="jdbc:mysql://localhost:3306/com?useUnicode=true&amp;characterEncoding=UTF-8"
       username="root"   
       password="root"   
       maxActive="100"   
       maxIdle="2"   
       maxWait="10000" />
      <ResourceParams name="jdbc/mysql">
         <parameter>
         <name>removeAbandoned</name>
   	 <value>true</value>
      </parameter>
      <parameter>
        <name>removeAbandonedTimeout</name>
        <value>60</value>
      </parameter>
    <parameter>
       <name>logAbandoned</name>
    <value>true</value>
    </parameter>
   </ResourceParams>
  <Resource name="jdbc/sqlserver"   
       auth="Container"
	   type="javax.sql.DataSource"   
	   driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"   
	   url="jdbc:microsoft:sqlserver://localhost:1433;databaseName=fashion"   
	   username="sa"   
	   password=""   
	   maxActive="100"   
	   maxIdle="30"   
	   maxWait="10000" />
      <ResourceParams name="jdbc/sqlserver">
         <parameter>
         <name>removeAbandoned</name>
   	 <value>true</value>
      </parameter>
      <parameter>
        <name>removeAbandonedTimeout</name>
        <value>60</value>
      </parameter>
    <parameter>
       <name>logAbandoned</name>
    <value>true</value>
    </parameter>
   </ResourceParams>-->
    <!-- Default set of monitored resources -->
 <!--   <WatchedResource>WEB-INF/web.xml</WatchedResource>-->
	<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" />
    -->
</Context>

 

 设置项目目录下面的WEB-INF下面的web.xml文件 在里面加入

 

	<!-- 数据源 -->
     <resource-ref>
  	    <description>DB Connection</description>   
    	<res-ref-name>jdbc/mysql</res-ref-name>   
    	<res-type>javax.sql.DataSource</res-type>   
    	<res-auth>Container</res-auth>   
 	 </resource-ref>
 	 <resource-ref>
  	    <description>DB</description>   
    	<res-ref-name>jdbc/sqlserver</res-ref-name>   
    	<res-type>javax.sql.DataSource</res-type>   
    	<res-auth>Container</res-auth>   
 	 </resource-ref>

测试文件

 

<%@ page contentType="text/html;charset=UTF-8" import="com.shop.util.*"%>
<%@ page import="java.sql.*"%>    
<%@ page import="javax.sql.*"%>     
<%@ page import="javax.naming.*"%> 
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
DataSource ds = null;
InitialContext ctx=new InitialContext();    
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/sqlserver");    
Connection conn = ds.getConnection();   
%>
		  		<%
	Statement stmt=conn.createStatement();
	String sql_gun="select * from sm_t_salesheet" ;
	ResultSet rs_gun=stmt.executeQuery(sql_gun);
 	while(rs_gun.next())
  	{
	%>
		  		<%=rs_gun.getString("sheet_no")%>
	<%
	 }
	 rs_gun.close();
	 stmt.close();
	 %>
	
<%conn.close(); %>

<%
DataSource ds2 = null;
InitialContext ctx2=new InitialContext();    
ds2=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");    
Connection conn2 = ds2.getConnection();   
%>
	<%
	Statement stmt2=conn2.createStatement();
	String sql_gun2="select * from sm_t_salesheet" ;
	ResultSet rs_gun2=stmt2.executeQuery(sql_gun2);
 	while(rs_gun2.next())
  	{
	%>
		  		<%=rs_gun2.getString("sheet_no")%>
	<%
	 }
	 rs_gun2.close();
	 stmt2.close();
	 %>
	
<%conn2.close(); %>

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值