Black Horse Day11 DBCP Connection pool

Source: Internet
Author: User

Summary: The DBCP database connection pool is a database connection pool for Apache-developed databases:

To use this well-developed database connection pool:

1. Import the appropriate jar package:


2. Use in the class:

Method 1: Use Basicdatasource Way

Database:

Create Database Day11;

Use Day11;

CREATE TABLE Account (

ID int primary KEY auto_increment,

Name varchar (30),

Money double

);

The data is: a b c D E

Package Com.itheima.dbcp;import Java.sql.connection;import Java.sql.preparedstatement;import java.sql.ResultSet; Import Java.sql.sqlexception;import Org.apache.commons.dbcp.basicdatasource;public class DBCPDemo1 {public static void Main (string[] args) {Connection con = null; PreparedStatement PS = null; ResultSet rs = null;try{//mode 1: Basicdatasource datasource=new basicdatasource () using Basicdatasource method;// Registered Datasource.setdriverclassname ("Com.mysql.jdbc.Driver");//url Datasource.seturl ("jdbc:mysql://localhost:3306/ Day11 ");//username datasource.setusername (" root ");//Password Datasource.setpassword (" 169500 ");//Get Connection con= Datasource.getconnection ();p s = con.preparestatement ("SELECT * from Account"), rs = Ps.executequery (); while (Rs.next ()) {String name = rs.getstring ("name"); SYSTEM.OUT.PRINTLN (name);}} catch (SQLException e) {e.printstacktrace (); throw new RuntimeException ();} finally {if (rs! = null) {try {rs.close ();} cat CH (SQLException e) {e.printstacktrace ();} finally {rs = null;}} if (PS! = null) {try {ps.clOSE ();} catch (SQLException e) {e.printstacktrace ();} finally {PS = null;}} if (con! = null) {try {con.close ()} catch (SQLException e) {e.printstacktrace ();} finally {con = null;}}}}
Method 2: Use the configuration file using the Basicdatasourcefactory method:

To create a configuration file config.properties:

driverclassname=com.mysql.jdbc.driverurl=jdbc:mysql:///day11username=rootpassword=169500
Classes are used:

Package Com.itheima.dbcp;import Java.io.filereader;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.basicdatasource;import Org.apache.commons.dbcp.basicdatasourcefactory;public class DBCPDemo2 {public static void main (string[] args) {Connection con = null; PreparedStatement PS = null; ResultSet rs = null;try{//Mode 2: How to use Basicdatasourcefactory Properties Pro=new properties ();p ro.load (New FileReader ( DBCPDemo2.class.getClassLoader (). GetResource ("Dbcp.properties"). GetPath ())); Basicdatasourcefactory factory=new basicdatasourcefactory ();D atasource DataSource =factory.createdatasource (PRO); Con=datasource.getconnection ();p s = con.preparestatement ("SELECT * from Account"), rs = Ps.executequery (); while ( Rs.next ()) {String name = rs.getstring ("name"); SYSTEM.OUT.PRINTLN (name);}} catch (Exception e) {e.printstacktrace (); throw new RuntimeException ();} finally {if (rs! = null) {try {rs.close ()} catch (SQLException e) {e.printstacktrace ();} finally {rs = null;}} if (PS! = null) {try {ps.close ()} catch (SQLException e) {e.printstacktrace ();} finally {PS = null;}} if (con! = null) {try {con.close ()} catch (SQLException e) {e.printstacktrace ();} finally {con = null;}}}}
The Close method has actually been repaired in order to use the connection back to the database connection to the database connection pool.

Operation Result:




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Black Horse Day11 DBCP Connection pool

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.