* 1.context.xml
* 2.hrp.xml中配置
1.配置tomcat安装目录conf/context.xml文件
<Context>
<Resource
name="jdbc/message" auth="Container" //name指的是指定Resource的JNDI的名字
type="javax.sql.DataSource"
maxActive="100" //maxActive表示处于活动状态的数据库连接的最大数目,取0表示不受限制,
maxIdle="30" //maxIdle表示闲置的最大数目
maxWait="10000"//数据库连接处于空闲状态的等待时间,取-1表示可以无限制等待
username="huyao"
password="123"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;DatabaseName=message" />
</Context>
2.配置所在项目里面的webroot/web-info/web.xml文件
<resource-ref>
<description>message DataSource</description>//对所引用的资源的说明
<res-ref-name>jdbc/message</res-ref-name> //指定所引用的资源的JNDI的名字,与<Resource>中的那么对应
<res-type>javax.sql.DataSource</res-type> //指定所引用的资源的类名字,与<Resource>中的type对应
<res-auth>Container</res-auth> //指定管理所引用资源的Manager,与<Resource>中的auth对应
</resource-ref>
3.最后在dao中建立connection
Context ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/message"); //java:comp/env/前缀加上数据源名称
Connection conn=ds.getConnection();
* 2.hrp.xml中配置
1.tomcat中的配置文件(tomcat\conf\catalina\localhost\hrp.xml)
<?xml version='1.0' encoding='utf-8'?>
<Context displayName="hrp" docBase="E:\workspace_weiquan\hrp_weiquan\web\hrp" path="/hrp"
reloadable="true" workDir="E:\workspace_weiquan\hrp_weiquan\work">
<Resource name="jdbc/gs"
auth="Container"
type="javax.sql.DataSource"
maxActive="20"
maxIdle="30"
maxWait="5000"
username="sa"
password="12345"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;selectMethod=cursor;databaseName=HRP_ACT">
</Resource>
</Context>
2.各项属性说明
name:表示你的连接池的名称也就是你要访问连接池的地址
auth:是连接池管理权属性
Container表示容器管理
type:是对象的类型
driverClassName:是数据库驱动的名称
url:是数据库的地址
username:是登陆数据库的用户名
password:是登陆数据库的密码
maxIdle,最大空闲数,数据库连接的最大空闲时间。超过空闲时间,数据库连
接将被标记为不可用,然后被释放。设为0表示无限制。
MaxActive,连接池的最大数据库连接数。设为0表示无限制。
maxWait ,最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示
无限制。
3.建立连接(WEB-INF\web.xml)
<resource-ref>
<description>Allow</description>
<res-ref-name>jdbc/gs</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
4.各项属性说明
description: 对所引用的资源的说明
res-ref-name:指定所引用的资源的JNDI的名字,与<Resource>中的那么对应
res-type:指定所引用的资源的类名字,与<Resource>中的type对应
res-auth:指定管理所引用资源的Manager,与<Resource>中的auth对应
5.dao中建立connection
Context ctx=new InitialContext();
DataSource source=(DataSource)ctx.lookup("java:comp/env/jdbc/gs");
Connection conn=source.getConnection();