数据库连接dbcp$c3p0

帅比萌擦擦* 提交于 2019-12-26 05:26:24

 

<?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();
 }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!