來源:轉(zhuǎn)載 發(fā)布時間:2018-12-08 11:34:01 閱讀量:949
1、相關jar包,這里需要3個jar包
2、具體鏈接數(shù)據(jù)庫代碼
ComboPooledDataSource類繼承自AbstractComboPooledDataSource類,且AbstractComboPooledDataSource類實現(xiàn)了PooledDataSource接口
ComboPooledDataSource常用方法
類型
方法和功能
void
setDriverClass(java.lang.String driverClass)
設置連接數(shù)據(jù)庫的驅(qū)動名稱
void
setJdbcUrl(java.lang.String jdbcUrl)
設置連接數(shù)據(jù)庫的地址
void
setUser(java.lang.String user)
設置連接數(shù)據(jù)庫的用戶名
void
setPassword(java.lang.String password)
設置連接數(shù)據(jù)庫的密碼
void
setInitialPoolSize(int initialPoolSize)
設置數(shù)據(jù)庫連接池初始化的連接數(shù)目
void
setMaxPoolSize(int maxPoolSize)
設置最大數(shù)據(jù)庫連接池的數(shù)量
void
setMinPoolSize(int minPoolSize)
設置最小數(shù)據(jù)庫連接池的數(shù)量
Connection
getConnection()
從連接池獲取一個連接
構造方法
ComboPooledDataSource()
ComboPooledDataSource(boolean autoregister)
ComboPooledDataSource(java.lang.String configName)
(1)、通過ComboPooledDataSource類直接創(chuàng)建數(shù)據(jù)源對象
Example4.java
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
import java.sql.SQLException;
public class Example4{
public static DataSource dataSource = null;
//初始化C3P0數(shù)據(jù)源
static {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
try{
comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
comboPooledDataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/chapter02");
comboPooledDataSource.setUser("root");
comboPooledDataSource.setPassword("1234");
//初始化
comboPooledDataSource.setInitialPoolSize(5);
//設置最大的鏈接數(shù)
comboPooledDataSource.setMaxPoolSize(15);
dataSource = comboPooledDataSource;
}catch (Exception e){
}
}
public static void main(String[] args)throws SQLException {
System.out.println(dataSource.getConnection());
}
}
(2)、通過配置文件創(chuàng)建數(shù)據(jù)源對象
在項目的src目錄下創(chuàng)建一個出c3p0-donfig.xml文件
c3p0-donfig.xml內(nèi)容如下
<?xml version="1.0" encoding="utf-8" ?>
<c3p0-config>
<default-config>
<property name="user">root</property>
<property name="password">1234</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">
jdbc:mysql://localhost:3306/chapter02
</property>
<property name="checkoutTimeout">30000</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
</default-config>
<named-config name="dongyao">
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">15</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">
jdbc:mysql://localhost:3306/chapter02
</property>
<property name="user">root</property>
<property name="password">1234</property>
</named-config>
</c3p0-config>
其中<default-config>是默認配置,<named-config name=””>是自定義配置,一個配置文件中可以有一個或者多個自定義配置,調(diào)用ComboPoolDataSource(String configName)方法傳入<named-config>節(jié)點中name屬性的值即可創(chuàng)建C3P0數(shù)據(jù)源對象。
在項目的src目錄下創(chuàng)建一個Example5的類
Example5.java
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
import java.sql.SQLException;
public class Example5 {
public static DataSource dataSource = null;
static {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("dongyao");
dataSource = comboPooledDataSource;
}
public static void main(String[] args) throws SQLException {
System.out.println(dataSource.getConnection());
}
}
3、控制臺顯示