<?xml version="1.0" encoding="UTF-8"?>

<c3p0-config>

<!-- 这是默认配置信息 -->

<default-config name="hoobey">

<!-- 连接四大参数配置 -->

<property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property>

<property name="driverClass">com.mysql.jdbc.Driver</property>

<property name="user">root</property>

<property name="password">123456</property>

<!-- 池参数配置 -->

<property name="acquireIncrement">3</property>

<property name="initialPoolSize">10</property>

<property name="minPoolSize">2</property>

<property name="maxPoolSize">10</property>

</default-config>

</c3p0-config>

package cn.itcast.demo1;

import java.beans.PropertyVetoException;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import org.junit.Test;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**

* c3p0

* @author cxf

*

*/

public class c3p0 {

/**

* 代码配置

* @throws PropertyVetoException

* @throws SQLException

*/

@Test

public void fun1() throws PropertyVetoException, SQLException {

// 创建连接池对象

ComboPooledDataSource dataSource = new ComboPooledDataSource();

// 对池进行四大参数的配置

dataSource.setDriverClass("com.mysql.jdbc.Driver");

dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mydb1");

dataSource.setUser("root");

dataSource.setPassword("123456");

// 池配置

dataSource.setAcquireIncrement(5);

dataSource.setInitialPoolSize(20);

dataSource.setMinPoolSize(2);

dataSource.setMaxPoolSize(50);

Connection con = dataSource.getConnection();

String sql = "select * from products";

Statement stmt = con.createStatement();

ResultSet rs = (ResultSet) stmt.executeQuery(sql);

int count = rs.getMetaData().getColumnCount();

while(rs.next()){

for(int i=1; i<=count; i++){

System.out.println(rs.getString(i));

if(i<count){

System.out.println(",");

}

}

}

rs.close();

stmt.close();

con.close();

}

/**

* 配置文件的默认配置

* @throws SQLException

*/

@Test

public void fun2() throws SQLException{

/**

* 在创建连接池对象时,这个对象就会自动加载配置文件!不用我们来指定

*/

ComboPooledDataSource dataSource  = new ComboPooledDataSource();

Connection con = dataSource.getConnection();

System.out.println(con);

String sql = "select * from products";

Statement stmt = con.createStatement();

ResultSet rs = (ResultSet) stmt.executeQuery(sql);

int count = rs.getMetaData().getColumnCount();

while(rs.next()){

for(int i=1; i<=count; i++){

System.out.println(rs.getString(i));

if(i<count){

System.out.println(",");

}

}

}

rs.close();

stmt.close();

con.close();

con.close();

}

/**

* 使用命名配置信息

* @throws SQLException

*/

@Test

public void fun3() throws SQLException{

/**

* 构造器的参数指定命名配置元素的名称!

* <named-config name="oracle-config">

*/

ComboPooledDataSource dataSource  = new ComboPooledDataSource("hoobey");

Connection con = dataSource.getConnection();

System.out.println(con.getClass().getName());

con.close();

}

}

package cn.itcast.demo1;

import java.beans.PropertyVetoException;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import org.apache.commons.dbcp.BasicDataSource;

import org.junit.Test;

/**

* c3p0

* @author cxf

*

*/

public class dbcp {

/**

* 代码配置

* @throws PropertyVetoException

* @throws SQLException

*/

@Test

/*

* dbcp连接池 commons-dbcp-1.4.jar commons-pool-1.3.jar

*/

public void fun1() throws PropertyVetoException, SQLException {

// 创建连接池对象

BasicDataSource dataSource = new BasicDataSource();

// 对池进行四大参数的配置

dataSource.setDriverClassName("com.mysql.jdbc.Driver");

dataSource.setUrl("jdbc:mysql://localhost:3306/mydb1");

dataSource.setUsername("root");

dataSource.setPassword("123456");

// 池配置

dataSource.setMaxActive(20);

dataSource.setMinIdle(3);

dataSource.setMaxWait(200);

Connection con = dataSource.getConnection();

System.out.println(con.getClass().getName());

String sql = "select * from products";

Statement stmt = con.createStatement();

ResultSet rs = (ResultSet) stmt.executeQuery(sql);

int count = rs.getMetaData().getColumnCount();

while(rs.next()){

for(int i=1; i<=count; i++){

System.out.println(rs.getString(i));

if(i<count){

System.out.println(",");

}

}

}

rs.close();

stmt.close();

con.close();

}

}

package cn.itcast.jdbc;

import java.sql.Connection;

import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JdbcUtils {

// 配置文件的默认配置!要求你必须给出c3p0-config.xml!!!

private static ComboPooledDataSource dataSource = new ComboPooledDataSource();

/**

* 使用连接池返回一个连接对象

* @return

* @throws SQLException

*/

public static Connection getConnection() throws SQLException {

return dataSource.getConnection();

}

/**

* 返回连接池对象!

* @return

*/

public static DataSource getDataSource() {

return dataSource;

}

}

-------------------------------------------------------------------------------------------

public void fun4() throws SQLException{
  System.out.println("---------最简单的获取连接对象的方法---------------------------------------");
  Connection con = JdbcUtil.getConnection();
  String sql = "select * from products";
  Statement stmt = con.createStatement();
  ResultSet rs = (ResultSet) stmt.executeQuery(sql);
  int count = rs.getMetaData().getColumnCount();
  while(rs.next()){
   for(int i=1; i<=count; i++){
    System.out.println(rs.getString(i));
    if(i<count){
     System.out.println(",");
    }
   }
  }
  rs.close();
  stmt.close();
  con.close();
  con.close();
 }

数据库连接dbcp$c3p0的更多相关文章

  1. 数据库连接JDBC和数据库连接池C3P0自定义的java封装类

    数据库连接JDBC和数据库连接池C3P0自定义的java封装类 使用以下的包装类都需要自己有JDBC的驱动jar包: 如 mysql-connector-java-5.1.26-bin.jar(5.1 ...

  2. 数据库连接池 c3p0 druid

    druid 数据库连接池 c3p0 使用C3P0数据源时需要依赖 mchange-commons-java-0.2.3.4.jar包.缺少该jar包则会报错!

  3. 数据库连接池——C3P0&Druid(快速入门)

    数据库连接池--C3P0&Druid (一) 数据库连接池 每一个事物都有其存在的意义,在初学jdbc的时候,我们建立数据库连接对象后,会对其进行释放,但是数据库连接的建立和关闭是非常消耗资源 ...

  4. 数据库连接池c3p0和dbcp

    现在常用的开源数据连接池主要有c3p0.dbcp和proxool三种,其中: hibernate开发组推荐使用c3p0; spring开发组推荐使用dbcp(dbcp连接池有weblogic连接池同样 ...

  5. 数据库连接池c3p0学习

    这里只记录c3p0的数据源,不会涉及到其它方面和别的数据库连接池的对比 配置文件主要的实现方式有三种: 1.手写代码去加载一个配置文件 创建一个config.properties文件如下: drive ...

  6. [数据库连接池二]Java数据库连接池--C3P0和JDNI.

    前言:上一篇文章中讲了DBCP的用法以及实现原理, 这一篇再来说下C3P0和JDNI的用法. 1.1.C3P0数据源 C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规 ...

  7. 数据库连接池 c3p0 demo 代码和分析

    import java.sql.Connection; import java.sql.SQLException; import java.beans.PropertyVetoException; i ...

  8. 数据库连接池 C3p0

    数据库连接池 C3po 1 定义 本质上是个容器(集合) 存放数据库的连接容器(connection 对象) ,当系统初始化以后 容器就会创建 容器中就会申请一些连接对象 ,当用户来访问数据库的时候 ...

  9. java学习笔记41(数据库连接池 C3p0连接池)

    在之前的学习中,我们发现,我们需要频繁的创建连接对象,用完之后还需要在关闭资源,因为这些连接对象都是占资源的,但是又不得不创建,比较繁琐,为了解决这种情况,Java出现了数据库连接池: 数据库连接池的 ...

  10. JAVA数据库连接池C3p0 以及阿里Druid提供的连接池

    一:连接池的定义 本质上就是个容器(集合) 存放数据库连接的容器,当系统初始化后,容器被创建,容器中就会申请一些连接对象,当用户来访问数据库的时候,从容器中取连接对象,用户用完之后,归还. 二:常用的 ...

随机推荐

  1. Valid Palindrome(LintCode)

    Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...

  2. Loj#6434「PKUSC2018」主斗地(搜索)

    题面 Loj 题解 细节比较多的搜索题. 首先现将牌型暴力枚举出来,大概是\(3^{16}\)吧. 然后再看能打什么,简化后无非就三种决策:单牌,\(3+x\)和\(4+x\). 枚举网友打了几张\( ...

  3. ALL运算符

    ALL在英文中的意思是“所有”,ALL运算符要求比较的值需要匹配子查询中的所有值.ALL运算符同样不能单独使用,必须和比较运算符共同使用. 下面的SQL语句用来检索在所有会员入会之前出版的图书: SE ...

  4. 【BFS/DFS/YY】派对灯

    [luogu1468]派对灯 题目描述 在IOI98的节日宴会上,我们有N(10<=N<=100)盏彩色灯,他们分别从1到N被标上号码. 这些灯都连接到四个按钮: 按钮1:当按下此按钮,将 ...

  5. Gym 101128F Sheldon Numbers(网络流)

    [题目链接] http://codeforces.com/gym/101128/attachments [题目大意] 给出一张地图,分为高地和低地,高低地的交界线上划有红线, 现在要开小车跨过每条红线 ...

  6. 【置换群】【枚举约数】hdu6038 Function

    把b数组的所有置换群求出来,用数组记录一下每个大小所出现的次数. 然后求a的置换群,对每个置换群求能被其整除的b的置换群的大小总和(只有这些才能满足构造出一个f,且不自相矛盾),然后把它们全都乘起来就 ...

  7. Redis简单入门认识

    写在前面: 最近一直都在按照老大的学习路线来进行学习,这几天看了下redis,从刚开始的摸不着头脑,到后面慢慢的查资料,对其逐渐有了简单的了解,也通过一个与ssm框架整合的小demo去动手实践 了,知 ...

  8. Problem A: 零起点学算法91——找出一个数组中出现次数最多的那个元素

    #include<stdio.h> int main() { ],b[]={}; while(scanf("%d",&n)!=EOF) { ;i<n;i+ ...

  9. Codeforces Gym 100269B Ballot Analyzing Device 模拟题

    Ballot Analyzing Device 题目连接: http://codeforces.com/gym/100269/attachments Description Election comm ...

  10. 免费网站监控服务阿里云监控,DNSPod监控,监控宝,360云监控使用对比

    网站会因为各种原因而导致宕机,具体表现为服务器没有响应,用户打不开网页,域名解析出错,搜索引擎抓取页面失败,返回各种HTTP错误代码.网站宕机可能带来搜索引擎的惩罚,网站服务器不稳定与百度关系文章中就 ...