C3p0 the writing of the JDBC Connection pool XML configuration file

Source: Internet
Author: User
Tags stub

Need to create a c3p0-config.xml configuration file

* C3P0 configuration file
* 1. Configuration file name: C3p0-config.xml
* 2. The location of the configuration file must be under the Classpath

Copy

Modify a file

Capitalize the first letter to lowercase

Configuration file Setup Complete

Remember to import the database connection jar package

C3P0 jar Package

Code

1  Packagetest03;2 3 Importjava.beans.PropertyVetoException;4 Importjava.sql.Connection;5 ImportJava.sql.ResultSet;6 Importjava.sql.SQLException;7 8 ImportCom.mchange.v2.c3p0.ComboPooledDataSource;9 Ten /** One * C3P0 Connection data source A  * @authorStar -  * -  */ the  Public classC3p0test { -      Public Static voidMain (string[] args)throwsException { -         //test1 (); - test2 (); +     } -      +     Private Static voidTest1 ()throwsSQLException, propertyvetoexception { A         //TODO auto-generated Method Stub at         //Create a connection pool -Combopooleddatasource pool =NewCombopooleddatasource (); -          -         //four parameters of the set connection -         // -Pool.setdriverclass ("Com.mysql.jdbc.Driver"); inPool.setjdbcurl ("Jdbc:mysql:///day01"); -Pool.setuser ("root"); toPool.setpassword ("root"); +         // -         //Get Connections theConnection conn =pool.getconnection (); *String sql = "SELECT * from Stu"; $ResultSet rs =conn.createstatement (). executeQuery (SQL);Panax Notoginseng          while(Rs.next ()) { -System.out.println (Rs.getint (1) + "" +rs.getstring (2)); the         } + rs.close (); A conn.close (); the     } +     //using configuration Files -     /* $ * C3P0 configuration file $ * 1. Configuration file name: C3p0-config.xml - * 2. The location of the configuration file must be under the Classpath -      */ the     Private Static voidTest2 ()throwsException { -         //TODO auto-generated Method StubWuyi         //c3p0 Creating a connection pool object theCombopooleddatasource pool =NewCombopooleddatasource (); -         //Get Connections WuConnection conn =pool.getconnection (); -String sql = "SELECT * from Stu"; AboutResultSet rs =conn.createstatement (). executeQuery (SQL); $          while(Rs.next ()) { -System.out.println (Rs.getint (1) + "" +rs.getstring (2)); -         } - rs.close (); A conn.close (); +          the     } -      $}
c3p0 Connection Pool

1 <C3p0-config>2   <Default-config>3     < Propertyname= "Driverclass">Com.mysql.jdbc.Driver</ Property>4     < Propertyname= "Jdbcurl">Jdbc:mysql:///day01</ Property>5     < Propertyname= "User">Root</ Property>6     < Propertyname= "Password">Root</ Property>7     8 9   </Default-config>Ten </C3p0-config>
C3p0-config.xml

C3p0 Creating a Connection pool object
Combopooleddatasource pool = new Combopooleddatasource ();

C3p0 the writing of the JDBC Connection pool XML configuration file

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.