配置和使用服务器Tomcat连接池

本文详细介绍了如何在Tomcat6.0中配置数据库连接池,包括修改context.xml文件以设置数据源参数,创建DBWater.java类用于数据库操作,以及在web.xml中引用数据源。此外,还提到了将JDBC驱动复制到Tomcat/lib目录下的必要性。

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下面

转载于:https://www.cnblogs.com/shanmao/p/3411374.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值