Web开发中与数据库的连接是必不可少的,而数据库连接池技术很好的优化了动态页与数据库的连接,相比单个连接数据库连接池节省了很大的资源。用一个通俗的比喻:如果一个人洗澡需花一桶水,那一百个人就要花一百桶水,太浪费了.如果都在池子里洗,洗多少个人都不怕了。

1.将MySQL的JDBC驱动复制到Tomcat安装目录里的lib文件夹下。驱动可以从MySQL官网上下载,为jar包。

2.将Tomcat的配置文件Context.xml做如下修改:

<Context path="/DBTest" docBase="DBTest"
        debug="5" reloadable="true" crossContext="true">

<!-- maxActive: Maximum number of dB connections in pool. Make sure you
         configure your mysqld max_connections large enough to handle
         all of your db connections. Set to -1 for no limit.
         -->

<!-- maxIdle: Maximum number of idle dB connections to retain in pool.
         Set to -1 for no limit.  See also the DBCP documentation on this
         and the minEvictableIdleTimeMillis configuration parameter.
         -->

<!-- maxWait: Maximum time to wait for a dB connection to become available
         in ms, in this example 10 seconds. An Exception is thrown if
         this timeout is exceeded.  Set to -1 to wait indefinitely.
         -->

<!-- username and password: MySQL dB username and password for dB connections  -->

<!-- driverClassName: Class name for the old mm.mysql JDBC driver is
         org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
         Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
         -->
   
    <!-- url: The JDBC connection url for connecting to your MySQL dB.
         The autoReconnect=true argument to the url makes sure that the
         mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
         connection.  mysqld by default closes idle connections after 8 hours.
         -->

<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>

</Context>

注意代码中红色部分:DBTest 改为自己的项目路径;TestDB改为自己的数据源名,但是后面使用时候要与这里的配置保持一致;javauser和 javauser改为自己MySQL的用户名密码;url的格式依次为jdbc:mysql://{你的数据库服务所在的IP,如果为本机就为localhost}:{你的数据库服务端口号}/{MySQL中要使用的数据库名称}?autoReconnect=true  。

3.修改项目WEB-INF/web.xml 配置文件(若无,请新建),在“</web-app>”之上添加如下代码:

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

上步中若修改了数据源名此步中红色部分请保持与上步中的一致。

4.代码示例:

Context initContext = new InitialContext();
Context envContext  = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/TestDB");
Connection conn = ds.getConnection();
Statement st = null;
ResultSet rs = null;
st = conn.createStatement();
rs = st.executeQuery(yoursql);

注意红色部分与上两步中的一致;yoursql处写你的sql代码。

通过1-3步就在Tomcat中配置好了MySQL的数据库连接池。

Tomcat中配置MySQL数据库连接池的更多相关文章

  1. JNDI和在tomcat中配置DBCP连接池 元数据的使用 DBUtils框架的使用 多表操作

    1 JNDI和在tomcat中配置DBCP连接池 JNDI(Java Naming and Directory Interface),Java命名和目录接口,它对应于J2SE中的javax.namin ...

  2. tomcat中使用mysql连接池的配置

    1.下载相应的jar包,添加到工程中 需要下载的包主要有commons-pool2-2.2 commons-dbcp2-2.0.1-src commons-dbcp2-2.0.1  commons-c ...

  3. Tomcat中的c3p0数据库连接池的释放

    一个项目通过c3p0获得连接池,相关代码如下: public class JdbcUtil { // 连接池的核心类 private static ComboPooledDataSource data ...

  4. 在tomcat下context.xml中配置各种数据库连接池(JNDI)

    1.   首先,需要为数据源配置一个JNDI资源.我们的数据源JNDI资源应该定义在context元素中.在tomcat6版本中,context元素已经从server.xml文件中独立出来了,放在一个 ...

  5. 在tomcat下context.xml中配置各种数据库连接池

    作者:郑文亮 Tomcat6的服务器配置文件放在 ${tomcat6}/conf 目录底下.我们可以在这里找到 server.xml 和 context.xml.当然,还有其他一些资源文件.但是在在本 ...

  6. 浅谈tomcat的配置及数据库连接池的配置

    1.如何修改tomcat的端口 在某些情况下,可能需要修改tomcat监听的端口8080,比如: a.需要启动两份tomcat服务器 b.某个服务占用了8080端口(1433,1521,3306... ...

  7. 在Tomcat中配置MySQL数据源

    打开context.xml文件,路劲为:apache-tomcat-7.0.61\conf,添加如下代码: <Resource auth="Container" driver ...

  8. 在tomcat中配置连接池

    在tomcat的conf/Catalina/localhost目录下配置项目路径,tomcat启动是会直接根据配置去加载项目. 虽然配置就一句话,但经常忘,今天记下来. 如果你的项目成名是:mypro ...

  9. 在Tomcat中配置连接池和数据源

    1.DataSource接口介绍 (1)DataSource 概述 JDBC1.0原来是用DriverManager类来产生一个对数据源的连接.JDBC2.0用一种替代的方法,使用DataSource ...

随机推荐

  1. bzoj 4439: [Swerc2015]Landscaping -- 最小割

    4439: [Swerc2015]Landscaping Time Limit: 2 Sec  Memory Limit: 512 MB Description FJ有一块N*M的矩形田地,有两种地形 ...

  2. 最短路径:我的理解--SPFA算法

    SPFA算法 求单源最短路的SPFA算法的全称是:Shortest Path Faster Algorithm. 最短路径快速算法-SPFA算法是西南交通大学段凡丁于1994年发表的. 适用范围:给定 ...

  3. JDK源码(1.7) -- java.util.Collection<E>

     java.util.Collection<E> 源码分析(JDK1.7) -------------------------------------------------------- ...

  4. mybatis源码分析(2)-----SqlSession创建

    1. 在创建好sqlSessionFactory之后,接着就要配置sqlSession的创建. <bean id="simpleTempalte" class="o ...

  5. java类中元素初始化顺序详解

    父类静态变量父类静态块子类静态变量子类静态块父类普通变量父类普通块父类构造方法子类普通变量子类普通块子类构造方法

  6. Caused by: java.net.UnknownHostException: localhost.localdomain: localhost.localdomain的问题解决

    在hosts文件增加如下配置即可,下面的方法适合上面提示的错误,无论是Tomcat问题还是MongoDB等等的问题都可以完美解决. vi /etc/hosts 127.0.0.1 localhost ...

  7. SpringMvc的服务器端跳转和客户端跳转

    首先,找到 package org.springframework.web.servlet.view; public class InternalResourceViewResolver extend ...

  8. jdbcTemplate异常:like模糊查询报错(Parameter index out of range (1 > number of parameters)

    http://cuisuqiang.iteye.com/blog/1480525   模糊查询like要这样写 注意Object参数和like语法   public static void main( ...

  9. asp.net Core 中间件Hello world

    虽然在ASP.NET 5中,微软没有再强调OWIN(Open Web Interface for .NET)及其微软官方的OWIN实现Katana,但是其中涉及到一些原则和设计思想依然被ASP.NET ...

  10. C++ Tr1中的正則表達式

    要使用正則表達式,首先要有类库支持,C++曾经不像Java或者C#有完整的类库使用,可是在Tr1中早已提供了正则库,仅仅是非常少被人们注意罢了 TR1中包括了一个正则库,来自Boost的 regex, ...