数据库连接池

实现数据库连接池步骤如下:

1.配置/Tomcat安装目录/conf/context.xml文件。

<Resource name="jdbc/news" auth="Container" type="javax.sql.DataSource"
  maxActive="100" maxIdle="10" maxWait="10000" username="root"
  password="root" driverClassName="com.mysql.jdbc.Driver"
  url="jdbc:mysql://localhost:3306/NewsManagerSystem?useUnicode=true&amp;characterEncoding=utf-8"
 />

2.配置/webRoot/WEB-INF/web.xml文件。

  <resource-ref>
   <description>news DataSource</description>
   <res-ref-name>jdbc/news</res-ref-name>
   <res-type>javax.sql.DataSource</res-type>
   <res-auth>Container</res-auth>
  </resource-ref>

3.将数据可驱动的jar文件复制到Tomcat安装目录下的lib目录中。

4.实现查找数据源。

   Context ctx = new InitialContext();
   DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/news");
    conn = ds.getConnection();

posted @ 2018-01-09 20:18  kaluse天蝎  阅读(199)  评论(0编辑  收藏  举报