package jdbc.connection_pool.c3p0;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class C3P0Demo {
@Test
public void manual() throws SQLException, PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/jdbc");
dataSource.setUser("root");
dataSource.setPassword("root");
dataSource.setAcquireIncrement(5);
dataSource.setInitialPoolSize(20);
dataSource.setMinPoolSize(2);
dataSource.setMaxPoolSize(50);
Connection con = dataSource.getConnection();
Statement stmt = con.createStatement();
String sql = "SELECT * FROM nametb";
ResultSet rs = stmt.executeQuery(sql);
int count = rs.getMetaData().getColumnCount();
while (rs.next()) {
for (int i = 1; i <= count; i++) {
System.out.print(rs.getString(i));
if (i < count) {
System.out.print("\t");
}
}
System.out.println();
}
rs.close();
stmt.close();
con.close();
}
@Test
public void automatic() throws SQLException, PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
Connection con = dataSource.getConnection();
Statement stmt = con.createStatement();
String sql = "SELECT * FROM team";
ResultSet rs = stmt.executeQuery(sql);
int count = rs.getMetaData().getColumnCount();
while (rs.next()) {
for (int i = 1; i <= count; i++) {
System.out.print(rs.getString(i));
if (i < count) {
System.out.print("\t");
}
}
System.out.println();
}
rs.close();
stmt.close();
con.close();
}
@Test
public void automaticByName() throws SQLException {
ComboPooledDataSource dataSource = new ComboPooledDataSource("oracle-config");
Connection con = dataSource.getConnection();
System.out.println(con);
con.close();
}
}