基于数据库连接池的配置

来源:互联网 发布:linux 查看系统编码 编辑:程序博客网 时间:2024/04/29 08:18

1.修改Tomcat的conf/context.xml中的配置

 

即<Context>

   、、、、、、

 

   <Resource name="jdbc/books" 

       auth="Container"type="javax.sql.DataSource"  maxActive="100"  

       maxIdle="30" maxWait="10000"   username="sa"   password="sa" 

       driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"  

       url="jdbc:sqlserver://localhost:1433;DatabaseName=books"/>

  </Context>

 

 

2.加入数据库驱动文件

把数据库驱动的.jar文件,加入到Tomcat的lib中

 

3.应用程序的web.xml文件的配置

在web.xml中配置<resource-ref>

即<resource-ref>

 

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

 

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

 

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

 

  </resource-ref>

4.修改ConnectionManager.java

即修改:public static Connection getConnection(){

try{

Context ic = new InitialContext();

DataSource source = 

(DataSource)ic.lookup("java:comp/env/jdbc/books");

             conn = source.getConnection();

 

}catch(Exception e){

e.printStackTrace();

}

return conn;

}

 

原创粉丝点击