simpledbconnectionfactory.java

来自「opennms得相关源码 请大家看看」· Java 代码 · 共 170 行

JAVA
170
字号
//// This file is part of the OpenNMS(R) Application.//// OpenNMS(R) is Copyright (C) 2002-2003 The OpenNMS Group, Inc.  All rights reserved.// OpenNMS(R) is a derivative work, containing both original code, included code and modified// code that was published under the GNU General Public License. Copyrights for modified // and included code are below.//// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.// // Copyright (C) 1999-2001 Oculan Corp.  All rights reserved.//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.//// For more information contact://      OpenNMS Licensing       <license@opennms.org>//      http://www.opennms.org///      http://www.opennms.com/////// Tab Size = 8////package org.opennms.core.resource.db;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.Properties;/** * A trivial implementation of <code>DbConnectionFactory</code> that creates a * new connection for every request and does no caching or connection sharing. *  * <p> * Note that no real connection pooling is occurring within this class. This * factory will be an inefficient connection management scheme for most * real-world applications. It was designed only for development/debugging * purposes and for applications that are <strong>very </strong> infrequently * used. In those cases, caching idle database connections for long periods of * time can be wasteful, and this pooling scheme would be appropriate. * </p> *  * <p> * This manager simply initializes the JDBC driver and then stores the database * credential information (if any). Then when a connection is requested, a new * connection is made with the stored credentials (if any). * </p> *  * @author <A HREF="mailto:larry@opennms.org">Lawrence Karnowski </A> * @author <A HREF="http://www.opennms.org/">OpenNMS </A> */public class SimpleDbConnectionFactory extends Object implements DbConnectionFactory {    protected String url = null;    protected String username = null;    protected String password = null;    protected Properties properties = null;    /**     * Initialize a new database pool. This method will only load the JDBC     * driver.     */    public void init(String dbUrl, String dbDriver) throws ClassNotFoundException, SQLException {        if (dbUrl == null || dbDriver == null) {            throw new IllegalArgumentException("Cannot take null parameters.");        }        Class.forName(dbDriver);        this.url = dbUrl;    }    /**     * Initialize a new database pool with the given database username and     * password. This method will load the JDBC driver and store the given     * database credentials. When a connection is requested, a new connection     * will be made using the credentials.     */    public void init(String dbUrl, String dbDriver, String username, String password) throws ClassNotFoundException, SQLException {        if (dbUrl == null || dbDriver == null || username == null || password == null) {            throw new IllegalArgumentException("Cannot take null parameters.");        }        Class.forName(dbDriver);        this.url = dbUrl;        this.username = username;        this.password = password;    }    /**     * Initialize a new database pool with the given database properties. This     * method will load the JDBC driver and store the given database properties.     * When a connection is requested, a new connection will be made using the     * properties.     */    public void init(String dbUrl, String dbDriver, Properties properties) throws ClassNotFoundException, SQLException {        if (dbUrl == null || dbDriver == null || properties == null) {            throw new IllegalArgumentException("Cannot take null parameters.");        }        Class.forName(dbDriver);        this.url = dbUrl;        this.properties = properties;    }    /**     * Clear all database URL and credential information so no more connections     * can be requested.     */    public void destroy() {        this.url = null;        this.username = null;        this.password = null;        this.properties = null;    }    /**     * Create a new connection for the given database URL. This method will     * check to ensure that the URL has already been registered as a valid     * database pool. If any database credentials or properties were registered,     * those will be used when creating the new connection.     */    public Connection getConnection() throws SQLException {        if (this.url == null) {            throw new IllegalArgumentException("This database factory has not been initialized or has been destroyed.");        }        Connection connection = null;        if (this.properties != null) {            connection = DriverManager.getConnection(this.url, this.properties);        } else if (this.username != null && this.password != null) {            connection = DriverManager.getConnection(this.url, this.username, this.password);        } else {            connection = DriverManager.getConnection(this.url);        }        return (connection);    }    /**     * Close the given connection.     */    public void releaseConnection(Connection connection) throws SQLException {        if (connection == null) {            throw new IllegalArgumentException("Cannot take null parameters.");        }        connection.close();    }}

⌨️ 快捷键说明

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