Use the C3P0 database connection pool

Source: Internet
Author: User
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 );}}

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.