Common configuration method of C3P0

Source: Internet
Author: User
Tags connection pooling postgresql

1: The first way is very simple

C3p0.driverclass=com.mysql.jdbc.driver
c3p0.jdbcurl=jdbc:mysql://localhost:3308/database
c3p0.user= Root
c3p0.password=root

File name: C3p0.properties (placed under the SRC directory)
The program I write is relatively simple and can be used to test execution configuration.

Package jdbc.mysql;

Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;


Import Com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3p0 {public
	static void Main (string[] args) throws SQLException {
	//database connection pooling will go to Classpath to find the configuration of the database by default.
		Combopooleddatasource data = new Combopooleddatasource ();
		Connection conn = Data.getconnection ();
		String sql = "SELECT * from table";
		PreparedStatement pstmt = conn.preparestatement (sql);
		ResultSet rs = Pstmt.executequery ();
		while (Rs.next ()) {
			System.out.println (rs.getstring (1));
		}
}}

2: The second way is very flexible and easy to use, but also very close to our usual hibernate and struts of the configuration file way
And it can provide services for multiple data sources, providing default-config and named-config two configuration modes.
In which the specific parameters can refer to the C3P0 document (http://www.mchange.com/projects/c3p0/) relatively simple. You can almost see the meaning by the name of the attribute.

<?xml version= "1.0" encoding= "UTF-8"?> <c3p0-config> <default-config> <property name= "User" > root</property> <property name= "password" >1234</property> <property name= "Driverclass" >com .mysql.jdbc.driver</property> <property name= "Jdbcurl" >jdbc:mysql://localhost:3306/database? characterencoding=utf-8&useoldaliasmetadatabehavior=true</property> <property name= "initialPoolSize ">2</property> <property name=" MaxIdleTime ">30</property> <property name=" Maxpoolsize "> 100</property> <property name= "minpoolsize" >2</property> </default-config> <named-co Nfig name= "Database" > <property name= "user" >root</property> <property name= "password" >root&lt ;/property> <property name= "Driverclass" >com.mysql.jdbc.Driver</property> <property name= "Jdbcur L ">jdbc:mysql://localhost:3308/database?characterencoding=utf-8&useoldaliasmetadatabehavior=true</property> <property name= "initialPoolSize ">2</property> <property name=" MaxIdleTime ">30</property> <property name=" Maxpoolsize "> 100</property> <property name= "minpoolsize" >2</property> </named-config> </c3p0-config& Gt
If you put C3p0-config.xml under the src directory, then you do not need to specify the configuration file, it will automatically find this profile, that is, in the program without writing System.setproperty (" Com.mchange.v2.c3p0.cfg.xml ", System.getproperty (" User.dir ") +"/config/c3p0-config.xml ");
Package jdbc.mysql;

Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;

Import Javax.sql.DataSource;

Import Com.mchange.v2.c3p0.ComboPooledDataSource;


public class C3p0jdbc {public
	static void Main (string[] args) throws SQLException {
		System.setproperty (" Com.mchange.v2.c3p0.cfg.xml ", System.getproperty (" User.dir ") +"/config/c3p0-config.xml ");
		The above profile  <default-config> 
		//constructor null value by default. Going to find the default configuration
		DataSource data = new Combopooleddatasource ();
		The configuration file above  <named-config name= "Database" >
		//If a value is passed in the constructor, the corresponding configuration is found.
		DataSource data1 = new Combopooleddatasource ("database");
		Connection conn = Data.getconnection ();
		Connection conn1 = Data1.getconnection ();
		String sql = "SELECT * from table";
		PreparedStatement pstmt = conn1.preparestatement (sql);
		ResultSet rs = Pstmt.executequery ();
		while (Rs.next ()) {
			System.out.println (rs.getstring (1));
		}
}}

3: This configuration is the least commonly used, more cumbersome, that is, all the configuration is written to the program.

Optional use

Import com.mchange.v2.c3p0.*;
........	
Combopooleddatasource CPDs = new Combopooleddatasource ();
Cpds.setdriverclass ("Org.postgresql.Driver"); Loads the JDBC driver            
cpds.setjdbcurl ("Jdbc:postgresql://localhost/testdb");
Cpds.setuser ("Dbuser");                                  
Cpds.setpassword ("Dbpassword");                                  

Cpds.setmaxstatements (180); 
Cpds.close ();


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.