Use of DBCP database connection pool

Source: Internet
Author: User
Packagetk. dong. connectionPool; importjava. io. IOException; importjava. io. inputStream; importjava. SQL. connection; importjava. SQL. preparedStatement; importjava. SQL. resultSet; importjava. SQL. SQLException; importjava. util. properties; importjav

Package tk. dong. connectionPool; import java. io. IOException; import java. io. inputStream; import java. SQL. connection; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. SQLException; import java. util. properties; import jav

Package tk. dong. connectionPool; import java. io. IOException; import java. io. inputStream; import java. SQL. connection; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. SQLException; import java. util. properties; import javax. SQL. dataSource; import org. apache. commons. dbcp. basicDataSourceFactory; public class Pool_dbcp {// first import the package commons-pool.jar and commons-dbcp-1.4.jar // declare the data source private stat Ic DataSource; private static PreparedStatement pstmt; private static ResultSet rs; static {// read the configuration file into InputStream inputStream = Pool_dbcp.class.getClassLoader () as an input stream (). getResourceAsStream ("dbcp. properties "); // create the property operation object Properties properties = new Properties (); try {// read the configuration file into properties. load (inputStream); // create a basic data source to process the object BasicDataSourceFactory basicDataSourceFactory = new BasicDataSourceFa of the factory class Ctory (); // create the data source dataSource = basicperformancefactory through the basic data processing factory. createDataSource (properties);} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace () ;}}// method for creating the returned Connection object public static Connection getConn () {try {return dataSource. getConnection ();} catch (SQLException e) {// TODO Auto-generated catch block E. printStackTrace ();} return null;} public static void release (ResultSet rs, PreparedStatement pstmt) {// release the result set if (rs! = Null) {try {rs. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}}// release preparation statement if (pstmt! = Null) {try {pstmt. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}}// add, delete, and modify the encapsulation method public static boolean upDate (String SQL, Object [] obj) {boolean flag = false; try {// creation of the prepared statement, object with SQL command pstmt = getConn (). prepareStatement (SQL); for (int I = 1; I <= obj. length; I ++) {pstmt. setObject (I, obj [I-1]);} int I = pstmt.exe cuteUpdate (); if (I> 0) {flag = true ;} Catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}finally {release (rs, pstmt) ;}return flag ;}// batch Delete public static boolean updateBatchDel (String SQL, Object [] ids) {boolean flag = false; Connection conn = getConn (); PreparedStatement pstmt = null; ResultSet rs = null; try {conn. setAutoCommit (false); pstmt = conn. prepareStatement (SQL); for (int I = 0; I <ids. length; I ++) {Pstmt. setObject (1, ids [I]); System. out. println (SQL + "---------------" + ids [I]); pstmt. addBatch () ;}int [] num = pstmt.exe cuteBatch (); // run for (int I = 0; I <num. length; I ++) {if (num [I] = 0) {try {conn. rollback (); // perform transaction rollback return flag;} catch (SQLException ex) {ex. printStackTrace () ;}} conn. commit (); // submit the transaction flag = true;} catch (SQLException e) {e. printStackTrace ();} finally {release (rs, pstm T);} return flag;} // obtain all the pages of the input table based on the name of the input table and the data on each page. // tableName ::::: name of the data table for the Operation // pagesize :::::: number of information entries displayed on each page public static Integer getCountPage (String tableName, Integer pagesize) {Integer countPage = 0; string SQL = "select count (*) as c from" + tableName; Connection conn = Pool_dbcp.getConn (); PreparedStatement pstmt = null; ResultSet rs = null; conn = Pool_dbcp.getConn (); try {pstmt = conn. prepareStatement (SQL); Rs = pstmt.exe cuteQuery (); if (rs. next () {int countRecord = rs. getInt ("c"); countPage = countRecord % pagesize = 0? CountRecord/pagesize: countRecord/pagesize + 1 ;}} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}finally {Pool_dbcp.release (rs, pstmt);} return countPage ;}}

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.