Tomcat(各种版本) 数据库连接池配置

前言 :
  1  准备 mysql 的 jdbc 驱动程序
  2  安装 Tomcat 默认全部装在 D:\Server\tomcat5.0 D:\Server\tomcat5.5 D:\Server\tomcat6.0
  3  以 TOMCAT_HOME 代替个版本的安装目录
4         将 jdbc 驱动放入 TOMCAT_HOME\ common\lib\ 下面
5         如果是其他数据库的话,就是换一下 jdbc 驱动以及各种参数就可以了的
6         配置 tomcat 数据源时的各种参数的详细介绍
7         其他设置。
1.       Tomcat 4.0
待添加
2.       Tomcat 5.0
2.1    建一个虚拟目录 test  (TOMCAT_HOME\webapps\)
  可以通过 http://localhost:8080/test 访问你的网站了
2.2    配置 Context ( 在 TOMCAT_HOME\conf\Catalina\localhost\ 下 建 test.xm,)
test.xml:
<?xml version="1.0" encoding="utf-8"?>
<Context docBase="dbpool" path="/test" workDir="work\Catalina\localhost\test">
</Context>
2.3    配置 server.xml
server.xml  的内容 : 红色表示加入的 ( 或者通过 http://localhost:8080/admin 进行可视化配置 )
<?xml version='1.0' encoding='utf-8'?>
<Server>
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
  <GlobalNamingResources>
    <Environment name="simpleValue" type="java.lang.Integer" value="30"/>
    <Resource auth="Container" description="User database that can be updated and saved" name="UserDatabase" type="org.apache.catalina.UserDatabase"/>
    <Resource auth="Container" name="jdbc/mysql" type="javax.sql.DataSource"/>
    <ResourceParams name="UserDatabase">
      <parameter>
        <name>factory</name>
        <value>org.apache.catalina.users.MemoryUserDatabaseFactory</value>
      </parameter>
      <parameter>
        <name>pathname</name>
        <value>conf/tomcat-users.xml</value>
      </parameter>
    </ResourceParams>
    <ResourceParams name="jdbc/mysql">
      <parameter>
        <name>maxWait</name>
        <value>5000</value>
      </parameter>
      <parameter>
        <name>maxActive</name>
        <value>4</value>
      </parameter>
      <parameter>
        <name>password</name>
        <value>198395</value>
      </parameter>
      <parameter>
        <name>url</name>
        <value>jdbc:mysql://localhost:3306/study?autoReconnect=true</value>
      </parameter>
      <parameter>
        <name>driverClassName</name>
        <value>com.mysql.jdbc.Driver</value>
      </parameter>
      <parameter>
        <name>maxIdle</name>
        <value>2</value>
      </parameter>
      <parameter>
        <name>username</name>
        <value>sa</value>
      </parameter>
    </ResourceParams>
  </GlobalNamingResources>
  <Service name="Catalina">
    <Connector acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" port="8080" redirectPort="8443" maxSpareThreads="75" maxThreads="150" minSpareThreads="25">
    </Connector>
    <Connector port="8009" protocol="AJP/1.3" protocolHandlerClassName="org.apache.jk.server.JkCoyoteHandler" redirectPort="8443">
    </Connector>
    <Engine defaultHost="localhost" name="Catalina">
      <Host appBase="webapps" name="localhost">
        <Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_log." suffix=".txt" timestamp="true"/>
      </Host>
      <Logger className="org.apache.catalina.logger.FileLogger" prefix="catalina_log." suffix=".txt" timestamp="true"/>
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>
    </Engine>
  </Service>
</Server>


2.4    配置 虚拟目录命名的 test.xml
test.xml: 红色表示加入的
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="testds" path="/testds" workDir="work\Catalina\localhost\testds">
<Resource name="jdbc/mysql" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/mysql">
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>4</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://127.0.0.1:3306/study?useUnicode=true</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>password</name>
<value>198395</value>
</parameter>
</ResourceParams>
</Context>
配置完毕

3.       Tomcat 5.5
3.1    建虚拟目录 test( 在 TOMCAT_HOME/webapp 或者其地方 )
  可以通过 http://localhost:8080/test 访问你的网站了
3.2    配置 server..xml( 或者可以通过 http://localhost:8080/admin 来可视化界面来完成 )
server.xml 内容 红色是加入的内容
<?xml version="1.0" encoding="UTF-8"?>
<Server>
  <Listener className="org.apache.catalina.core.AprLifecycleListener"/>
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
  <Listener className="org.apache.catalina.storeconfig.StoreConfigLifecycleListener"/>
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>
  <GlobalNamingResources>
    <Environment
      name="simpleValue"
      type="java.lang.Integer"
      value="30"/>
    <Resource
      auth="Container"
      description="User database that can be updated and saved"
      name="UserDatabase"
      type="org.apache.catalina.UserDatabase"
      pathname="conf/tomcat-users.xml"
      factory="org.apache.catalina.users.MemoryUserDatabaseFactory"/>
    <Resource
      name="jdbc/mysql"
      type="javax.sql.DataSource"
      password="198395"
      driverClassName="com.mysql.jdbc.Driver"
      maxIdle="2"
      maxWait="5000"
      username="sa"
      url="jdbc:mysql://localhost:3306/study?autoReconnect=true"
      maxActive="4"/>
  </GlobalNamingResources>
  <Service
      name="Catalina">
    <Connector
        port="8080"
        redirectPort="8443"
        minSpareThreads="25"
        connectionTimeout="20000"
        maxSpareThreads="75"
        maxThreads="150">
    </Connector>
    <Connector
        port="8009"
        redirectPort="8443"
        protocol="AJP/1.3">
    </Connector>
    <Engine
        defaultHost="localhost"
        name="Catalina">
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>
      <Host
          appBase="webapps"
          name="localhost">
      </Host>
    </Engine>
  </Service>
</Server>

3.3    配置 Context ( 在 TOMCAT_HOME\conf\Catalina\localhost\ 下 建 test.xml 与项目名字一样 )
  test.xml   红色表示新加入的
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/test" docBase="test" reloadable="true" crossContext="true" debug="0" >
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="sa" password="198395" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/study?useUnicode=true"/>
</Context>
4.       Tomcat 6.0

5.       测试用例


DatasourceCon.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Test of Tomcat  connection pool</title>
</head>
<body>
<%
out.print("strat test connection pool!<br/>");
try{
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
Object obj = (Object) ctx.lookup("jdbc/mysql");
DataSource ds = (javax.sql.DataSource)obj;
Connection conn = ds.getConnection();
out.print("tomcat mysql connection pool runs perfectly!");
conn.close();
}
catch(Exception ex){
out.print(ex.getMessage());
ex.printStackTrace();
}
%>
</body>
</html>

二、配置tomcat连接oracle数据库
2. Tomcat 6.0的  conf\Catalina\localhost 目录下xx.xml文件内容。
    xx名称与工程名一样。

<Context debug="1" docBase="C:\Tomcat 6.0\webapps\myweb" path="/myweb" reloadable="true" workDir="work\Catalina\localhost\myweb">
<Resource name="jdbc/openetdb" auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@111.11.11.111:1521:openet"
username="newdao"
password="123"
maxActive="20"
maxIdle="3"
removeAbandoned="true"
maxWait="3000" />
</Context>

3.拷贝ojdbc14.jar 到 tomcat的lib下
  oracle8i对应的为class12.jar,oracle9i为ojdbc14.jar

4.测试
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="javax.naming.*" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>   
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
 
  <body>
    This is my JSP page. <br>
    <%
            DataSource ds = null;
 
      
       try
       {
          InitialContext ctx = new InitialContext();
          ds = (DataSource)ctx.lookup("java:comp/env/jdbc/openetdb");
          Connection conn = ds.getConnection();
         
          String strSql = "SELECT  field  from TABLE " ;           

          Statement stmt = conn.createStatement();          
          ResultSet rs = stmt.executeQuery(strSql);                
        
           while(rs.next())
          {                  
              out.println("查出:"+rs.getObject("field"));               
           }                   
           rs.close();
           stmt.close();
           conn.close();              
            
          }catch(Exception ex)
          {
            ex.printStackTrace();     
            out.println(ex);       
         
          }
   
     %>
  </body>
</html>
 
另外通过研究还有一种方法配置数据库 以oracle为例:
1.配置%TOMCAT_HOME%\conf\server.xml

   <GlobalNamingResources>
      <Resource name="jdbc/openetdb"
          global="jdbc/openetdb"
          auth="Container"
          type="javax.sql.DataSource"
          password="111"
          driverClassName="oracle.jdbc.driver.OracleDriver"
          maxIdle="2"
          maxWait="5000"
          username="abc"
         url="jdbc:oracle:thin:@111.11.11.111:1521:SID"
         maxActive="4"/>
        
  </GlobalNamingResources>
2.然后,在ROOT\WEB-INF\web.xml中<web-app>和</web-app>之间添加

<resource-ref>
  <description>DataSource</description>
  <res-ref-name>jdbc/openetdb</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
  </resource-ref>

3.最后在%TOMCAT_HOME%\conf\content.xml里面配置当前应用程序的连接:

  <ResourceLink
              name="jdbc/openetdb"
              type="javax.sql.DataSource"
              global="jdbc/openetdb"/>

你可能感兴趣的:(tomcat,mysql,xml,SQL Server,jdbc)