A tool for c3p0 connection pool

Source: Internet
Author: User

Note: required jar packages:

C3p0-0.9.1.2-jdk1.3.jar

C3p0-0.9.1.2.jar

C3p0-oracle-thin-extras-0.9.1.2.jar

You also need a configuration file to store the database connection data.

DB. Properties


Package CN. c3p0;


Import java. SQL. connection;
Import java. SQL. resultset;
Import java. SQL. sqlexception;


Import com. mchange. v2.c3p0. combopooleddatasource;
Import com. MySQL. JDBC. preparedstatement;


// C3p0 connection pool: Note: XML files must be placed under SRC
Public class c3p0utils {
Private Static combooleddatasource DS = new combopooleddatasource ();


Public static connection getconnection (){
Try {
Return Ds. getconnection ();
} Catch (sqlexception e ){
Throw new runtimeexception (E );
}
}


// Close the resource and return the retrieved Resource
Public static void closejdbc (connection Conn, resultset St,
Preparedstatement PS ){
If (Conn! = NULL ){
Try {
Conn. Close ();
} Catch (sqlexception e ){
E. printstacktrace ();
}
}
If (st! = NULL ){
Try {
St. Close ();
} Catch (sqlexception e ){
E. printstacktrace ();
}
}
If (PS! = NULL ){
Try {
PS. Close ();
} Catch (sqlexception e ){
E. printstacktrace ();
}
}
}
}


Test class

Package CN. c3p0;


Import java. SQL. connection;


Import com. MySQL. JDBC. preparedstatement;
Import com. MySQL. JDBC. statement;


Public class test {
Private Static connection conn;
Private Static statement st;
Private preparedstatement pS;
Public static void add (){
Conn = c3p0utils. getconnection ();
System. Out. println (conn );
C3p0utils. closejdbc (Conn, null, null );
}
Public static void main (string [] ARGs ){
Add ();
}
}


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.