你的位置:编程库 >> 资讯 >> WEB服务器 >> TOMCAT >> 详细内容 在线投稿

Tomcat5.5数据源配置

发布: 2008-12-04 12:44 |  作者: webmaster |   来源: 本站原创 |  查看: 11次

1. Context configuration

tomcat\conf\server.xml文件配置

<Context path="/项目名称" docBase="项目实际路径" debug="5" reloadable="true" crossContext="true">  
<Resource name="JNDI名称" auth="Container"
              type="javax.sql.DataSource"
              driverClassName="驱动程序"
              url="连接地址"
              username="用户名"
              password="密码"
              maxActive="最大连接数"
              maxIdle="最大维持数"
              maxWait="最大等待时间"/>

  </Context>

2. web.xml configuration

项目web.xml文件配置

 <resource-ref>
<description>名称(任意)</description>
<res-ref-name>JNDI名称(同上)</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

3. Code example

获取连接类

Context initContext = new InitialContext();
Context envContext  = (Context)initContext.lookup("java:/comp/env"); //"java:/comp/env"Tomcat中在上下文目录中的定位
DataSource ds = (DataSource)envContext.lookup("JNDI名称(同上)");
Connection conn = ds.getConnection();//创建Connection连接

使用数据源的好处:

能够减少数据库打开和关闭的操作次数,节约资源

 

TAG: 数据源
打印 | 收藏此页 |  推荐给好友 | 举报
上一篇 下一篇
 

评分:0

发表评论
查看全部回复【已有0位网友发表了看法】