1、utils 包右鍵 –> New -> Class
2踏幻、Name -> JDBCUtils -> Finish
3、JDBCUtils –> code內(nèi)容
package com.yg.utils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* @author 黑猴子的家
* @博客 http://www.reibang.com/u/37fd8e2dff4c
*/
public class JDBCUtils {
static ComboPooledDataSource cpds;
static {
// 1.創(chuàng)建連接池對象
cpds = new ComboPooledDataSource("c3p0config");
}
/**
* 獲取連接
*
* @return 可用的連接對象
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
return cpds.getConnection();
}
/**
* 釋放資源
*
* @param set
* @param statement
* @param connection
* @throws Exception
*/
public static void closeConnection(ResultSet set,
Statement statement,
Connection connection) throws Exception {
if (set != null)
set.close();
if (statement != null)
statement.close();
if (connection != null)
connection.close();
}
}