配置和使用服务器Tomcat连接池
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连接池的更多相关文章
- Tomcat连接池配置与实现/JNDI
方法一: 在Tomcat的conf/context.xml中配置在Tomcat\apache-tomcat-6.0.33\conf目录下的context.xml文件中配置默认值如下: <?xml ...
- Tomcat 连接池详解
(转) JDBC 连接池 org.apache.tomcat.jdbc.pool 是Apache-Commons DBCP连接池的一种替换或备选方案. 那究竟为何需要一个新的连接池? 原因如下: Co ...
- DB数据源之SpringBoot+MyBatis踏坑过程(七)手动使用Tomcat连接池
DB数据源之SpringBoot+MyBatis踏坑过程(七)手动使用Tomcat连接池 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之SpringBoot+Mybatis踏坑 ...
- Tomcat 连接池调优
性能较好的Tomcat 配置文件内容 <Context> <Resource name="jdbc/pgsql" type="javax.sql.Dat ...
- Tomcat连接池配置
今日做了个小网站,数据量不大,但当发布到虚拟主机上之后,接连不断的遇到各种问题. 被折磨了数日后,在网上查了大量的相关资料,现总结如下. 一.项目在上传到远程服务器的过程中,有可能丢失文件,或文件内容 ...
- tomcat连接池配置详解
<bean class="org.apache.tomcat.jdbc.pool.PoolProperties"> <property name="ur ...
- tomcat连接池配置和使用
一种方法是在conf/context.xml文件中配置,配置oracle连接池的一个例子的context内容如下: <?xml version='1.0' encoding='utf-8'?&g ...
- Tomcat连接池
步骤1: 找到Tomcat安装目录下的context.xml文件,在config目录下.在<Context/>节点下加入: <Resource name="jdbc/myt ...
- SpringBoot配置MySql数据库和Druid连接池
1.pom文件增加相关依赖 <dependency> <groupId>mysql</groupId> <artifactId>mysql-connec ...
随机推荐
- IOS开发---菜鸟学习之路--(十七)-利用UITableView实现个人信息界面
首先来看下我们要实现的效果 需要实现这样的效果 然后我们开始动手吧. 首先选择添加一个新的ViewController 然后打开XIB文件,添加一UITableView 并将样式设置为分组 同时将按住 ...
- 【Luogu】P3320寻宝游戏(Splay)
题目链接 其实这题用Set就完事了但我不会Set 智商-=inf 求虚树上所有边权和的两倍. 具体方式就是splay把所有在虚树上的点存一下,(按照DFS序排序的)每次插入/删除会更新前驱和它.后继和 ...
- Mail.Ru Cup 2018 Round 2 Problem C Lucky Days
设在第 $x$ 天二人都 lucky,则有 $\DeclareMathOperator{\lcm}{lcm}$ $ x = y_a t_a + R_a $ $ x= y_b t_ b + R_b$ 约 ...
- THUSC2018 爆零记
没想到我还真能过这个...... 太玄学了= = 不过这直接导致我月考数学挂科,掉出年级前十= = 5.26 THU过了! 真是十分意外的惊喜啊$-\omega-$ 6.1 今天出发去帝都! 然而飞行 ...
- linux系统——fread()与read()函数族区别
fread与read区别: 1,fread是带缓冲的,read不带缓冲. 2,fopen是标准c里定义的,open是POSIX中定义的. 3,fread可以读一个结构.read在linux/unix中 ...
- vue的v-for循环普通数组、对象数组、对象、数字
如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8& ...
- rpm包安装mysql5.6.*版本
1.查看是否已经安装Mysql rpm -qa | grep -i mysql #删除已经安装的Mysql程序 rpm -ev *****.rpm 2.检查是否还有残留mysql文件夹 find / ...
- Eclipse与MyEclipse修改注释字体颜色
修改配置路劲 Window--->Preferences--->Java--->Editor--->Syntax Coloring--->Element--->Co ...
- border:none;和border:0;的区别
一.是理论上的性能差异 [border:0;]把border设为“0”像素虽然在页面上看不见,但按border默认值理解,浏览器依然对border-width/border-color进行了渲染,即已 ...
- 数据库一直显示"正在恢复"
restore database [DataBase] with recovery ALTER DATABASE DataBase SET OFFLINE WITH ROLLBACK IMMEDIAT ...