jsp:tomcat7+mysql配置数据库连接池(eclipse)

一、在Servers中找到context.xml文件

jsp:tomcat7+mysql配置数据库连接池(eclipse)_第1张图片

标签之间添加以下代码:

 
auth="Container"               
type="javax.sql.DataSource"      
maxActive="50"                      
maxIdle="30"                           
maxWait="10000"    
username="数据库用户名"  
password="数据库密码" 
driverClassName="com.mysql.jdbc.Driver"        
url="jdbc:mysql://localhost/test?serverTimezone=UTC" >   

二、在项目文件中的WebConntent(有的是WebRoot目录)下的WEB-INF中的web.xml文件中添加以下代码:

  
    DB Connection
    jdbc/test  
    javax.sql.DataSource
    Container
  

jsp:tomcat7+mysql配置数据库连接池(eclipse)_第2张图片

三、写一个测试jsp文件:

<%@ page import="javax.naming.*,javax.sql.DataSource" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>


  
    
    Insert title here
  
  
    <%
      try{
        Context initContext = new InitialContext();
        Context envContext= (Context)initContext.lookup("java:/comp/env");
        DataSource ds = (DataSource)envContext.lookup("jdbc/test");
        System.out.println(ds.getConnection());
    %>
    通过数据源获取数据连接
    <%
        }catch (Exception e){
    %> 
    连接失败
    <%=e %>
    <%
        }
    %>

  

四、结果

jsp:tomcat7+mysql配置数据库连接池(eclipse)_第3张图片

你可能感兴趣的:(jsp:tomcat7+mysql配置数据库连接池(eclipse))