网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.java 学习笔记.
.轻松掌握 Java 泛型 (第 4 部分).
.Sun.XVR-1200图形加速器的使用.
.什么是XSP?.
.Java 图形用户界面设计(上).
.从.class文件中寻找类名.
.学编程的人不能不看的好文章(1).
.JDBC编程基础第二部分.
.java写的文件浏览器.
.JBuilder2005实现重构之类内部提.
.java程序员:开发系统要注意的基本.
.办公新选择——Sun StarSuite 6..
.用Java得到Excel中Formula的值.
.Java咖啡馆(1)——叹咖啡.
.JDBC 程序实例.
.J2SE5.0新特性之ProcessBuilder.
.使用Observer模式.
.Java从入门到精通之过程篇.
.剖析事件监听和匿名类.
.X3D实战基础讲座之八.

一个 JDBC 连接池例子

发表日期:2008-1-5



  import Java.io.*;
  import java.sql.*;
  import java.util.*;
  import java.util.Date;
  
  /**
  * This class is a Singleton that provides Access to one or many
  * connection pools defined in a Property file. A client gets
  * access to the single instance through the static getInstance()
  * method and can then check-out and check-in connections from a pool.
  * When the client shuts down it should call the release() method
  * to close all open connections and do other clean up.
  */
  public class DBConnectionManager {
   static private DBConnectionManager instance; // The single instance
   static private int clients;
  
   private Vector drivers = new Vector();
   private PrintWriter log;
   private Hashtable pools = new Hashtable();
  
   /**
   * Returns the single instance, creating one if it's the
   * first time this method is called.
   *
   * @return DBConnectionManager The single instance.
   */
   static synchronized public DBConnectionManager getInstance() {
   if (instance == null) {
   instance = new DBConnectionManager();
   }
   clients++;
   return instance;
   }
  
   /**
   * A private constrUCtor since this is a Singleton
   */
   private DBConnectionManager() {
   init();
   }
  
   /**
   * Returns a connection to the named pool.
   *
   * @param name The pool name as defined in the properties file
   * @param con The Connection
   */
   public void freeConnection(String name, Connection con) {
   DBConnectionPool pool = (DBConnectionPool) pools.get(name);
   if (pool != null) {
   pool.freeConnection(con);
   }
   }
  
   /**
   * Returns an open connection. If no one is available, and the max
   * number of connections has not been reached, a new connection is
   * created.
   *
   * @param name The pool name as defined in the properties file
   * @return Connection The connection or null
   */
   public java.sql.Connection getConnection(String name) {
   DBConnectionPool pool = (DBConnectionPool) pools.get(name);
   if (pool != null) {
   return pool.getConnection();
   }
   return null;
   }
  
   /**
   * Returns an open connection. If no one is available, and the max
   * number of connections has not been reached, a new connection is
   * created. If the max number has been reached, waits until one
   * is available or the specified time has elapsed.
   *
   * @param name The pool name as defined in the properties file
   * @param time The number of milliseconds to wait
   * @return Connection The connection or null
   */
   public java.sql.Connection getConnection(String name, long time) {
   DBConnectionPool pool = (DBConnectionPool) pools.get(name);
   if (pool != null) {
   return pool.getConnection(time);
   }
   return null;
   }
  
   /**
   * Closes all open connections and deregisters all drivers.
   */
   public synchronized void release() {
   // Wait until called by the last client
   if (--clients != 0) {
   return;
   }
  
   Enumeration allPools = pools.elements();
   while (allPools.hasMoreElements()) {
   DBConnectionPool pool = (DBConnectionPool) allPools.nextElement();
   pool.release();
   }
   Enumeration allDrivers = drivers.elements();
   while (allDrivers.hasMoreElements()) {
   Driver driver = (Driver) allDrivers.nextElement();
   try {
   DriverManager.deregisterDriver(driver);
   log("Deregistered JDBC driver " + driver.getClass().getName());
   }
   catch (SQLException e) {
   log(e, "Can't deregister JDBC driver: " + driver.getClass().getName());
   }
   }
   }
  
   /**
   * Creates instances of DBConnectionPool based on the properties.
   * A DBConnectionPool can be defined with the following properties:
   *

   * <poolname>.url The JDBC URL for the database
   * <poolname>.user A database user (optional)
   * <poolname>.passWord A database user password (if user specified)
   * <poolname>.maxconn The maximal number of connections (optional)
   *

   *
   * @param props The connection pool properties
   */
   private void createPools(Properties props) {
   Enumeration propNames = props.propertyNames();
   while (propNames.hasMoreElements()) {
   String name = (String) propNames.nextElement();
   if (name.endsWith(".url")) {
   String poolName = name.substring(0, name.lastIndexOf("."));
   String url = props.getProperty(poolName + ".url");
   if (url == null) {
   log("No URL specified for " + poolName);
   continue;
   }
   String user = props.getProperty(poolName + ".user");
   String password = props.getProperty(poolName + ".password");
   String maxconn = props.getProperty(poolName + ".maxconn", "0");
   int max;
   try {
   max = Integer.valueOf(maxconn).intvalue();
   }
   catch (NumberformatException e) {
   log("Invalid maxconn value " + maxconn + " for " + poolName);
   max = 0;
   }
   DBConnectionPool pool =
   new DBConnectionPool(poolName, url, user, password, max);
   pools.put(poolName, pool);
   log("Initialized pool " + poolName);
   }
   }
   }
  
   /**
   * Loads properties and initializes the instance with its values.
   */
   private void init() {
   InputStream is = getClass().getResourceAsStream("/db.properties");
   Properties dbProps = new Properties();
   try {
   dbProps.load(is);
   }
   catch (Exception e) {
   System.err.println("Can't read the properties file. " +
   "Make sure db.properties is in the CLASSPATH");
   return;
   }
   String logFile = dbProps.getProperty("logfile", "DBConnectionManager.log");
   try {
   log = new PrintWriter(new FileWriter(logFile, true), true);
   }
   catch (IOException e) {
   System.err.println("Can't open the log file: " + logFile);
   log = new PrintWriter(System.err);
   }
   loadDrivers(dbProps);
   createPools(dbProps);
   }
  
   /**
   * Loads and registers all JDBC drivers. This is done by the
   * DBConnectionManager, as opposed to the DBConnectionPool,
   * since many pools may share the same driver.
   *
   * @param props The connection pool properties
上一篇:JDBC3.0 新特性 人气:1203
下一篇:JDBC 解决方案 人气:922
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐