zoukankan      html  css  js  c++  java
  • 配置Tomcat服务器数据连接池

    优势:由于HTTP协议的web程序是无状态的,在应用程序使用JDBC时,多次请求处理客户端都会重新建立连接,如果请求繁忙,将会消耗非常多的资源。

       数据库连接池负责分配、管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是重新建立。

      配置全局数据库连接池:

      配置局部数据库连接池:

    在Tomcat安装目录下, confserver.xml中

      <GlobalNamingResources>标签下

    添加

     <Resource name="jdbc/webdb" 
                 auth="Container" 
    	     type="javax.sql.DataSource"
                 driverClassName="com.mysql.jdbc.Driver"
                 url="jdbc:mysql://localhost:3306/shop?characterEncoding=utf-8"
                 username="root" 
                 password="root" 
                 maxActive="200" 
                 maxIdle="50" 
                 maxWait="3000" 
       />
    

     在confCatalinalocalhost

    创建以web命名的xml

    <?xml version='1.0' encoding='utf-8'?>
    <Context path ="/webdemo" docBase="webdemo" debug="0">
    		<ResourceLink name="jdbc/webdb" global="jdbc/webdb"  
    type="javax.sql.DataSource"/>	
    	
    </Context>
    

     创建Servlet

    package com.demo.servlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.sql.DataSource;
    
    public class HelloWorldServlet extends HttpServlet {
    
    	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
    		//设置HTTP响应头的ContentType字段值
    		response.setContentType("text/html; charset=UTF-8");
    		
    		//获取用于输入输出消息的PrintWriter对象
    		PrintWriter out = response.getWriter();
    		
    		try {
    			//或得Context对象
    			Context context = new InitialContext();
    			DataSource ds = (DataSource) context.lookup("java:/comp/env/jdbc/webdb");
    			//获得Connetion对象
    			Connection connection = ds.getConnection();
    			PreparedStatement statement = connection.prepareStatement("select * from memberlevel");
    			
    			ResultSet rs = statement.executeQuery();
    			StringBuffer table = new StringBuffer();
    			table.append("<table border='1'>");
    			table.append("<tr><td>ID</td><td>级别</td><td>积分</td></tr>");
    			while(rs.next()){
    				
    				table.append("<tr><td>"+rs.getString("ID")+"</td><td>"+rs.getString("LevelName")+"</td><td>"+rs.getString("Favourable")+"</td></tr>");
    			
    			}
    			table.append("</table>");
    			
    			out.println(table.toString());
    		} catch (Exception e) {
    			out.println(e.getMessage());
    		}
    		
    		
    	}
    }
    

     在lib中加入mysql的jar包

    web.xml配置

    之后请求

    http://localhost:8080/webdemo/servlet/HelloWorldServlet

    结果:

    配置局部数据库连接池将web命名的xml中内容替换即可

    <Context path ="/webdemo" docBase="webdemo" debug="0">
    。。。。。。
    配置
    </Context>
  • 相关阅读:
    扫描线算法
    评论备份(3)
    评论备份(2)
    二分法的注意事项
    sam模板
    Machine Learning(Andrew Ng)学习笔记
    洛谷P2221 [HAOI2012]高速公路
    洛谷P3233 [HNOI2014]世界树
    P2515 [HAOI2010]软件安装
    BZOJ4293: [PA2015]Siano
  • 原文地址:https://www.cnblogs.com/danwuxinbolg/p/5568315.html
Copyright © 2011-2022 走看看