package com.hong.dao;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.junit.Test;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3P0App {
@Test
public void testComboPooledDataSource(){
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl("jdbc:mysql:///test");
try {
dataSource.setDriverClass("com.mysql.jdbc.Driver");
} catch (PropertyVetoException e1) {
throw new RuntimeException(e1);
}
dataSource.setUser("root");
dataSource.setPassword("825z.jkc76");
dataSource.setInitialPoolSize(3);
dataSource.setMaxPoolSize(6);
dataSource.setMaxIdleTime(3000);
try {
Connection conn = dataSource.getConnection();
String sql ="select * from userinfo;";
PreparedStatement prep =conn.prepareStatement(sql);
ResultSet result = prep.executeQuery();
while(result.next()){
System.out.println(result.getString("name"));
System.out.println(result.getString("password"));
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Test
public void testConfigComboPooledDataSource(){
ComboPooledDataSource dataSource = new ComboPooledDataSource();
try {
Connection conn = dataSource.getConnection();
String sql ="select * from userinfo;";
PreparedStatement prep =conn.prepareStatement(sql);
ResultSet result = prep.executeQuery();
while(result.next()){
System.out.println(result.getString("name"));
System.out.println(result.getString("password"));
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
c3p0创建连接池
最新推荐文章于 2024-08-01 15:05:06 发布