您的位置:首页 > 其它

(三)JDBC连接池&DBUtils—C3P0的使用

2017-08-17 18:24 274 查看

(三)JDBC连接池&DBUtils—C3P0的使用

C3P0开源免费的连接池!目前使用他的开源项目有:Spring、Hibernate等。使用第三方工具需要导入jar包,C3P0使用时还需要添加配置文件c3p0-config.xml

通过这个配置来加载驱动和获取连接

C3P0配置文件

需要从官网下载平常可以自己收藏一份来使用

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

<c3p0-config>

 

<default-config>

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

<property name="jdbcUrl">jdbc:mysql:///web08</property>

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

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

<property name="initialPoolSize">5</property>

<property name="maxPoolSize">20</property>

</default-config>

 

<named-config name="itheima">

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

<property name="jdbcUrl">jdbc:mysql:///web08</property>

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

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

</named-config>

</c3p0-config>

这是比较常用的配置

具体参数有:

 

常见配置项:

 

 

C3P0提供核心工具类:

ComboPooledDataSource,如果要使用连接池,必须创建该类的实例对象。

public class C3P0Utils {

private static ComboPooledDataSource dataSource=new ComboPooledDataSource("itheima");

public static DataSource getDataSource(){

return dataSource;

}

public static Connection getConnection(){

try {

return dataSource.getConnection();

} catch (SQLException e) {

// TODO Auto-generated catch block

throw new RuntimeException(e);

}

}

}

public class C3P0Utils {
priva
4000
te static ComboPooledDataSource dataSource=new ComboPooledDataSource("itheima");
public static DataSource getDataSource(){
return dataSource;
}
public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}

}


public class TestC3P0 {
@Test
public void testAddUser1() {
Connection conn = null;
PreparedStatement pstmt = null;

try {
// 2.从池子中获取连接
conn = C3P0Utils.getConnection();
String sql = "insert into tbl_user values(null,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "德玛");
pstmt.setString(2, "西亚");
int rows = pstmt.executeUpdate();
if (rows > 0) {
System.out.println("添加成功!");
} else {
System.out.println("添加失败!");
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JDBCUtils_V3.release(conn, pstmt, null);
}
}

@Test
public void testAddUser() {
Connection conn = null;
PreparedStatement pstmt = null;
// 1.创建C3P0连接池对象
//在ComboPooledDataSource()方法中参数部分是C3P0配置文件里面的 名字
//加载默认的配置
ComboPooledDataSource dataSource=new ComboPooledDataSource();

//加载有名称的配置
//ComboPooledDataSource dataSource1=new ComboPooledDataSource("itheima");

try {
// 2.从池子中获取连接
conn = dataSource.getConnection();
String sql = "insert into tbl_user values(null,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "赵信");
pstmt.setString(2, "德邦");
int rows = pstmt.executeUpdate();
if (rows > 0) {
System.out.println("添加成功!");
} else {
System.out.println("添加失败!");
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JDBCUtils_V3.release(conn, pstmt, null);
}
}
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐