C3P0 database connection pool,

Source: Internet
Author: User

C3P0 database connection pool,

Use the C3P0 database connection pool

1. Copy jar package: c3p0-0.9.1.2.jar c3p0-0.9.1.2-jdk1.3.jar c3p0-oracle-thin-extras-0.9.1.2.jar (oracle required)

2. Write the preparation file under the src directory: c3p0-config.xml (name can only be this)

3. Class C3P0Util

C3p0-config.xml content:

<?xml version="1.0" encoding="UTF-8"?><c3p0-config><!-- This is default config! --><default-config><property name="initialPoolSize">10</property><property name="maxIdleTime">30</property><property name="maxPoolSize">100</property><property name="minPoolSize">10</property><property name="maxStatements">200</property></default-config><!-- This is my config for mysql--><named-config name="mysql"><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property><property name="user">root</property><property name="password"></property><property name="initialPoolSize">10</property><property name="maxIdleTime">30</property><property name="maxPoolSize">100</property><property name="minPoolSize">10</property><property name="maxStatements">200</property></named-config><!-- This is my config for oracle --><named-config name="oracle"><property name="driverClass">oracle.jdbc.driver.OracleDriver</property><property name="jdbcUrl">jdbc:oracle:thin:@localhost:1521:orcl</property><property name="user">scott</property><property name="password">liang</property><property name="initialPoolSize">10</property><property name="maxIdleTime">30</property><property name="maxPoolSize">100</property><property name="minPoolSize">10</property><property name="maxStatements">200</property></named-config></c3p0-config>

C3P0Util:

Package com. liang. util; 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 com. mchange. v2.c3p0. comboPooledDataSource;/***** database tool class * @ author liang **/public class C3P0Util {static ComboPooledDataSource cpds = null; static {// you can write the configuration file, if you want to change the database, it's simple // cpd= new ComboPooledDataSour Ce ("oracle"); // This is the oracle database cpds = new ComboPooledDataSource ("mysql "); // This is the mysql database}/*** obtain the database Connection * @ return Connection */public static Connection getConnection () {try {return CPPS. getConnection ();} catch (SQLException e) {e. printStackTrace (); return null ;}} /*** database close operation ** @ param conn * @ param st * @ param pst * @ param rs */public static void close (Connection conn, PreparedStatement pst, ResultSet rs) {if (r S! = Null) {try {rs. close () ;}catch (SQLException e) {e. printStackTrace () ;}} if (pst! = Null) {try {pst. close () ;}catch (SQLException e) {e. printStackTrace () ;}} if (conn! = Null) {try {conn. close ();} catch (SQLException e) {e. printStackTrace () ;}}/ *** test DBUtil class * @ param args */public static void main (String [] args) {Connection conn = getConnection (); System. out. println (conn. getClass (). getName (); close (conn, null, null );}}








Related Article

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.