package C3P0ConnectionPool;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
public class C3P0Test {
@Test
public void TestGetConnection1() throws PropertyVetoException, SQLException {
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass( "com.mysql.jdbc.Driver" );
cpds.setJdbcUrl( "jdbc:mysql://localhost:3306/cc" );
cpds.setUser("root");
cpds.setPassword("root");
cpds.setInitialPoolSize(10);
Connection connection = cpds.getConnection();
System.out.println(connection);
}
@Test
public void TestGetConnection2() throws SQLException {
ComboPooledDataSource cpds = new ComboPooledDataSource("helloc3p0");
Connection connection = cpds.getConnection();
System.out.println(connection);
}
}
<c3p0-config>
<named-config name="helloc3p0">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/cc</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="acquireIncrement">5</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">10</property>
<property name="maxPoolSize">100</property>
<property name="maxStatements">50</property>
<property name="maxStatementsPerConnection">2</property>
</named-config>
</c3p0-config>
package DBCPConnectionPool;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
public class DBCPTest {
@Test
public void TestGetConnection() throws SQLException {
BasicDataSource source =new BasicDataSource();
source.setDriverClassName("com.mysql.jdbc.Driver");
source.setUrl("jdbc:mysql://localhost:3306/cc");
source.setUsername("root");
source.setPassword("root");
source.setInitialSize(10);
Connection connection = source.getConnection();
System.out.println(connection);
}
@Test
public void TestGetConnection1() throws Exception {
Properties properties = new Properties();
FileInputStream fileInputStream = new FileInputStream(new File("src/DBCP.properties"));
properties.load(fileInputStream);
BasicDataSource dataSource = BasicDataSourceFactory.createDataSource(properties);
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
}
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/cc
username=root
password=root
initialSize=10
package DruidConnectionPool;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.junit.Test;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;
public class DruidTest {
@Test
public void GetConnection() throws Exception {
Properties properties = new Properties();
InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
properties.load(resourceAsStream);
DataSource source = DruidDataSourceFactory.createDataSource(properties);
Connection connection = source.getConnection();
System.out.println(connection);
}
}
url=jdbc:mysql:///cc
username=root
password=root
driverClass=com.mysql.jdbc.Driver
initialSize=10
maxActive=10
|