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. jeakins配置邮件通知,附带解决535报错:authentication failed,如果发现测试邮件可以发出,项目构成无法发出邮件,请开启SSL认证,端口号改为(465),qq邮箱、163邮箱通用

    535报错解决方案:调用163邮箱服务器来发送邮件,我们需要开启POP3/SMTP服务,这时163邮件会让我们设置客户端授权码,这个授权码替代上面代码部分的passwd即可成功发送邮件 如果设置的邮箱 ...

  2. 设计模式之迭代器模式 Iterator

    代码实现 public interface MyIterator { void first(); //将游标指向第一个元素 void next(); //将游标指向下一个元素 boolean hasN ...

  3. 实战项目——获取图片中的GPS位置信息和拍摄时间

    今天突然看到有人写过获取图片中位置信息的程序.我觉得很有趣,也就自己实践了一下,研究了一下 话不多说,先上代码 #!/usr/bin/env python3 # -*- coding: utf-8 - ...

  4. hibernate注解配置举例说明

    Hibernate Annotation   (Hibernate 注解)   进入:http://www.hibernate.org 说明文档: 英文:http://docs.jboss.org/h ...

  5. nginx访问控制allow、deny(ngx_http_access_module)

    单看nginx模块名ngx_http_access_module,很多人一定很陌生,但是deny和allow相比没一个人不知道的,实际上deny和allow指令属于ngx_http_access_mo ...

  6. vue2搭建简易spa

    使用vue-cli来配置webpack,webpack是一个打包工具,使程序模块化 全局安装vue-cli: npm install -g vue-cli 安装好后,使用vue-cli脚手架配置web ...

  7. 【06】Vue 之 组件化开发

    组件其实就是一个拥有样式.动画.js逻辑.HTML结构的综合块.前端组件化确实让大的前端团队更高效的开发前端项目.而作为前端比较流行的框架之一,Vue的组件和也做的非常彻底,而且有自己的特色.尤其是她 ...

  8. Safari 11.0 已发布,新特性都在这儿了!

    Safari 11.0 兼容性 Safari 11.0 可运行于 iOS 11.0 和 macOS 10.1版本的系统环境,同时在macOS 10.12.6 和 10.11.6版本中也可以使用. Hi ...

  9. 使用Google的Gson实现对象和json字符串之间的转换

    使用Google的Gson实现对象和json字符串之间的转换 需要gson.jar 1.JsonUtil.java package com.snail.json; import java.lang.r ...

  10. div 的相对定位与绝对定位

    网 上也有不少使用div + css进行布局的教程,却存在很多缺陷,一是对css的布局模型讲解不清楚,让人很难理解相对定位.浮动等概念:二是虽然避免了“表格套表格”的缺点,却 带来了“div 套 di ...