TOMCATE 数据库连接池

数据库连接池的几种方法

首先安装TOMCAT MYSQL

将MYSQL的驱动拷贝到${TOMCAT}/COMMAN/lib/目录下

新建一个WEB工程,在工程中加index.jsp ,WEB-INF,META-INF

<%@ page import="java.util.*,javax.naming.*,java.sql.*,javax.sql.* "%>

<%@ page contentType="text/html;charset=gb2312"%>

<%

Context ctx=new InitialContext();

String strLookUp="java:comp/env/jdbc/test";

DataSource ds=(DataSource)ctx.lookup(strLookUp);

Connection con=ds.getConnection();

if(con!=null)

{out.print("success");}

esle{out.print("false");}

%>

web.xml中加入

<resource-ref>

<res-ref-name>jdbc/test</res-ref-name>

<res-ref-type>javax.sql.DataSource<res-ref-type>

<res-auth>container</res-auth>

<res-sharing-scope>Shareable</res-sharing-scope>

<resource-ref>

配置tomcat
这一步的目的就是告诉tomcat如何连接数据库
可以分为两种大的类型,每种类型又有很多种配置方式
配置类型一;
(直接配置的类型,这种方式最简单)

方法一:
直接在tomcat_home/conf/localhost/下建立一个xml文件,文件名是<yourAppName>.xml
例如我的工程名叫jndi,对应的名字叫jdni.xml
内容如下:


<Context>
 <Resource
          name="jdbc/test"
          type="javax.sql.DataSource"
          password="bb"
          driverClassName="com.mysql.jdbc.Driver"
          maxIdle="2"
          maxWait="50"
          username="root"
          url="jdbc:mysql://localhost:3306/test"
          maxActive="4"/>
</Context>
方法二:
只需在tomcat_home/webapps/myapps/META-INF/context.xml中增加:
<context>
<Resource
          name="jdbc/test"
          type="javax.sql.DataSource"
          password="bb"
          driverClassName="com.mysql.jdbc.Driver"
          maxIdle="2"
          maxWait="50"
          username="root"
          url="jdbc:mysql://localhost:3306/test"
          maxActive="4"/>
</context>

说明:这种配置需要告诉tomcat resource的内容,resource应用于什么地方
第一种方法通过文件名知道了app的name
第二种方式本身就在app内部,所以name肯定知道
两种方式都要放在context中

配置类型二:
(配置全局resource,然后通过resourcelink来映射)

步骤一:配置全局resource(这一步对于所有的配置都是一样的)
打开tomcat_home/conf/server.xml加入

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

步骤二:映射
(映射可以配置在多个地方,也就有多个配置方法:)

方法一:(对比类型一的配置理解)
直接在tomcat_home/conf/localhost/下建立一个xml文件,文件名是<yourAppName>.xml
例如我的工程名叫jndi,对应的名字叫jdni.xml加入如下内容

<Context>
<ResourceLink global="jdbc/test" name="jdbc/test" type="javax.sql.DataSource"/>
</Context>
方法二:(对比类型一的配置理解)
在tomcat_home/webapps/myapps/META-INF/context.xml的Context中增加:
<context>
<ResourceLink global="jdbc/test" name="jdbc/test" type="javax.sql.DataSource"/>
</context>

方法三:(上边两种方法都是把全局的resource 映射给jndi这个web应用,第三种方法就是把这个
全局的resource直接公开给所有的应用)
在tomcat_home/conf/context.xml的<Context></context>之间加入
<ResourceLink global="jdbc/test" name="jdbc/test" type="javax.sql.DataSource"/>

运行测试:
打开ie,输入http://localhost:8080/jndi/index.jsp
看到success

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值