Java uses DBCP MySQL connection pool for database operations

Source: Internet
Author: User

Required jar packages are commons-dbutils, COMMONS-DBCP, Commons-pool, Mysql-connector-java

The local database.propertties is configured to

driverclass=com.mysql.jdbc.Driverurl=jdbc:mysql://Localhost/mydatabase?useunicode=true &CHARACTERENCODING=GBKusername=rootpassword=***

The file is created under the SRC root directory

First do a DBCP tool class

1  PackageJdbcdemo;2 3 Importjava.io.IOException;4 ImportJava.io.InputStream;5 Importjava.util.Properties;6 7 ImportJavax.activation.DataSource;8 9 ImportOrg.apache.commons.dbcp.BasicDataSource;Ten  One  Public classDbcputils { A     Privatedbcputils () {}; -     Private StaticBasicdatasource dbs =NewBasicdatasource (); -     Private StaticString classname= ""; the     Private StaticString sqlurl= ""; -     Private StaticString sqluser= ""; -     Private StaticString sqlpassword= ""; -     Static { + initconfig (); -         //set up basic information + Dbs.setdriverclassname (className); A Dbs.seturl (sqlurl); at Dbs.setusername (sqluser); - Dbs.setpassword (sqlpassword); -         //number of connection pool controls--Not available -Dbs.setinitialsize (10);//Number of connection pool initialization connections -Dbs.setmaxactive (8);//Maximum number of links -Dbs.setmaxidle (5);//Maximum idle number inDbs.setminidle (5);//Minimum idle number -     } to      Public StaticBasicdatasource Getdatasource () { +         returnDBS; -     } the     Private Static voidinitconfig () { *InputStream inf = jdbcutils.class. getClassLoader (). getResourceAsStream ("Database.propertties"); $Properties Properties =NewProperties ();Panax Notoginseng         Try { - Properties.load (INF); theClassName = Properties.getproperty ("Driverclass"); +Sqlurl = properties.getproperty ("url"); ASqlUser = Properties.getproperty ("username"); theSQLPassword = Properties.getproperty ("Password"); +}Catch(IOException e) { - e.printstacktrace (); $         } $     } -}

Application of this tool class combined with dbutils written demo

1  PackageJdbcdemo;2 3 Importjava.io.IOException;4 ImportJava.io.InputStream;5 Importjava.sql.Connection;6 Importjava.sql.SQLException;7 Importjava.util.Properties;8 Importjavax.management.RuntimeErrorException;9 ImportOrg.apache.commons.dbcp.BasicDataSource;Ten ImportOrg.apache.commons.dbutils.QueryRunner; One  A  -  Public classDbcpdemo { -     Private StaticQueryrunner QR =NewQueryrunner (Dbcputils.getdatasource ()); the      Public Static voidMain (string[] args) { -         Try { - Insert (); -}Catch(Exception e) { +E.printstacktrance (); -         } +     }     A      Public Static voidInsert ()throwsSQLException { atString sql = "INSERT into Zhangwu (Name,money) VALUES (?,?)"; -object[] obj = {"Mahjong expense", 100}; -         intres =qr.update (sql,obj); -         if(res>0) { -System.out.println ("Insert data Success"); -}Else { inSystem.out.println ("Insert data Failed"); -         } to     } +}

Java uses DBCP MySQL connection pool for database operations

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.