Java uses DBCP connection pooling to create tool classes

Source: Internet
Author: User

1. Description

There is an extension package in Java Javax has a dataresource interface below Javax.sql.DataResource

This interface defines the method specification for connection pooling

While the DBCP framework has been developed by Apache, he implements the method of interface Dataresource definition.

His realization class is Basicdataresorce org.apache.commons.dbcp.BasicDataSource;

2. Write the class that defines the connection pool
 Packagedemo;/** Use DBCP to implement database connection pool * Connection pool configuration, custom class, * Most basic four items complete * For database connection pool Four other items, custom*/ImportJavax.sql.DataSource;ImportOrg.apache.commons.dbcp.BasicDataSource;//implementation class of DataSource Public classjdbcutils{//Create a Basicdatasource class object    Private StaticBasicdatasource datasource=NewBasicdatasource (); //static code base, object Basicdatasource configuration in object, custom    Static{        //database connection information, requiredDatasource.setdriverclassname ("Com.mysql.jdbc.Driver"); Datasource.seturl ("Jdbc:mysql://localhost:3306/mybase"); Datasource.setusername ("Root"); Datasource.setpassword ("123456"); //To set the number of connections in a connection pool configuration, optional configuration ItemsDatasource.setinitialsize (10);//number of connections initializedDatasource.setmaxactive (8);//Maximum number of connectionsDatasource.setmaxidle (5);//Maximum idle numberDatasource.setminidle (1);//Minimum idle number    }        //defines a static method that returns an object of the Basicdatasource class     Public StaticDataSource Getdatasource () {returnDataSource; }}
3. Implement database operation with Queryrunner method in Dbutils framework
 Packagedemo;/** Test Written tool class, * provided is a DataSource interface data source * Queryrunner class construction method, accept DataSource interface implementation class * behind, call method Update,query, without passing them connection Linked Objects*/Importjava.sql.SQLException;Importjava.util.List;ImportOrg.apache.commons.dbutils.QueryRunner;ImportOrg.apache.commons.dbutils.handlers.ArrayListHandler;ImportDemo. Jdbcutils; Public classqueryrunnerdemo{ Public Static voidMain (string[] args) {//Insert ();Select (); }                //Define 2 methods for adding data tables, querying data tables//Queyrunnner Class object, unloading class member location    Private StaticQueryrunner qr=NewQueryrunner (Jdbcutils.getdatasource ()); //data Table Query     Public Static voidSelect () {String SQL= "SELECT * from Sort"; Try{List<Object[]> list=qr.query (SQL,NewArraylisthandler ());  for(object[] objs:list) { for(Object obj:objs) {System.out.print (obj+ "\ T");        } System.out.println (); }        }Catch(SQLException e) {e.printstacktrace (); Throw NewRuntimeException ("Query data Failed"); }    }        //data Table Add data     Public Static voidInsert () {String SQL= "INSERT into sort (SNAME,SPRICE,SDESC) VALUES (?,?,?)"; Object[] Params={"Fruit", 100.12, "just listed Walnut"}; Try{        intnum=qr.update (Sql,params);        SYSTEM.OUT.PRINTLN (num); }Catch(SQLException e) {e.printstacktrace (); Throw NewRuntimeException ("Data addition failed"); }    }}

Java uses DBCP connection pooling to create tool classes

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.