Dbcp:database Connection Pool
1, the need of Jar:commons-dbcp.jar Commons-pool.jar
2. Copy the DBCP configuration file (dbcpconfig.properties) to the build path
3. Create class: Dbcputil
Dbcpconfig.properties:
The MySQL setup content is as follows:
#连接设置driverClassName =com.mysql.jdbc.driverurl=jdbc:mysql://localhost:3306/day16username=rootpassword=liang# <!--Initialize connection-->initialsize=10# maximum number of connections maxactive=50#<!--maximum idle connection-->maxidle=20#<!--minimum idle connection-->minidle=5 #<!--Timeout Wait time in milliseconds of 6000 milliseconds/1000 equals 60 seconds-->maxwait=60000#jdbc the Connection property property that accompanies the driver when the connection is established must be in such a format: [property name =property;] #注意: "User" The two attributes with "password" are explicitly passed, so there is no need to include them here. connectionproperties=useunicode=true;characterencoding=utf8# Specifies the auto-commit (Auto-commit) state of the connection created by the connection pool. Defaultautocommit=true#driver default Specifies the read-only (read-only) state of the connection created by the connection pool. #如果没有设置该值, the "setreadonly" method will not be called. (Some drivers do not support read-only mode, such as Informix) defaultreadonly= #driver default Specifies the transaction level (transactionisolation) of the connection created by the connection pool. #可用值为下列之一: (Details visible Javadoc. ) none,read_uncommitted, read_committed, Repeatable_read, serializable#oracle only support read_committed (default), Serializabledefaulttransactionisolation=repeatable_read
Dbcputil:
Package Cn.itcast.util;import Java.io.inputstream;import Java.sql.connection;import java.sql.resultset;import Java.sql.sqlexception;import Java.sql.statement;import Java.util.properties;import Javax.sql.DataSource;import Org.apache.commons.dbcp.basicdatasourcefactory;public class Dbcputil {private static DataSource Ds;static{try { InputStream in = DBCPUtil.class.getClassLoader (). getResourceAsStream ("dbcpconfig.properties"); Properties props = new properties ();p rops.load (in);d s = basicdatasourcefactory.createdatasource (props);} catch (Exception e) {e.printstacktrace ();}} public static Connection getconnection () {try {return ds.getconnection ();} catch (SQLException e) {throw new runtimeexcept Ion (E);}} public static void Release (ResultSet rs,statement stmt,connection conn) {if (rs!=null) {try {rs.close ();} catch ( SQLException e) {e.printstacktrace ();} rs = null;} if (stmt!=null) {try {stmt.close ();} catch (SQLException e) {e.printstacktrace ();} stmt = null;} if (conn!=null) {try {conn.close ();} catch (SQLexception e) {e.printstacktrace ();} conn = null;}}}
DBCP Data sources using