首页 | 联系我们 | 叶凡网络官方QQ群:323842844
游客,欢迎您! 请登录 免费注册 忘记密码
您所在的位置:首页 > 开发语言 > DataBase > 正文

Tomcat连接池的配置和使用

作者:名扬互联 来源: 日期:2013-07-19 17:23:27 人气:14 加入收藏 评论:0 标签:Tomcat连接池的配置和使用

在conf/context.xml文件中的配置:

<Context>
   <Resource
   name="jdbc/ufoffice"
   auth="Container"
   type="javax.sql.DataSource"
   username="root"
   password="pass"
   driverClassName="com.mysql.jdbc.Driver"                                                           url="jdbc:mysql://192.168.1.130:3306/sso?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=utf-8"
   maxActive="10"
   maxIdle="5"/>
</Context>




使用:


//
// Copyright (c) 2000-2009 Hfyefan. All Rights Reserved.
//
// This software is the confidential and proprietary information of
// Hfyefan
//
// original author: Liming
//
// HFYEFAN MAKES NO REPRESENTATIONS or WARRANTIES ABOUT THE SUITABILITY OF
// THE SOFTWARE, EITHER EXPRESS or IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE, or NON-INFRINGEMENT. HFYEFAN SHALL NOT BE
// LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
// MODIFYING or DISTRIBUTING THIS SOFTWARE or ITS DERIVATIVES.
//
// THIS SOFTWARE IS NOT DESIGNED or INTENDED FOR USE or RESALE AS ON-LINE
// CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
// PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
// NAVIGATION or COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
// SUPPORT MACHINES, or WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
// SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, or SEVERE
// PHYSICAL or ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). HFYEFAN
// SPECIFICALLY DISCLAIMS ANY EXPRESS or IMPLIED WARRANTY OF FITNESS FOR
// HIGH RISK ACTIVITIES.
//
package com.hfyefan.uua.common.utils;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;


/**
* This class provides the JDBC encapsulation,it handles the
* select,update,insert and delete event in the database.
*
* @author Liming
* @time Feb 27, 2009 10:01:16 AM
* @version 1.0
*/
@SuppressWarnings("unused")
public class DbUtil {

   // the string of db connection
   private String dbConnectionUrl;
   // the string of db driver
   private String dbDriver;
   // the string of db user name
   private String dbUser;
   // the string of db password
   private String dbPass;

   static ResultSet rs = null;
   static Connection conn = null;
   static DbUtil db = null;
   static PreparedStatement pst = null;
   private static final String DATASOURCE_LOOKUP_LINK = "java:comp/env/jdbc/ufoffice";

   public DbUtil() {
       conn = getConnection();
   }

   /**
    * get db connection
    *
    * @return connection
    */
   private Connection getConnection() {
       Connection connection = null;
       /*
        * try { Class.forName(this.dbDriver); connection =
        * DriverManager.getConnection(this.dbConnectionUrl, this.dbUser,
        * this.dbPass); } catch (ClassNotFoundException e) {
        * System.err.println("DB.getConnection" + e.getMessage()); } catch
        * (SQLException e) { System.err.println("DB.getConnection" +
        * e.getMessage()); }
        */

       try {
           Context ctx = new InitialContext();
           DataSource ds = (DataSource) ctx.lookupLink(DATASOURCE_LOOKUP_LINK);
           connection = ds.getConnection();
       } catch (NamingException e) {
           System.err.println("DB.getConnection" + e.getMessage());
       } catch (SQLException e) {
           System.err.println("DB.getConnection" + e.getMessage());
       }
       return connection;
   }

   /**
    * open db connection
    */

   public static DbUtil open() {
       if (db == null) {
           db = new DbUtil();
       }
       return db;
   }

   /**
    * execute query satament in the database
    *
    * @param sql,sql
    *        statement for query data
    * @return ResultSet,
    */
   public static ResultSet executeQuery(String sql) {
       try {
           pst = conn.prepareStatement(sql);
           rs = pst.executeQuery();
       } catch (SQLException e) {
           System.err.println("DB.excuteQuery:" + e.getMessage());
       }
       return rs;
   }

   /**
    * execute query satament in the database
    *
    * @param sql,sql
    *        statement for query data
    * @return ResultSet,
    */
   public static ResultSet executeQuery(String sql, Object[] objs) {
       try {
           pst = conn.prepareStatement(sql);
           if (objs != null) {
               for (int i = 0;i < objs.length;i++) {
                   pst.setObject(i + 1, objs[i]);
               }
           }
           rs = pst.executeQuery();
       } catch (SQLException e) {
           System.err.println("DB.excuteQuery:" + e.getMessage());
       }
       return rs;
   }

   /**
    * execute insert,update or delete statment in the database.
    *
    * @param sql,sql
    *        statement for insert,update or delete data
    * @return int, the records for insert,update or delete data
    */
   public static int executeUpdate(String sql) {
       int result = 0;
       try {
           pst = conn.prepareStatement(sql);
           result = pst.executeUpdate();
       } catch (SQLException e) {
           System.err.println("DB.excuteUpdate:" + e.getMessage());
       }
       return result;
   }

   /**
    * execute insert,update or delete statment in the database.
    *
    * @param sql,sql
    *        statement for insert,update or delete data
    * @return int, the records for insert,update or delete data
    */
   public static int executeUpdate(String sql, Object[] objs) {
       int result = 0;
       try {
           pst = conn.prepareStatement(sql);
           if (objs != null) {
               for (int i = 0;i < objs.length;i++) {
                   pst.setObject(i + 1, objs[i]);
               }
           }
           result = pst.executeUpdate();
       } catch (SQLException e) {
           System.err.println("DB.excuteUpdate:" + e.getMessage());
       }
       return result;
   }

   /**
    * close connection.
    */
   public static void close() {
       try {
           if (rs != null) {
               rs.close();
               rs = null;
           }
           if (pst != null) {
               pst.close();
               pst = null;
           }
           if (!conn.isClosed()) {
               conn.close();
               conn = null;
           }
           db = null;
       } catch (SQLException e) {
           System.err.println("DB.close:" + e.getMessage());
       }
   }
}

本文网址:http://www.mingyangnet.com/html/database/95.html
读完这篇文章后,您心情如何?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
上一篇: 理解数据库范式
更多>>网友评论
发表评论
编辑推荐
  • 没有资料