C3P0数据库连接池
// 保证在所有的通过C3P0获取的连接中,只有一个DataSource的对象。(推荐)
private static DataSource source = null;
static {
source = new ComboPooledDataSource(“helloc3p0”);
// 获取数据库的连接方式3:使用c3p0数据库连接池获取数据库的连接,使用配置文件
public static Connection getConnection3() throws Exception {
return source.getConnection();
对应的配置文件:c3p0-config.xml
jdbc:mysql:///test
com.mysql.jdbc.Driver
root
123456
5
10
5
100
10
5
DBCP数据库连接池
//随着类的加载,使用BasicDataSourceFactory的静态方法createDataSource()返回一个
//DataSource的对象
private static DataSource source1 = null;
static {
Properties info = new Properties();
// info.load(new FileInputStream(“dbcp·properties”));
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream(
“com/atguigu/java/dbcp·properties”);
try {
info.load(is);
source1 = BasicDataSourceFactory.createDataSource(info);
} catch (Exception e) {
e.printStackTrace();
// 获取数据库的连接方式4:使用DBCP数据库连接池获取数据库的连接(推荐)
public static Connection getConnection5() throws Exception {
return source1.getConnection();
配置文件dbcp.properties:
username=root
password=123456
url=jdbc:mysql://127.0.0.1:3306/test
driverClassName=com.mysql.jdbc.Driver
initialSize=10
maxActive=100
文章转载链接:http://www.atguigu.com/jsfx/1132.html
特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.