Jndi 连接池

(1) 第一步  Jndi配置

在Tomcat根目录/conf/context.xml文件中<Context>节点中添加<Resource> 信息内容如下

 <!-- 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="" />    -->

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

</Context>

 将用于数据连接的架包如mssqlserver2.jar复制到Tomcat根目录的/lib 目录下    

第二步应用程序中的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>

 <res-ref-name>jdbc/bin</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></web-app>  

第三步  连接获取Connection 连接

   导包import javax.naming.Context;

   import javax.naming.InitialContext;

   import javax.sql.DataSource;  

  Context ic = new InitialContext();   DataSource source = (DataSource) ic.lookup      ("java:comp/env/jdbc/bin");   conn = source.getConnection();       

简单应用程序可到我的字样里下载 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值