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

📄 datasourcewrapper.java

📁 JSP设计(第三版)一书源代码 JSP设计(第三版)》得到了充分的修订和更新
💻 JAVA
字号:
package com.ora.jsp.sql;

import java.io.*;
import java.sql.*;
import javax.sql.*;

/**
 * This class is a wrapper implementing the JDBC 2.0 DataSource
 * interface, used to make the ConnectionPool class look like
 * a JDBC 2.0 DataSource.
 *
 * @author Hans Bergsten, Gefion software <hans@gefionsoftware.com>
 * @version 2.0
 */
public class DataSourceWrapper implements DataSource {
    private ConnectionPool pool;
    private String driverClassName;
    private String url;
    private String user;
    private String password;
    private int initialConnections;

    /**
     * Sets the JDBC driver class name for the pool.
     */
    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    /**
     * Sets the JDBC URL for the pool.
     */
    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * Sets the user account for the pool.
     */
    public void setUser(String user) {
        this.user = user;
    }

    /**
     * Sets the user account password for the pool.
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Sets the number of connections to create when the pool is
     * created.
     */
    public void setInitialConnections(int initialConnections) {
        this.initialConnections = initialConnections;
    }

    /**
     * Gets a connection from the pool and returns it wrapped in
     * a ConnectionWrapper.
     */
    public Connection getConnection() throws SQLException {
        if (pool == null) {
            createConnectionPool();
        }
        return new ConnectionWrapper(pool.getConnection(), this);
    }
    
    /**
     * Returns a Connection to the pool. This method is called by
     * the ConnectionWrapper's close() method.
     */
    public void returnConnection(Connection conn) {
        pool.returnConnection(conn);
    }
    
    /**
     * Always throws an SQLException. Username and password are set
     * with the setter methods and can not be changed.
     */
    public Connection getConnection(String username, String password) 
            throws SQLException {
        throw new SQLException("Not supported");
    }
    
    /**
     * Always throws an SQLException. Not supported.
     */
    public int getLoginTimeout() throws SQLException {
        throw new SQLException("Not supported");
    }
    
    /**
     * Always throws an SQLException. Not supported.
     */
    public PrintWriter getLogWriter() throws SQLException {
        throw new SQLException("Not supported");
    }
    
    /**
     * Always throws an SQLException. Not supported.
     */
    public void setLoginTimeout(int seconds) throws SQLException {
        throw new SQLException("Not supported");
    }
    
    /**
     * Always throws an SQLException. Not supported.
     */
    public synchronized void setLogWriter(PrintWriter out) throws SQLException {
        throw new SQLException("Not supported");
    }

    /**
     * Create a Connection pool based on the configuration properties.
     */
    private void createConnectionPool() throws SQLException {

        try {
            pool = new ConnectionPool(driverClassName, url, user,
                password, initialConnections);
        }
        catch (SQLException e) {
            throw e;
        }
        catch (Exception e) {
            SQLException sqle = 
                new SQLException("Error creating pool: " + 
                    e.getClass().getName() + " : " + e.getMessage());
            throw sqle;
        }
    }
}

⌨️ 快捷键说明

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