如何使用c3p0技术构建数据库连接池?

更新于
2026-10-04 12:58:25
2阅读来源:SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计183个文字,预计阅读时间需要1分钟。

javapackage com.cn.utils;

import javax.sql.DataSource;import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0Utils { private static DataSource dataSource;

static { dataSource=new ComboPooledDataSource(); }}

gistfile1.txt

package com.cn.utils; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; //c3p0 会自动读取c3p0-config.xml文件(默认放在class目录下,会被自动读取) public class C3P0Utils { private static DataSource source; static{ source = new ComboPooledDataSource();//可以在c3p0的配置文件配置,选择要使用的数据库,不指定就使用默认配置项 //读取c3p0-config.xml文件中的信息,默认读取default-config配置的信息 } public static DataSource getSource(){ return source; } //不读取c3p0-config.xml,手动配置的方式 /*private static ComboPooledDataSource com =null; static{ try { com.setDriverClass("com.mysql.jdbc.Driver"); com.setJdbcUrl("jdbc:mysql://localhost:3306/web"); com.setUser("root"); com.setPassword("123456"); com.setMaxPoolSize(20); com.setMinPoolSize(10); com.setInitialPoolSize(5); } catch (Exception e) { throw new ExceptionInInitializerError(e); } }*/ }

本文共计183个文字,预计阅读时间需要1分钟。

javapackage com.cn.utils;

import javax.sql.DataSource;import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0Utils { private static DataSource dataSource;

static { dataSource=new ComboPooledDataSource(); }}

gistfile1.txt

package com.cn.utils; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; //c3p0 会自动读取c3p0-config.xml文件(默认放在class目录下,会被自动读取) public class C3P0Utils { private static DataSource source; static{ source = new ComboPooledDataSource();//可以在c3p0的配置文件配置,选择要使用的数据库,不指定就使用默认配置项 //读取c3p0-config.xml文件中的信息,默认读取default-config配置的信息 } public static DataSource getSource(){ return source; } //不读取c3p0-config.xml,手动配置的方式 /*private static ComboPooledDataSource com =null; static{ try { com.setDriverClass("com.mysql.jdbc.Driver"); com.setJdbcUrl("jdbc:mysql://localhost:3306/web"); com.setUser("root"); com.setPassword("123456"); com.setMaxPoolSize(20); com.setMinPoolSize(10); com.setInitialPoolSize(5); } catch (Exception e) { throw new ExceptionInInitializerError(e); } }*/ }