JDBC工具类使用C3P0连接池

来源:互联网 发布:studded mac 编辑:程序博客网 时间:2024/04/28 16:22
import java.sql.Connection;import java.sql.SQLException;import javax.sql.DataSource;import org.apache.commons.dbutils.QueryRunner;import com.mchange.v2.c3p0.ComboPooledDataSource;/** * jdbc工具类 * @author Zhang * */public class JdbcUtil {    /**     * 初始化C3P0连接池     */    private static DataSource dataSource;    static{        dataSource = new ComboPooledDataSource();    }    /**     * 创建jdbc核心工具类     */    public static QueryRunner getQueryRunner(){        return new QueryRunner(dataSource);    }    /**     * 从数据源中获取一个连接对象     *      */    public static Connection getConnection(){        try {            return dataSource.getConnection();        } catch (SQLException e) {            throw new RuntimeException(e);        }    }}

需要配合使用c3p0-config.xml

<c3p0-config>    <default-config>        <property name="jdbcUrl">jdbc:oracle:thin:@localhost:1521:orcl        </property>        <property name="driverClass">oracle.jdbc.driver.OracleDriver</property>        <property name="user">zc</property>        <property name="password">100700</property>        <property name="initialPoolSize">10</property>        <property name="maxPoolSize">20</property>        <property name="maxIdleTime">1000</property>    </default-config></c3p0-config>
原创粉丝点击