tomcat7连接池使用

来源:互联网 发布:乘联会销量数据suv 编辑:程序博客网 时间:2024/05/22 20:07

1 配置连接池

   将以下内容保存为context.xml,放在web项目下的META-INF文件夹下:

   

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE xml>  
  3. <Context>  
  4.     <Resource name="jdbc/test"   
  5.         auth="Container"   
  6.         type="javax.sql.DataSource"  
  7.         factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"   
  8.         maxActive="100" //最大激活连接  
  9.         maxIdle="30"   //最大空闲连接  
  10.         maxWait="1000"  //最大等待数  
  11.         username="test"  //数据库用户名  
  12.         password="testtest" //数据库密码  
  13.         driverClassName="com.mysql.jdbc.Driver" //此处使用mysql数据库,请自行修改驱动  
  14.         url="jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8" />   //test为数据库名,编码为UTF-8  
  15. </Context>  

 2 代码中引用连接池 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.hrb2c.servlet;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5. import java.sql.Connection;  
  6. import java.sql.ResultSet;  
  7. import java.sql.SQLException;  
  8. import java.sql.Statement;  
  9. import java.util.concurrent.Future;  
  10.   
  11. import javax.naming.Context;  
  12. import javax.naming.InitialContext;  
  13. import javax.naming.NamingException;  
  14. import javax.servlet.ServletException;  
  15. import javax.servlet.annotation.WebServlet;  
  16. import javax.servlet.http.HttpServlet;  
  17. import javax.servlet.http.HttpServletRequest;  
  18. import javax.servlet.http.HttpServletResponse;  
  19.   
  20. import org.apache.tomcat.jdbc.pool.DataSource;  
  21.   
  22. /** 
  23.  * 数据库连接池 
  24.  *  
  25.  * @author Yuedong Li 
  26.  *  
  27.  */  
  28.   
  29. @WebServlet("/ConnPoolTest")  
  30. public class ConnPoolTest extends HttpServlet {  
  31.     private static final long serialVersionUID = 1L;  
  32.     private Connection con = null;  
  33.     private static DataSource datasource;  
  34.   
  35.     /** 
  36.      * @see HttpServlet#HttpServlet() 
  37.      */  
  38.     public ConnPoolTest() {  
  39.         super();  
  40.         try {  
  41.             //获取数据源  
  42.             datasource = getInstance();  
  43.   
  44.             //连接池同步  
  45.             Future<Connection> future = datasource.getConnectionAsync();  
  46.             while (!future.isDone()) {  
  47.                 // 等待连接池同步  
  48.                 Thread.sleep(100);  
  49.             }  
  50.   
  51.             // 获取连接池  
  52.             con = future.get();  
  53.         } catch (Exception e) {  
  54.             e.printStackTrace();  
  55.         }  
  56.   
  57.     }  
  58.   
  59.     /** 
  60.      * 单例模式获取数据源 
  61.      * @return 
  62.      * @throws NamingException 
  63.      */  
  64.     private DataSource getInstance() throws NamingException {  
  65.         if (datasource == null) {  
  66.             Context initContext = new InitialContext();  
  67.             Context envContext = (Context) initContext.lookup("java:/comp/env");  
  68.             datasource = (DataSource) envContext.lookup("jdbc/test");  
  69.         }  
  70.         return datasource;  
  71.     }  
  72.   
  73.       
  74.     /** 
  75.      * 处理get方法 
  76.      */  
  77.     protected void doGet(HttpServletRequest request,  
  78.             HttpServletResponse response) throws ServletException, IOException {  
  79.         doPost(request, response);  
  80.     }  
  81.   
  82.     /** 
  83.      * 处理post方法 
  84.      */  
  85.     protected void doPost(HttpServletRequest request,  
  86.             HttpServletResponse response) throws ServletException, IOException {  
  87.         Statement st;  
  88.         StringBuffer buff = new StringBuffer("<html><body>");  
  89.         try {  
  90.             if (con != null) {  
  91.                 //简单查询,test表中有2列,id与name  
  92.                 buff.append("id");  
  93.                 buff.append("  ");  
  94.                 buff.append("name");  
  95.                 buff.append("<br>");  
  96.                 st = con.createStatement();  
  97.                 ResultSet rs = st.executeQuery("select * from test");  
  98.                 while (rs.next()) {  
  99.                     buff.append(rs.getString("id"));  
  100.                     buff.append("  ");  
  101.                     buff.append(rs.getString("name"));  
  102.                     buff.append("<br>");  
  103.                 }  
  104.             }  
  105.             buff.append("</body></html>");  
  106.         } catch (SQLException e) {  
  107.             e.printStackTrace();  
  108.         }  
  109.         PrintWriter out = response.getWriter();  
  110.         out.write(buff.toString());  
  111.     }  
  112.   
  113. }  

这样就可以运行起来了,运行后可能会出现以下异常:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. java.sql.SQLException: com.mysql.jdbc.Driver  

只需要将mysql的jdbc驱动的jar包放进tomcat的lib目录里就可以了,注意是tomcat的目录下,不是项目的目录下。

第一次配置,难免有叙述错误之处,望大神们指正。

0 0
原创粉丝点击