用JNDI实现数据库连接池

来源:互联网 发布:三砖淘宝店铺 编辑:程序博客网 时间:2024/06/08 15:05

JNDI实现数据库连接池

 

1.配置数据源(找到tomcat安装目录->conf->context.xml

<Resource

name="students"

type="javax.sql.DataSource"

auth="Container"            

maxActive="100"

maxIdle="30"

maxWait="10000"

username="root"

         password="123"

  url="jdbc:mysql://localhost:3306/students"

driverClassName="com.mysql.jdbc.Driver"

/>

2.在项目中引用数据源(项目的web.xml

  <resource-ref>

   <res-ref-name>students</res-ref-name>

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

   <res-auth>Container</res-auth>

  </resource-ref>

 

*第二步的name要与第一步的name相同

 

3.获得连接

Context ct = new InitialContext();

DataSource ds= (DataSource) ct.lookup("java:comp/env/students");

Connection conn=ds.getConnection();

原创粉丝点击