Database Connection Dbcp$c3p0

Source: Internet
Author: User
Tags connection pooling

<?xml version="1.0" encoding="UTF-8"?>

<c3p0-config>

<!-- This is the default configuration information --

<default-config name="Hoobey">

<!-- connection four parameter configurations --

<property name="Jdbcurl">jdbc:mysql://localhost:3306/mydb1</property>

<property name="Driverclass">com.mysql.jdbc.Driver</property>

<property name="user">root</property>

<property name="password">123456</property>

<!-- Pool parameter configuration --

<property name="acquireincrement">3</property>

<property name="initialpoolsize">10</property>

<property name="minpoolsize">2</property>

<property name="maxpoolsize">10</property>

</default-config>

</c3p0-config>

Package cn.itcast.demo1;

Import java.beans.PropertyVetoException;

Import java.sql.Connection;

Import Java.sql.ResultSet;

Import java.sql.SQLException;

Import java.sql.Statement;

Import Org.junit.Test;

Import Com.mchange.v2.c3p0.ComboPooledDataSource;

/**

* C3P0

* @author CXF

*

*/

public class C3p0 {

/**

* Code configuration

* @throws propertyvetoexception

* @throws SQLException

*/

@Test

public void Fun1 () throws Propertyvetoexception, SQLException {

To create a connection pool object

Combopooleddatasource DataSource = new Combopooleddatasource ();

four-parameter configuration of the pool

Datasource.setdriverclass ("Com.mysql.jdbc.Driver");

Datasource.setjdbcurl ("jdbc:mysql://localhost:3306/mydb1");

Datasource.setuser ("root");

Datasource.setpassword ("123456");

Pool Configuration

Datasource.setacquireincrement (5);

Datasource.setinitialpoolsize (20);

Datasource.setminpoolsize (2);

Datasource.setmaxpoolsize (50);

Connection con = datasource.getconnection ();

String sql = "SELECT * FROM Products";

Statement stmt = Con.createstatement ();

ResultSet rs = (ResultSet) stmt.executequery (SQL);

int count = Rs.getmetadata (). getColumnCount ();

while (Rs.next ()) {

for (int i=1; i<=count; i++) {

System.out.println (rs.getstring (i));

if (I<count) {

System.out.println (",");

}

}

}

Rs.close ();

Stmt.close ();

Con.close ();

}

/**

* default configuration for configuration files

* @throws SQLException

*/

@Test

public void Fun2 () throws sqlexception{

/**

* When creating a connection pool object, this object will automatically load the configuration file! We don't have to specify

*/

Combopooleddatasource DataSource = new Combopooleddatasource ();

Connection con = datasource.getconnection ();

System.out.println (con);

String sql = "SELECT * FROM Products";

Statement stmt = Con.createstatement ();

ResultSet rs = (ResultSet) stmt.executequery (SQL);

int count = Rs.getmetadata (). getColumnCount ();

while (Rs.next ()) {

for (int i=1; i<=count; i++) {

System.out.println (rs.getstring (i));

if (I<count) {

System.out.println (",");

}

}

}

Rs.close ();

Stmt.close ();

Con.close ();

Con.close ();

}

/**

* using named configuration information

* @throws SQLException

*/

@Test

public void Fun3 () throws sqlexception{

/**

* constructor parameter specifies the name of the named configuration element!

* <named-config name= "Oracle-config" >

*/

Combopooleddatasource DataSource = new Combopooleddatasource ("Hoobey");

Connection con = datasource.getconnection ();

System.out.println (Con.getclass (). GetName ());

Con.close ();

}

}

Package cn.itcast.demo1;

Import java.beans.PropertyVetoException;

Import java.sql.Connection;

Import Java.sql.ResultSet;

Import java.sql.SQLException;

Import java.sql.Statement;

Import Org.apache.commons.dbcp.BasicDataSource;

Import Org.junit.Test;

/**

* C3P0

* @author CXF

*

*/

public class DBCP {

/**

* Code configuration

* @throws propertyvetoexception

* @throws SQLException

*/

@Test

/*

* DBCP Connection pool Commons-dbcp-1.4.jar Commons-pool-1.3.jar

*/

public void Fun1 () throws Propertyvetoexception, SQLException {

To create a connection pool object

Basicdatasource DataSource = new Basicdatasource ();

four-parameter configuration of the pool

Datasource.setdriverclassname ("Com.mysql.jdbc.Driver");

Datasource.seturl ("jdbc:mysql://localhost:3306/mydb1");

Datasource.setusername ("root");

Datasource.setpassword ("123456");

Pool Configuration

Datasource.setmaxactive (20);

Datasource.setminidle (3);

Datasource.setmaxwait (200);

Connection con = datasource.getconnection ();

System.out.println (Con.getclass (). GetName ());

String sql = "SELECT * FROM Products";

Statement stmt = Con.createstatement ();

ResultSet rs = (ResultSet) stmt.executequery (SQL);

int count = Rs.getmetadata (). getColumnCount ();

while (Rs.next ()) {

for (int i=1; i<=count; i++) {

System.out.println (rs.getstring (i));

if (I<count) {

System.out.println (",");

}

}

}

Rs.close ();

Stmt.close ();

Con.close ();

}

}

Package CN.ITCAST.JDBC;

Import java.sql.Connection;

Import java.sql.SQLException;

Import Javax.sql.DataSource;

Import Com.mchange.v2.c3p0.ComboPooledDataSource;

public class Jdbcutils {

configuration file Default configuration! Request you must give out c3p0-config.xml!!!

private static Combopooleddatasource DataSource = new Combopooleddatasource ();

/**

* use connection pooling to return a connection object

* @return

* @throws SQLException

*/

public static Connection getconnection () throws SQLException {

return Datasource.getconnection ();

}

/**

* Return the connection pool Object!

* @return

*/

public static DataSource Getdatasource () {

return dataSource;

}

}

-------------------------------------------------------------------------------------------

public void Fun4 () throws sqlexception{
System.out.println ("---------The simplest way to get a Connection object---------------------------------------");
Connection con = jdbcutil.getconnection ();
String sql = "SELECT * FROM Products";
Statement stmt = Con.createstatement ();
ResultSet rs = (ResultSet) stmt.executequery (SQL);
int count = Rs.getmetadata (). getColumnCount ();
while (Rs.next ()) {
for (int i=1; i<=count; i++) {
System.out.println (rs.getstring (i));
if (I<count) {
System.out.println (",");
}
}
}
Rs.close ();
Stmt.close ();
Con.close ();
Con.close ();
}

Database Connection Dbcp$c3p0

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.