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. 5、CSS基础part-3

    1.CSS列表 ①类型 ul.disc {list-style-type: disc} ②位置 ul.inside {list-style-position: inside} ③列表图像 2.表格

  2. Tomcat网站根目录的配置

    在</Host>前插入: <Host> … … <Context path="" docBase="E:\Users\Administrat ...

  3. 关于jena-fuseki SPARQL query版本问题的解决方案

    在做“Apache jena SPARQL endpoint及推理”时,遇到了不少问题,主要原因是jena-fuseki版本更新了.以下对问题解决方案做下笔记.想了解更多,请查阅底部参考文献. Que ...

  4. [BZOJ2456] mode(一道很有意思的题)

    传送门 看到这个题的第一反应是离散化+线段树乱搞.. eeeeeeeeeeee感觉数据结构学傻了,其实直接存下来,sort一遍,n/2的位置的就是答案 当然前提是空间够的话 1m的空间连数组都开不下 ...

  5. BZOJ4540 [Hnoi2016]序列 【莫队 + ST表 + 单调栈】

    题目 给定长度为n的序列:a1,a2,-,an,记为a[1:n].类似地,a[l:r](1≤l≤r≤N)是指序列:al,al+1,-,ar- 1,ar.若1≤l≤s≤t≤r≤n,则称a[s:t]是a[ ...

  6. 洛谷P3045 [USACO12FEB]牛券Cow Coupons

    P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB ...

  7. vue项目中使用iconMoon图标

    前两篇文章写了一下如何在vue项目中使用vue-awesome和阿里的iconfont,这里介绍一下如何使用iconMoon图标 iconMoon和前两者相比可以生成自己的矢量图,这点是我喜欢的.至于 ...

  8. 关于 react state的改变数据上面的一点问题

    在react当中 比如说 this.state = { loginInfo: { account: "...", password: "..." } } thi ...

  9. wsl折腾记

    参考1 wsl在哪 C:\Users\用户名\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndg ...

  10. cf 341D lahub and xors

    题目大意 给定初始值为\(0\)的\(n*n\)矩阵 两种操作 矩形内异或一个值 求矩阵内异或和 \(n\le 1000\) 分析 二维线段树标记不下传貌似直接可做 有没有更简便的方法? 考虑异或的特 ...