Tomcat7和mysql连接池dbcp方式的配置方法和测试

jopen 9年前

一.设计测试用的数据库

    1.新建数据库

     create database testmysql;

    2.新建一个用户信息数据表

     create table test(

       username varchar(20) primary key,

       password varchar(20));

   3.给新表插入数据信息

     insert into test values('keivn','123456');

 

二. 设计局部数据源和连接池

     1.在webapps目录中新建test目录,然后在test中分别新建WEB-INF和META-INF目录,在WEB-INF目录

 中新建classes和lib目录,将mysql数据库驱动文.jar放进lib目录中

     这里我使用的版本是mysql-connector-java-5.1.22-bin.jar

    2.在META-INF目录中新建context.xml文件,然后将下面内容复制,保存

<?xml version='1.0' encoding='utf-8'?>

<Context>

 <Resource name="jdbc/mysql"

         auth="Container"

         driverClassName="com.mysql.jdbc.Driver"

         type="javax.sql.DataSource"

         url="jdbc:mysql://localhost:3306/testmysql"

         username="root"

         password="123456"

         maxActive="100" 

         maxIdle="30"

         maxWait="10000" />

</Context>

context.xml中的参数的解析如下:

   name属性是数据源名称,通常采取jdbc/**.

   type属性是数据源方式。

   driverClassName属性是驱动程序名称。

   username,password,数据库名称和密码

   url:访问的数据库路径

   maxActive属性是并发连接的最大数。设置为0则无限制。

   maxWait属性是等待连接的最大连接的时间。

   maxIdle属性是连接池中空闲的连接的个数。

  上文中的设置的 maxActive="100"说明可以最大连接的个数为100个,再建立连接,则出现异常。

  而maxIdle="30"说明当关闭数据库时(不是真正的断开连接,而是归还连接池中)连接池中最大可以有空闲的连接数为30个。

  若是再有建立连接,此时若连接池中没有空闲的连接,但是又没有达到maxActive并发的最大连接数,则在连接池中建立连接。

 

  3.然后在WEB-INF中新建web.xml文件,然后将下面内容复制,保存

    <?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

                     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

  version="3.0"

  metadata-complete="true">

 

  <display-name>test</display-name>

  <welcome-file-list>

    <welcome-file>test.jsp</welcome-file>

  </welcome-file-list>

  <resource-ref>

  <description>DB Connection</description>

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

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

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

  </resource-ref>

 

</web-app>

 4.完成后,重启tomcat服务器。

 

三.编写一个jsp页面测试设置连接池,连接数据库是否成功。

 在test目录中新建test.jsp文件,然后将下面内容复制,保存

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test</title>
</head>
<body>
<%
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;

String sql=null;
String username=null;
String alias=null;
String pwd=null;
String strMsg=null;
try{
    Context cxt = new InitialContext();
    DataSource ds = (DataSource)cxt.lookup("java:/comp/env/jdbc/mysql");
    conn = ds.getConnection();
    sql="select * from  test";
    stmt=conn.prepareStatement(sql);
    rs=stmt.executeQuery();
    if(rs.next()){
        strMsg="
连接ok";
        out.println(rs.getString("username"));
  out.println(rs.getString("password"));
        out.println("
连接ok");
    }else{
        out.println("rs.next() fail");
    }
    rs.close();
   
} catch(Exception e){
    out.println("连接失败
:"+e.getMessage());
    //e.printStackTrace();
}

try{
    if(stmt!=null) stmt.close();
}catch(Exception e){}

try{
    if(conn!=null) conn.close();
}catch(Exception e){}


%>
</body>
</html>

使用浏览器输入http://localhost:8080/test/test.jsp