Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Application Server  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:关于Tomcat的Connection管理问题 [Re:cxp108]
lisliefor





发贴: 287
积分: 7
于 2006-09-12 09:18 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
/********************************************************************************
模块名    : 数据库管理器
文件名    : DatabaseManager.java
相关文件    :
文件实现功能  : 与数据库建立连接、断开连接、执行查询和更新操作
作者    : William@Passion Software
版本    : 1.1
--------------------------------------------------------------------------------
备注    : <灵活性不够>
--------------------------------------------------------------------------------
修改记录    :
日 期 版本 修改人 修改内容
31/07/2006 1.1 <William> <proxool连接池>
*******************************************************************************/

package com.passionsoft.util;

import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DatabaseManager {

  private static final Log logger = LogFactory.getLog(DatabaseManager.class);

  // 批处理
  private String[] str; // 批处理语句包

  private static int flag = 0; // 数组指针

  private int no = 0; // 语句数目

  public DatabaseManager() {
  }

  /**
   * 从连接池获得一个数据库连接
   *
   * @return Connection
   *
   */
  public Connection getConnection() {

    Connection conn = null;

    try {
      Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
      conn = DriverManager.getConnection("proxool.att");
    } catch (ClassNotFoundException e) {
      // 在classpath中未找到合适的驱动
      logger.error(e.getMessage());
    } catch (SQLException e) {
      // 获取连接失败
      logger.error(e.getMessage());
    }

    if (conn != null) {
      if (logger.isDebugEnabled()) {
        logger.debug("Get a Connection from Connection Pool");
      }
      // System.out.println("Get a Connection from Connection Pool");
    }

    return conn;// 返回该Connection
  }

  /**
   * 传入执行查询的语句,返回结果集
   *
   * @param Connection
   * conn
   * @param String
   * sql
   * @return ResultSet
   */
  public ResultSet executeQuery(Connection conn, String sql) {

    Statement stmt = null;// 声明Statement stmt
    ResultSet rs = null;// 声明ResultSet rs

    if (conn != null) {
      // Connection 不为null,执行查询

      // 若debug模式开启,则输出debug 信息
      if (logger.isDebugEnabled()) {
        logger.debug(sql);
      }

      try {
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
            ResultSet.CONCUR_READ_ONLY);// 通过Connection创建一个Statemet
        rs = stmt.executeQuery(sql);// 执行查询语句,
      } catch (SQLException sqlex) {
        logger.error(sqlex.getMessage());
        return null;
      }
      return rs;
    } else {
      // Connection 为null,输出debug信息,返回null
      if (logger.isDebugEnabled()) {
        logger.debug("Connection is null");
      }
      return null;
    }
  }

  /**
   * 传入执行数据更新的语句,返回更新结果,成功执行为真
   *
   * @param Connection
   * conn
   * @param String
   * sql
   * @return boolean
   */
  public boolean executeUpdate(Connection conn, String sql) {

    boolean status = false;// 执行结果,默认为false

    Statement stmt = null;// 声明Statement stmt

    if (conn != null) {
      // Connection 不为null,执行更新

      // 若debug模式开启,则输出debug 信息
      if (logger.isDebugEnabled()) {
        logger.debug(sql);
      }

      try {
        stmt = conn.createStatement();// 通过Connection创建一个Statemet
        int count = stmt.executeUpdate(sql);// 执行更新数据操作,返回影响的行数

        // 根据count值,判断执行的成功或失败
        if (count > 0)
          status = true;
      } catch (SQLException sqlex) {
        logger.error(sqlex.getMessage());
      }
    } else {
      // Connection 为null,输出debug信息,返回false
      if (logger.isDebugEnabled()) {
        logger.debug("Connection is null");
      }
    }
    return status;

  }

  /**
   * 数据库语句批处理操作
   * @param str[]
   * 批处理语句包
   * @param conn
   */
  public boolean executeUpdateBatch(Connection conn, String[] str) {
    
    boolean status = false;    //执行结果
    
    Statement stmt = null;
    
    if(conn != null){
      
      try{
        // 当全部批处理都正确时,才执行数据提交执行;否则所有语句都不被执行
        conn.setAutoCommit(false);
        
        stmt = conn.createStatement();
        int[] rows;
        
        //添加批处理操作
        for(int i=0;i<str.length;i++)
          stmt.addBatch(" " + i +" " + str[i]);
        
        rows = stmt.executeBatch();
        
        //提交事务
        conn.commit();
        
        if(rows.length == str.length)
          status = true;
        
      }catch(BatchUpdateException bue){
        int[] success_rows;
        success_rows = bue.getUpdateCounts();
        System.err.println("Success Counts: " + success_rows);
      }catch(SQLException e){
        e.printStackTrace();
      }
    }
    
    return status;
  }

  /**
   * 批处理操作的辅助方法——将sql语句添加进处理批次中
   *
   * @param sql
   * 处理语句
   * @return
   */
  public void addSql(String sql) {
    str[flag] = sql;
    flag++;

    // 防止数组越界
    if (flag >= no)
      flag = 0;
  }

  /**
   * 批处理操作的辅助方法——设置批处理语句数目
   *
   * @param i
   * 处理语句数目
   */
  public void setBatch(int i) {
    str = new String[i];
    no = i;
  }

  /**
   * 批处理操作的辅助方法——获取处理语句包
   *
   * @return
   */
  public String[] getBatch() {

    flag = 0; // 获取处理语句包后,将指针拨回
    return str;
  }

  /**
   * 释放从连接池取得的连接
   *
   * @param Connection
   * conn
   */
  public void releaseConnection(Connection conn) {

    // 释放连接
    if (conn != null) {
      try {
        conn.close();
        if (logger.isDebugEnabled()) {
          logger.debug("Released a Connection to Connection Pool");
        }
        // System.out.println("Released a Connection to Connection
        // Pool");
      } catch (SQLException sqlex) {
        logger.error(sqlex.getMessage());
      } finally {
        conn = null;
      }
    }
  }
}

注:william是我们团队的老大,这里面只有相关批处理操作是我写的,还没有经过测试哦。 Smile
Log对象是用的appach的一个调试工具类




话题树型展开
人气 标题 作者 字数 发贴时间
16565 关于Tomcat的Connection管理问题 cxp108 526 2006-09-11 10:52
14183 Re:关于Tomcat的Connection管理问题 lisliefor 130 2006-09-12 09:02
14061 Re:关于Tomcat的Connection管理问题 lisliefor 11 2006-09-12 09:09
14040 Re:关于Tomcat的Connection管理问题 lisliefor 1230 2006-09-12 09:15
14268 Re:关于Tomcat的Connection管理问题 lisliefor 5692 2006-09-12 09:18
14088 Re:关于Tomcat的Connection管理问题 lisliefor 130 2006-09-12 09:24
14217 Re:关于Tomcat的Connection管理问题 cxp108 181 2006-09-12 09:30
14563 Re:关于Tomcat的Connection管理问题 lisliefor 129 2006-09-13 13:09
14569 Re:关于Tomcat的Connection管理问题 wsfx 85 2006-09-14 16:35
14797 Re:关于Tomcat的Connection管理问题 cxp108 73 2006-09-15 08:39

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923