⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbmanager.java

📁 OA典型例子
💻 JAVA
字号:
package com.sure.dataabstraction;

import java.sql.Connection;
import java.lang.reflect.Constructor;
import java.sql.ResultSet;
import java.sql.Statement;
/**
 *  Description of the Class
 *
 *@author     Mengzy
 *@date       2002-8-16
 */
class DestroyHook extends Thread {
    private ConnectionPool pool;


    /**
     *  Constructor for the DestroyHook object
     *
     *@param  connPool  Description of the Parameter
     */
    public DestroyHook(ConnectionPool connPool) {
	pool = connPool;
    }


    /**
     *  Main processing method for the DestroyHook object
     */
    public void run() {
	if (pool == null) {
	    return;
	}
	pool.destroy();
    }
}

/**
 *  manager all database connection
 *
 *@author     Administrator
 *@created    2002年1月23日
 */
public class DBManager {


    private static ConnectionPool connectionPool;
    //init ConnectionPool object when this class was loaded by ClassLoader
    static {
	if (connectionPool == null) {
	    DatabaseInfo info = DatabaseInfoFactory.getDefaultDatabaseInfo();
	    String className = info.getConnectionPoolName();
	    if (className == null) {
		try {
		    boolean debug = Boolean.valueOf(info.getProperty("debug", "false")).booleanValue();
		    connectionPool = new ConnectionPoolImpl(info);
		    ((ConnectionPoolImpl) connectionPool).setDebug(debug);
		} catch (DBException e) {
		    System.err.println(e.getMessage());
		}
	    } else {
		try {
		    Class cl = Class.forName(className);
		    Constructor constructor = cl.getConstructor(new Class[]{DatabaseInfo.class});
		    connectionPool = (ConnectionPool) constructor.newInstance(new Object[]{info});
		} catch (NoSuchMethodException ne) {
		    //maybe the class has not (databaseInfo DatabaseInfo) method
		    try {
			connectionPool = new ConnectionPoolImpl(info);
		    } catch (DBException e1) {
			System.err.println(e1.getMessage());
		    }
		} catch (ClassCastException ce) {
		    //maybe the class do not a ConnectionPool sub class
		    try {
			connectionPool = new ConnectionPoolImpl(info);
		    } catch (DBException e1) {
			System.err.println(e1.getMessage());
		    }
		} catch (Exception e) {
		    System.err.println(e.getMessage());
		}
	    }
	    //end of class name == null
	    if (connectionPool != null) {
		//add shutdownHook , it will be invoke when JVM shutdown
		Runtime.getRuntime().addShutdownHook(new DestroyHook(connectionPool));
	    }
	}
    }


    /**
     *@return                      Connection
     *@exception  DBPoolException
     *@roseuid                     3BEA1CEB00B6
     */
    public static Connection getConnection() throws DBPoolException {
	return connectionPool.getConnection();
    }


    /**
     *@return    DBConnectionPool
     */
    public static ConnectionPool getConnectionPool() {
	return connectionPool;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -