1.配置Tomcat6.0根目录\conf\context.xml

<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context reloadable="true"> <!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource> <!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
--> <!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
--> <Resource name="jdbc/DBWater" auth="Container"
Type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/testmysql"
/> </Context>

2.新建一个类DBWater.java

//引入包
package com.cjg.test; import java.sql.Connection;
import java.sql.ResultSet;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Statement; public class DBWater {
//定义三个对象name 、number、sex
String name;
int number;
boolean sex; public String getName() {
return name;
}
public int getNumber() {
return number;
}
public boolean isSex() {
return sex;
}
//初始化一些对象
public void init() {
try {
//创建InitialContext对象
InitialContext initc = new InitialContext();
if (initc == null)
throw new Exception("No Context");
/*
* 在下面的字符串"java:comp/env/jdbc/DBWater"中,*"java:comp/env/"是不变的,
* 而"jdbc/DBWater"配置文件数据源名称
*/
DataSource ds = (DataSource)initc.lookup("java:comp/env/jdbc/DBWater");
if (ds != null) {
Connection conn = ds.getConnection(); //得到连接对象
if (conn != null) {
Statement stmt=conn.createStatement(); //创建陈述对象
//得到运行结果
ResultSet rst=stmt.executeQuery("select * from student");
//遍历运行结果
while (rst.next()) {
number=rst.getInt(1);
name=rst.getString(2);
}
conn.close(); //关闭连接对象
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

3.配置DBWater/WebRoot/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<!-- 描述信息 -->
<description>Connection Pool</description>
<!-- 数据源名字 -->
<res-ref-name>jdbc/DBWater</res-ref-name>
<!-- 数据源的类型 -->
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</web-app>

上面红色字体的名称要保持一致,另外要把数据库的jdbc驱动拷贝到Tomcat根目录/lib下面

配置和使用服务器Tomcat连接池的更多相关文章

  1. Tomcat连接池配置与实现/JNDI

    方法一: 在Tomcat的conf/context.xml中配置在Tomcat\apache-tomcat-6.0.33\conf目录下的context.xml文件中配置默认值如下: <?xml ...

  2. Tomcat 连接池详解

    (转) JDBC 连接池 org.apache.tomcat.jdbc.pool 是Apache-Commons DBCP连接池的一种替换或备选方案. 那究竟为何需要一个新的连接池? 原因如下: Co ...

  3. DB数据源之SpringBoot+MyBatis踏坑过程(七)手动使用Tomcat连接池

    DB数据源之SpringBoot+MyBatis踏坑过程(七)手动使用Tomcat连接池 liuyuhang原创,未经允许禁止转载  系列目录连接 DB数据源之SpringBoot+Mybatis踏坑 ...

  4. Tomcat 连接池调优

    性能较好的Tomcat 配置文件内容 <Context> <Resource name="jdbc/pgsql" type="javax.sql.Dat ...

  5. Tomcat连接池配置

    今日做了个小网站,数据量不大,但当发布到虚拟主机上之后,接连不断的遇到各种问题. 被折磨了数日后,在网上查了大量的相关资料,现总结如下. 一.项目在上传到远程服务器的过程中,有可能丢失文件,或文件内容 ...

  6. tomcat连接池配置详解

    <bean class="org.apache.tomcat.jdbc.pool.PoolProperties"> <property name="ur ...

  7. tomcat连接池配置和使用

    一种方法是在conf/context.xml文件中配置,配置oracle连接池的一个例子的context内容如下: <?xml version='1.0' encoding='utf-8'?&g ...

  8. Tomcat连接池

    步骤1: 找到Tomcat安装目录下的context.xml文件,在config目录下.在<Context/>节点下加入: <Resource name="jdbc/myt ...

  9. SpringBoot配置MySql数据库和Druid连接池

    1.pom文件增加相关依赖 <dependency> <groupId>mysql</groupId> <artifactId>mysql-connec ...

随机推荐

  1. C# 方法重载 overload、方法重写 override、隐藏 new

    一.重载:同一个作用域内发生(比如一个类里面),定义一系列同名方法,但是方法的参数列表不同.这样才能通过传递不同的参数来决定到底调用哪一个. 值得注意的是,方法重载只有通过参数不同来判断调用哪个方法, ...

  2. Kafka 配置参数

    Broker  Configs Property Default Description broker.id   每个broker都可以用一个唯一的非负整数id进行标识:这个id可以作为broker的 ...

  3. 从输入url开始,完善前端体系架构

    原文链接: https://segmentfault.com/a/1190000013662126 从输入URL到页面加载的过程?如何由一道题完善自己的前端知识体系! javascript 前端 23 ...

  4. redis 集群添加新节点

    准备好需要添加的节点:如何创建节点 启动创建的节点: 启动成功: 添加新节点:redis-cli --cluster add-node 127.0.0.1:7006 127.0.0.1:7000  第 ...

  5. Unity 脚本<1>

    RaycastHit2D hit = Physics2D.Linecast(targetPosition, targetPosition + new Vector2(x, y)); 猜测是lineca ...

  6. 【转】 [UnityUI]UGUI射线检测

    http://blog.csdn.net/lyh916/article/details/50947026 1.Graphic Raycaster 主要用于UI上的射线检测,挂有这个组件的物体,必须要挂 ...

  7. NOIP试题解析

    NOIP试题解析           by QTY_YTQ noip2010关押罪犯(并查集) 题意是有n个罪犯关在两个监狱中,其中有m对罪犯有仇恨关系,如果有仇恨的罪犯关在一起会产生一定影响力的事件 ...

  8. Incorrect column count: expected 1, actual 6

    JdbcTemplate使用时出现了一些问题: 解决办法:

  9. HDU 4614 Vases and Flowers(线段树+二分)

    Vases and Flowers Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others ...

  10. Python之数据结构:序列

    一.序列 1.列表.元组和字符串都是序列 二.序列的两个特点:索引操作符和切片操作符 1.索引操作符:从序列中抓取一个特定项目 下标操作:使用索引获取序列中的单个项目: eg:shoplist[0] ...