C3P0 Database Connection Pool usage

Source: Internet
Author: User

C3P0 Database Connection Pool usage

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 (required by Oracle)

2, write the preparation file in the SRC directory: c3p0-config.xml (the 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 was 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 was 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 Tools class * @author Liang * */public class C3p0util {static Combopooleddatas Ource cpds=null;static{//Here is a good thing, write the configuration file, want to change the database, simple//cpds = new Combopooleddatasource ("Oracle");//This is the Oracle database CPDs = new Combopooleddatasource ("MySQL");//This is the MySQL database}/** * Get database connection * @return Connection */public static Connection getconnection () {try {return cpds.getconnection ();} catch (SQLException e) {e.printstacktrace (); return null;}} /** * Database off operation * @param conn * @param St * @param PST * @param rs */public static void Close (Connection conn,prepared Statement Pst,resultset rs) {if (rs!=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);}}


C3P0 Database Connection Pool usage

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.