配置文件:
scott
123456
oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@192.168.32.32:1521:orcl
3
30
20
3
15
实现类:
package com.niit.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;
import com.mchange.v2.c3p0.PoolBackedDataSource;
/**
* 数据库连接类工具
* @author Administrator
*
*/
public class UtilsSql {
private static UtilsSql install = new UtilsSql();
private static ComboPooledDataSource data;
private UtilsSql(){
data = new ComboPooledDataSource("data");
}
/**
* 获取连接
* @return 连接
* @throws SQLException 连接异常
*/
public static Connection getConnection() throws SQLException{
return data.getConnection();
}
public static UtilsSql getUtilsSql(){
return install;
}
/**
* 关闭连接
*/
public static void closeAll(Connection conn, Statement ment, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (ment != null) {
try {
ment.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
}
}