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 ();
}
}