在tomcat7.x版本中配置数据源

近期用java开发一个基于微信公众平台的系统,需要在tomcat7.x中配置数据源,在网上查了一些资料并结合自己亲自实践,终于配置成功,现将过程记录如下:

1、修改tomcat安装目录下的conf目录的context.xml文件,增加<Resource />节点内容,修改如下:

<?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>

    <!-- 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/zhxq" auth="Container" type="javax.sql.DataSource" 
          driverClassName="com.mysql.jdbc.Driver" 
    url="jdbc:mysql://127.0.0.1:3306/zhxq" 
    username="root" 
    password="670710" 
    maxActive="100" 
    maxIdle="30" 
          maxWait="10000" />  
</Context>

加黑部分是添加的内容,其中:

name="jdbc/zhxq"是自定义的名称,以后需要通过jndi访问,在该名称中jdbc/是必需的,zhxq可以自己定义。

driverClassName="com.mysql.jdbc.Driver" 是数据库驱动的名称,本项目采用的事MySQL数据库。

url="jdbc:mysql://127.0.0.1:3306/zhxq" 是数据库所在的位置,本项目数据库的名称为zhxq。

username="root" 数据库登录的账号
password="670710" 数据库登录的密码

2、修改WEB应用的web.xml文件,添加<resource-ref>节点,修改文件如下:

<?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">
 <servlet>
  <servlet-name>WeixinServlet</servlet-name>
  <servlet-class>Servlet.WeixinServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>WeixinServlet</servlet-name>
  <url-pattern>/Servlet/WeixinServlet</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <login-config>
  <auth-method>BASIC</auth-method>
 </login-config>
 <!-- 配置tomcat 数据源 -->
 <resource-ref>
    <description>MySQL Datasource</description>
    <res-ref-name>jdbc/zhxq</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

</web-app>

其中:

<res-ref-name>jdbc/zhxq</res-ref-name>要和前面定义的name="jdbc/zhxq"名称一致。

3、将数据库驱动程序的jar文件拷贝到tomcat安装目录下的lib文件夹中。

通过以上3步,数据源配置完成,通过如下代码,获取数据连接:

package ZHXQ.DataAcess;
import java.sql.Connection;
import javax.sql.DataSource;
import javax.naming.InitialContext;
import javax.naming.Context;
public class MySQLConnection implements IDatabaseConnect{
    private String myJNDIName;
    private Connection myConnection=null;
    //实现接口的方法
    public MySQLConnection(String name) throws Exception
    {
     //JNDI的查找名称
     myJNDIName="java:comp/env/"+name;
     CreateConnection();
    }
    //从连接池中取得连接
    public void CreateConnection() throws Exception
    {
     //得到连接池的数据连接
     try
     {
        Context myContex=new InitialContext();
        DataSource myDataSource=(DataSource)myContex.lookup(myJNDIName);
        myConnection=myDataSource.getConnection();
     }
     catch(Exception e)
     {
      throw e;
     }
    }
    //从数据连接池中获取连接对象
    public Connection GetConnection()throws Exception
    {
     return myConnection;
    }
    public void Close() throws Exception
    {
     if(myConnection!=null)
     {
      try{
         myConnection.close();
      }
      catch(Exception e){
       throw e;
      }
     }
    }
    public void SetJNDIName(String name)
    {
     myJNDIName="java:comp/env/"+name;
    }
}

定义的接口如下:

package ZHXQ.DataAcess;
import java.sql.Connection;
public interface IDatabaseConnect {
   //设定JNDI名称,本项目中为 "jdbc/zhxq"

   public void SetJNDIName(String name);
   //从连接池获取连接对象
   public void CreateConnection() throws Exception;
   //得到连接对象
   public Connection GetConnection() throws Exception;
   //把连接对象放回连接池
   public void Close() throws Exception;
}

你可能感兴趣的:(Tomcat7配置数据源)