DBCP Data sources using

Source: Internet
Author: User
Tags time in milliseconds

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.