1. 下載c3p0-0.9.1.2.jar 鏈接:https://pan.baidu.com/s/1UAWHRC24gulNRRdWpqDGWQ 密碼:tf6d
2.添加配置文件c3p0-config.xm
3、配置文件內容如下:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<c3p0-config>
-
<!-- This is default config! -->
-
<default-config>
-
<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>
-
-
<!-- This is my config for mysql-->
-
<named-config name="mysql">
-
<property name="driverClass">com.mysql.jdbc.Driver </property>
-
<property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=UTF8 </property>
-
<property name="user">root </property>
-
<property name="password"> </property>
-
<!-- 初始化連接池中的連接數,取值應在minPoolSize與maxPoolSize之間,默認為3-->
-
<property name="initialPoolSize">10 </property>
-
<!--最大空閑時間,60秒內未使用則連接被丟棄。若為0則永不丟棄。默認值: 0 -->
-
<property name="maxIdleTime">30 </property>
-
<!--連接池中保留的最大連接數。默認值: 15 -->
-
<property name="maxPoolSize">100 </property>
-
<!-- 連接池中保留的最小連接數,默認為:3-->
-
<property name="minPoolSize">10 </property>
-
<!--c3p0全局的PreparedStatements緩存的大小。如果maxStatements與maxStatementsPerConnection均為0,則緩存不生效,只要有一個不為0,則語句的緩存就能生效。如果默認值: 0-->
-
<property name="maxStatements">200 </property>
-
<!-- 當連接池連接耗盡時,客戶端調用getConnection()后等待獲取新連接的時間,超時后將拋出SQLException,如設為0則無限期等待。單位毫秒。默認: 0 -->
-
<property name="checkoutTimeout" value="3000"/>
-
<!--當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數。默認值: 3 -->
-
<property name="acquireIncrement" value="2"/>
-
<!--定義在從數據庫獲取新連接失敗后重復嘗試的次數。默認值: 30 ;小於等於0表示無限次-->
-
<property name="acquireRetryAttempts" value="0"/>
-
<!--重新嘗試的時間間隔,默認為:1000毫秒-->
-
<property name="acquireRetryDelay" value="1000" />
-
<!--關閉連接時,是否提交未提交的事務,默認為false,即關閉連接,回滾未提交的事務 -->
-
<property name="autoCommitOnClose">false </property>
-
<!--c3p0將建一張名為Test的空表,並使用其自帶的查詢語句進行測試。如果定義了這個參數那么屬性preferredTestQuery將被忽略。你不能在這張Test表上進行任何操作,它將只供c3p0測試使用。默認值: null -->
-
<property name="automaticTestTable">Test </property>
-
<!--如果為false,則獲取連接失敗將會引起所有等待連接池來獲取連接的線程拋出異常,但是數據源仍有效保留,並在下次調用getConnection()的時候繼續嘗試獲取連接。如果設為true,那么在嘗試獲取連接失敗后該數據源將申明已斷開並永久關閉。默認: false-->
-
<property name="breakAfterAcquireFailure">false </property>
-
<!--每60秒檢查所有連接池中的空閑連接。默認值: 0,不檢查 -->
-
<property name="idleConnectionTestPeriod">60 </property>
-
<!--maxStatementsPerConnection定義了連接池內單個連接所擁有的最大緩存statements數。默認值: 0 -->
-
<property name="maxStatementsPerConnection"> </property>
-
</named-config>
-
-
-
<!-- This is my config for oracle -->
-
<named-config name="oracle">
-
<property name="driverClass">oracle.jdbc.driver.OracleDriver </property>
-
<property name="jdbcUrl">jdbc:oracle:thin:@localhost:1521:orcl </property>
-
<property name="user">scott </property>
-
<property name="password">liang </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>
-
</named-config>
-
</c3p0-config>
4、連接池連接類
-
package com.xxx.utils;
-
-
import java.sql.Connection;
-
import java.sql.SQLException;
-
-
import javax.sql.DataSource;
-
-
import com.mchange.v2.c3p0.ComboPooledDataSource;
-
-
public class JDBCUtil {
-
-
private static DataSource dataSource= null;
-
static{
-
dataSource= new ComboPooledDataSource( "mysql");
-
}
-
-
/**
-
* 獲取數據庫連接
-
* @return
-
*/
-
public static Connection getConnection(){
-
Connection conn= null;
-
try {
-
conn=dataSource.getConnection();
-
} catch (SQLException e) {
-
e.printStackTrace();
-
}
-
return conn;
-
}
-
-
-
/**
-
* 關閉數據庫連接
-
* @param conn
-
*/
-
public static void closeConn(Connection conn){
-
try {
-
if(conn!= null && conn.isClosed()){
-
conn.close();
-
}
-
} catch (SQLException e) {
-
e.printStackTrace();
-
}
-
}
-
}
5、測試
-
package com.xxx.test;
-
-
import java.sql.Connection;
-
import java.sql.PreparedStatement;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
-
import org.junit.Test;
-
-
import com.xxx.utils.JDBCUtil;
-
-
public class TestJdbc {
-
-
@Test
-
public void test() {
-
Connection conn=JDBCUtil.getConnection();
-
System.out.println(conn);
-
try {
-
PreparedStatement stmt=conn.prepareStatement( "select * from tb_user");
-
ResultSet re=stmt.executeQuery();
-
while(re.next()){
-
String name=re.getString( 2);
-
System.out.println(name);
-
-
}
-
-
-
} catch (SQLException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
-
-
-
}
-