Required Public jar package Mysql-connector-java-5.0.8-bin.jar (MySQL database) ojdbc14.jar (Oracle database)
A.DBCP requires jar package: Commons-dbcp.jar,commons-pool.jar and. Properties configuration file (directly in SRC directory)
#连接设置driverClassName=Com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/mysqlusername=Rootpassword=root#<!--Initialize connection-->initialsize=10#最大连接数量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驱动建立连接时附带的连接属性属性的格式必须为这样:[attribute name =property;]#注意:"User" and "password"Two attributes are explicitly passed, so there is no need to include them here. ConnectionProperties=useunicode=true;Characterencoding=utf8#指定由连接池所创建的连接的自动提交 (auto-commit) state. 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,serializabledefaulttransactionisolation=read_committed
View Code
Packagede.bvb.utils;ImportJava.io.InputStream;Importjava.sql.Connection;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;Importjava.util.Properties;ImportJavax.sql.DataSource;Importorg.apache.commons.dbcp.BasicDataSourceFactory; Public classJDBCUTILS_DBCP {Private StaticDataSource ds =NULL; Static { Try{InputStream in= jdbcutils_dbcp.class. getClassLoader (). getResourceAsStream ("Dbcpconfig.properties"); Properties prop=NewProperties (); Prop.load (in); DS=Basicdatasourcefactory.createdatasource (prop); } Catch(Exception e) {Throw NewExceptionininitializererror (e); } } Public StaticConnection getconnection ()throwsSQLException {returnds.getconnection (); } Public Static voidRelease (Connection Conn, Statement St, ResultSet rs) {if(rs! =NULL) { Try{rs.close ();//throw New}Catch(Exception e) {e.printstacktrace (); } RS=NULL; } if(St! =NULL) { Try{st.close (); } Catch(Exception e) {e.printstacktrace (); } St=NULL; } if(Conn! =NULL) { Try{conn.close (); } Catch(Exception e) {e.printstacktrace (); } } }}
B.C3P0 need jar Package: C3p0-0.9.2-pre1.jar,mchange-commons-0.2.jar and C3p0-config.xml configuration file (directly in SRC directory, and file name including suffix cannot be changed to other)
<C3p0-config> <Default-config> < Propertyname= "Driverclass">Com.mysql.jdbc.Driver</ Property> < Propertyname= "Jdbcurl">Jdbc:mysql://localhost:3306/mysql</ Property> < Propertyname= "User">Root</ Property> < Propertyname= "Password">Root</ Property> < Propertyname= "Initialpoolsize">10</ Property> < Propertyname= "MaxIdleTime">30</ Property> < Propertyname= "Maxpoolsize">20</ Property> < Propertyname= "Minpoolsize">5</ Property> < Propertyname= "Maxstatements">200</ Property> </Default-config> <Named-configname= "MySQL"> < Propertyname= "Acquireincrement">50</ Property> < Propertyname= "Initialpoolsize">100</ Property> < Propertyname= "Minpoolsize">50</ Property> < Propertyname= "Maxpoolsize">1000</ Property><!--Intergalactoapp adopts a different approach to configuring statement caching - < Propertyname= "Maxstatements">0</ Property> < Propertyname= "Maxstatementsperconnection">5</ Property> </Named-config> <Named-configname= "Oracle"> < Propertyname= "Acquireincrement">50</ Property> < Propertyname= "Initialpoolsize">100</ Property> < Propertyname= "Minpoolsize">50</ Property> < Propertyname= "Maxpoolsize">1000</ Property><!--Intergalactoapp adopts a different approach to configuring statement caching - < Propertyname= "Maxstatements">0</ Property> < Propertyname= "Maxstatementsperconnection">5</ Property> </Named-config></C3p0-config>
Packagede.bvb.utils;Importjava.sql.Connection;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;ImportCom.mchange.v2.c3p0.ComboPooledDataSource; Public classjdbcutils_c3p0 {Private StaticCombopooleddatasource ds =NULL; Static { Try{DS=NewCombopooleddatasource ();//load the default configuration in the XML file, that is, the configuration under <default-config>//ds = new Combopooleddatasource ("MySQL");//load the XML file in <named-config name= "MySQL" > the configuration below}Catch(Exception e) {Throw NewExceptionininitializererror (e); } } Public StaticConnection getconnection ()throwsSQLException {returnds.getconnection (); } Public Static voidRelease (Connection Conn, Statement St, ResultSet rs) {if(rs! =NULL) { Try{rs.close (); } Catch(Exception e) {e.printstacktrace (); } RS=NULL; } if(St! =NULL) { Try{st.close (); } Catch(Exception e) {e.printstacktrace (); } St=NULL; } if(Conn! =NULL) { Try{conn.close (); } Catch(Exception e) {e.printstacktrace (); } } }}
Javaweb database connection Pool Connection database