<?xml version="1.0" encoding="utf-8"?>
<c3p0-config>
<!-- 注释 -->
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/heima</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">10</property>
<property name="checkoutTimeout">3000</property>
</default-config>
<named-config name="otherc3p0">
</named-config>
</c3p0-config>
package com.itheima.utils;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils2 {
private JDBCUtils2(){}
private static String driverClass;
private static String url;
private static String username;
private static String password;
public static void loadProperties() {
Properties pp = new Properties();
try {
pp.load(JDBCUtils2.class.getClassLoader().getResourceAsStream("config.properties"));
} catch (IOException e) {
e.printStackTrace();
}
driverClass = pp.getProperty("driverClass");
url = pp.getProperty("url");
username = pp.getProperty("username");
password = pp.getProperty("password");
}
static {
try {
loadProperties();
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
try {
return DriverManager.getConnection(url, username,password);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void release(Connection conn, Statement stat, ResultSet rs) {
try {
if (rs != null) {
rs.close();
rs = null;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (stat != null) {
stat.close();
stat = null;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public static void release(Connection conn, Statement stat) {
try {
if (stat != null) {
stat.close();
stat = null;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}