Chinaunix首页 | 论坛 | 博客
  • 博客访问: 866273
  • 博文数量: 215
  • 博客积分: 10062
  • 博客等级: 上将
  • 技术积分: 2235
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-01 13:21
文章分类

全部博文(215)

文章存档

2012年(1)

2011年(24)

2009年(16)

2008年(91)

2007年(83)

我的朋友

分类:

2007-12-10 22:22:33

package com.itanger.web.Dao;
import java.sql.*;
import java.sql.*;
import org.apache.commons.dbcp.BasicDataS
文件: DBCP.rar
大小: 564KB
下载: 下载
ource;
/**
 *

Title: 使用DBCP连接池(以SQL SERVER 为例)


 *

Description:创建单实例模式


 *

Copyright: Copyright (c) 2006


 *

Company:


 * @author not attributable
 * @version 1.0
 */
public class DBManager {
  public DBManager() {
  }
  private static BasicDataSource instance = null;
  /**
   *   创建数据源的单个实例
   */
  public static BasicDataSource getInstance() {
    if (instance == null) {
      init();
    }
    return instance;
  }
  /**
   * 初始化数据源
   */
  private static void init() {
    instance = new BasicDataSource();
    instance.setDriverClassName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    instance.setUrl(
        "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=News;");
    instance.setUsername("sa");
    instance.setPassword("sa");
  }
  /**
   * 得到数据源中的一个实例
   * @return Connection
   */
  public Connection getConnection() {
    try {
      return instance.getConnection();
    }
    catch (SQLException ex) {
      return null;
    }
  }
  /**
   * 关闭结果集、陈述语句或者预编译语句、连接以释放资源
   * @param con Connection
   * @param stmt Statement
   * @param pstmt PreparedStatement
   * @param rs ResultSet
   */
  public static void close(Connection con, Statement stmt,
                           PreparedStatement pstmt, ResultSet rs) {
    try {
        if (con != null) {
        con.close();
      }
      if (stmt != null) {
        stmt.close();
      }
      if (pstmt != null) {
        pstmt.close();
      }
      if (rs != null) {
        rs.close();
      }
    }
    catch (SQLException ex) {
        ex.printStackTrace();
    }
  }
}
阅读(761) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~