import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DataSourceUtil
{
private static DataSource ds;
static
{
// 读取c3p0的配置文件
ds =new ComboPooledDataSource();
}
public static DataSource getDataSource()
{
return ds;
}
public static Connection getConn()
{ Connection conn = null;
try {
conn = ds.getConnection();
}
catch (SQLException e)
{
e.printStackTrace();
}
return conn;
}
}
链接数据库的c3p0的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="checkoutTimeout">30000</property>
<property name="idleConnectionTestPeriod">30</property>
<property name="initialPoolSize">2</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">5</property>
<property name="minPoolSize">2</property>
<property name="maxStatements">50</property>
<property name="acquireIncrement">3</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">
<![CDATA[jdbc:mysql://127.0.0.1:3306/db909?useUnicode=true&characterEncoding=UTF-8]]>
</property>
<property name="user">root</property>
<property name="password">root</property>
</default-config>
</c3p0-config>
通过这个工具类就可以链接数据库了
|