public final class DriverUitl {
/ *c3p0 的方式 读取配置文件 连接数据库的方法
前提:1.导入相应的 jar 包(c3p0-0.9.1.2.jar)
2 . src 路径下存在 c3p0-config.xml 文件
*
* 配置文件 命名 为 : c3p0-config.xml
* <named-config name="Datbasec3p0">
*/
private static DataSource ds =null;
static {
//读取c3p0配置文件
ds = new ComboPooledDataSource("Datbasec3p0");
}
public static Connection getConnectionC3P() throws SQLException{
return ds.getConnection();
}
/*
* 读取properties 文件获取连接字符串的 公用
* 获取数据库连接的方法
* 获取数据库连接的公用方法
*/
public static Connection getConnection() throws Exception {
// 1创建读取配置文件的 对象
Properties pros = new Properties();
// 1 获取文件流 因为是静态中, 使用以下格式
InputStream inps = DriverUitl.class.getClassLoader().getResourceAsStream("jdbc.properties");
// 2 读取文件流
pros.load(inps);
// 3为 连接字符串赋值
String driver = pros.getProperty("driver");
String url = pros.getProperty("url");
String user = pros.getProperty("user");
String password = pros.getProperty("password");
// 2 加载驱动
Class.forName(driver);
// 3 获取数据库连接 并将其返回
return DriverManager.getConnection(url, user, password);
}
/*
* 关闭流的公用方法
*/
public static void close(ResultSet resultSet,
PreparedStatement statement, Connection connection) {
// 关闭 ResultSet流
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
System.out.println("关闭ResultSet 对象出现错误!");
e.printStackTrace();
}
}
// 关闭 PreparedStatement 流
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
System.out.println("关闭PreparedStatement 对象出现错误!");
e.printStackTrace();
}
}
// Connection 流
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
System.out.println("关闭Connection 对象出现错误!");
e.printStackTrace();
}
}
}
}
/ *c3p0 的方式 读取配置文件 连接数据库的方法
前提:1.导入相应的 jar 包(c3p0-0.9.1.2.jar)
2 . src 路径下存在 c3p0-config.xml 文件
*
* 配置文件 命名 为 : c3p0-config.xml
* <named-config name="Datbasec3p0">
*/
private static DataSource ds =null;
static {
//读取c3p0配置文件
ds = new ComboPooledDataSource("Datbasec3p0");
}
public static Connection getConnectionC3P() throws SQLException{
return ds.getConnection();
}
/*
* 读取properties 文件获取连接字符串的 公用
* 获取数据库连接的方法
* 获取数据库连接的公用方法
*/
public static Connection getConnection() throws Exception {
// 1创建读取配置文件的 对象
Properties pros = new Properties();
// 1 获取文件流 因为是静态中, 使用以下格式
InputStream inps = DriverUitl.class.getClassLoader().getResourceAsStream("jdbc.properties");
// 2 读取文件流
pros.load(inps);
// 3为 连接字符串赋值
String driver = pros.getProperty("driver");
String url = pros.getProperty("url");
String user = pros.getProperty("user");
String password = pros.getProperty("password");
// 2 加载驱动
Class.forName(driver);
// 3 获取数据库连接 并将其返回
return DriverManager.getConnection(url, user, password);
}
/*
* 关闭流的公用方法
*/
public static void close(ResultSet resultSet,
PreparedStatement statement, Connection connection) {
// 关闭 ResultSet流
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
System.out.println("关闭ResultSet 对象出现错误!");
e.printStackTrace();
}
}
// 关闭 PreparedStatement 流
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
System.out.println("关闭PreparedStatement 对象出现错误!");
e.printStackTrace();
}
}
// Connection 流
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
System.out.println("关闭Connection 对象出现错误!");
e.printStackTrace();
}
}
}
}