1 <?xml version="1.0" encoding="UTF-8"?> 2 3 -<c3p0-config> 4 5 6 -<default-config> 7 8 <property name="driverClass">com.mysql.jdbc.Driver</property> 9 10 <property name="jdbcUrl">jdbc:mysql://localhost:3306/dataSourceDemo?characterEncoding=UTF8</property>11 12 <property name="user">root</property>13 14 <property name="password">root</property>15 16 <property name="maxPoolSize">15</property>17 18 <property name="minPoolSize">3</property>19 20 <property name="initialPoolSize">3</property>21 22 <property name="acquireIncrement">3</property>23 24 <property name="maxIdleTime">600</property>25 26 <property name="checkoutTimeout">0</property>27 28 </default-config>29 30 </c3p0-config>
c3po-config.xml
1.导入XML文件
2.使用jdbcutils获取连接池对象
1 import java.beans.BeanInfo; 2 import java.beans.Introspector; 3 import java.beans.PropertyDescriptor; 4 import java.sql.Connection; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.ResultSetMetaData; 8 import java.sql.SQLException; 9 import java.sql.Statement; 10 import java.util.ArrayList; 11 import java.util.List; 12 13 import javax.sql.DataSource; 14 15 import com.mchange.v2.c3p0.ComboPooledDataSource; 16 17 public class JDBCUtils { 18 19 // 使用C3P0数据库连接池管理数据库连接 20 private static DataSource dataSource = new ComboPooledDataSource(); 21 22 // 获取连接 23 public static Connection getConnection() throws SQLException { 24 return dataSource.getConnection(); 25 } 26 27 // 关闭连接 28 public static void close(Connection conn) { 29 if (conn != null) { 30 try { 31 conn.close(); 32 } catch (SQLException e) { 33 } 34 } 35 } 36 37 // 关闭statement 38 public static void close(Statement stmt) { 39 if (stmt != null) { 40 try { 41 stmt.close(); 42 } catch (SQLException e) { 43 } 44 } 45 } 46 47 // 关闭结果集 48 public static void close(ResultSet rs) { 49 if (rs != null) { 50 try { 51 rs.close(); 52 } catch (SQLException e) { 53 } 54 } 55 } 56 57 // 关闭结果集、statement、连接 58 public static void closeAll(ResultSet rs) { 59 if (rs == null) { 60 return; 61 } 62 try { 63 close(rs); 64 // 如果先关掉statement再关conn,会抛出“You cannot operate on a closed 65 // Statement!”的异常,导致conn没有关闭 66 close(rs.getStatement().getConnection()); 67 close(rs.getStatement()); 68 69 } catch (SQLException e) { 70 e.printStackTrace(); 71 } 72 } 73 74 // 关闭结果集、statement 75 public static void closeResultSetAndStatement(ResultSet rs) { 76 if (rs == null) { 77 return; 78 } 79 try { 80 close(rs); 81 close(rs.getStatement()); 82 } catch (SQLException e) { 83 84 } 85 } 86 87 // 执行insert、update、delete等sql语句 88 public static int executeUpdate(String sql, Object... parameters) throws SQLException { 89 Connection conn = null; 90 try { 91 conn = getConnection(); 92 return executeUpdate(conn, sql, parameters); 93 } finally { 94 close(conn); 95 } 96 } 97 98 // 执行insert、update、delete等sql语句 99 public static int executeUpdate(Connection conn, String sql, Object... parameters) throws 100 SQLException {101 PreparedStatement ps = null;102 try {103 ps = conn.prepareStatement(sql);104 for (int i = 0; i < parameters.length; i++) {105 ps.setObject(i + 1, parameters[i]);106 }107 return ps.executeUpdate();108 } finally {109 close(ps);110 }111 }112 113 // 执行查询114 public static ResultSet executeQuery(String sql, Object... parameters) throws SQLException {115 Connection conn = null;116 try {117 conn = getConnection();118 return executeQuery(conn, sql, parameters);119 } catch (SQLException ex) {120 close(conn);121 throw ex;122 }123 }124 125 // 执行查询126 public static ResultSet executeQuery(Connection conn, String sql, Object... parameters) throws 127 SQLException {128 PreparedStatement ps = null;129 try {130 ResultSet rs = null;131 ps = conn.prepareStatement(sql);132 for (int i = 0; i < parameters.length; i++) {133 ps.setObject(i + 1, parameters[i]);134 }135 rs = ps.executeQuery();136 return rs;137 } catch (SQLException ex) {138 close(ps);139 throw ex;140 }141 }142 143 // 回滚144 public static void rollback(Connection conn) {145 try {146 conn.rollback();147 } catch (SQLException e) {148 149 }150 }151 }
JDBCUtils
jar包 c3p0