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

1.配置Tomcat6.0根目录confcontext.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