DBCP Connection pool-mode 1 by reading the configuration file

Source: Internet
Author: User


Connection Pool Core class DataSource

Based on this core class, there are several ways to create a connection pool, and here's how to create a connection pool for the database by reading the configuration file.

Dbcputils.java

Package Com.itheima.a_dbcp;import Java.io.inputstream;import Java.sql.connection;import java.sql.SQLException; Import Java.util.properties;import Javax.sql.datasource;import org.apache.commons.dbcp.BasicDataSourceFactory; public class Dbcputils {private static DataSource DataSource; static{try {//* provides parameters, content from config file properties props = new Propertie S ();//*** load InputStream is = DBCPUtils.class.getClassLoader (). getResourceAsStream ("dbcpconfig.properties"); Props.load (IS);//* Create a connection pool, just create one time DataSource = Basicdatasourcefactory.createdatasource (props);} catch (Exception e) {throw new RuntimeException (e);}} public static Connection getconnection () throws sqlexception{//get connection from Connection pool return datasource.getconnection ();}}

Testdbcppool.java


Package Com.itheima.a_dbcp;import Java.sql.connection;import Java.sql.sqlexception;public class TestDBCPPool {public static void Main (string[] args) throws SQLException {Connection conn = dbcputils.getconnection (); SYSTEM.OUT.PRINTLN (conn); Conn.close ();}}

Todo

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.