struts2使用数据库连接池

1

配tomcat

在conf文件夹下server.xml文件

<GlobalNamingResources>   

<Resource name="jdbc/mysql" auth="Container" 

type="javax.sql.DataSource" 

driverClassName="com.mysql.jdbc.Driver" 

url="jdbc:mysql://192.168.1.203:3306/mysql?useUnicode=true&amp;characterEncoding=gbk" username="spider" password="spider" 

maxActive="5" maxIdle="3" maxWait="-1" />

同时

context.xml文件

<ResourceLink global="jdbc/mysql" name="jdbc/mysql" type="javax.sql.DataSource"/>

 

2

工程自己配置文件

META-INF文件夹下context.xml

内容<Context>

<Resource name="jdbc/mysql" auth="Container" 

type="javax.sql.DataSource" 

driverClassName="com.mysql.jdbc.Driver" 

url="jdbc:mysql://192.168.1.203:3306/mysql?useUnicode=true&amp;characterEncoding=gbk" username="spider" password="spider" 

maxActive="5" maxIdle="3" maxWait="-1" />

</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>

</web-app>

 

以上2种方法各有优劣,推荐第二种,在java代码中即可调用连接池了:

try {

   Context env = (Context) new InitialContext().lookup("java:comp/env");

   DataSource ds = (DataSource) env.lookup("jdbc/mysql");

   Connection con = null;

   if (ds == null)

System.err.println("[DbPool]no pool");

else

con = ds.getConnection();

} catch (NamingException ne) {

   ne.printStackTrace();

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值