C3P0 database connection pool using 1, copy jar package: c3p0-0.9.1.2.jarc3p0-0.9.1.2-jdk1.3.jarc3p0-oracle-thin-extras-0.9.1.2.jar (oracle needs) 2, write preparation file under the src directory: c3p0-config.xml (name can only be this) 3, class C3P0Utilc3p0-config.xml content :? Xmlversion
C3P0 database connection pool using 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 needs) 2, write preparation file under the src directory: c3p0-config.xml (name can only be this) 3, C3P0Util c3p0-config.xml content :? Xml version
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:
10
30
100
10
200
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/test
root
10
30
100
10
200
oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@localhost:1521:orcl
scott
liang
10
30
100
10
200
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 );}}