配置和使用服务器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 ...
随机推荐
- Linux编程之变量
Bash变量与变量分类 变量命名规则 变量名必须以字母或下划线打头,名字中间只能由字母.数字和下划线组成 变量名的长度不得超过255个字符 变量名在有效的范围内必须是唯一的 在Bash中,变量的默认类 ...
- 整理 pycharm console调试博客
在Debug模式下,查看变量发现只能看到300个变量,报错: two large to show contents. Max items to show:300. 点击Debugger左侧consol ...
- 第二个python自动化练习
#Author:xiaoxiao from selenium import webdriver import unittest class DownLoad(unittest.TestCase): # ...
- mysql查看语句执行状态的常见函数
row_count() 返回上一个insert.update.delete语句的影响行数 found_rows() 返回上一个select查到的记录条数 last_insert_id() 返回上一次插 ...
- P4555 最长双回文串
题目描述 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为abc,逆序为cba,不相同). 输入长度为 n的串 S ,求 S的最长双回文子串 T ,即可将 ...
- SQL中GETDATE()一些操作
Sql Server 中一个非常强大的日期格式化函数Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AMSelect CONVE ...
- Twitter如何在数千台服务器上快速部署代码?
答案是:用BT,也就是你我应该都很熟悉的BitTorrent. 对于网站经营者.创业者来说,扩展性的问题是在网站流量成长过程中势必会面对的问题,如何建立一个具有扩展性的架构(scalable arch ...
- Maven 的基本用法
一.Maven 的安装 二.Maven的常用构建命令 1.mvn -v 查看maven版本 2.mvn compile 编译 3.mvn test 测试 4.mvn package 打包 5.Mvn ...
- Linux 命令行下使用多行输入
比较简单,建议实操,直接上图: 一行结束,直接敲回车换行.上一个例子,输入eof,终止多行输入:下一个例子,输入done,终止多行 ~~ 如果是参数太多,一行输入不完,可以通过 "空格\en ...
- Flask 服务器设置host=0.0.0.0之后外部仍然无法访问, 使用命令python *.py run启动OK
IP设置成0.0.0.0,还是不能外部访问 if __name__ == '__main__': app.run(host=, debug=True) 使用的Pycharm IDE,使用的三角号运行的 ...