C3P0简单使用
来源:互联网 发布:通达信编程300例教程 编辑:程序博客网 时间:2024/05/16 10:39
C3P0是一个开源的JDBC连接池,现使用第二种配置方式 via an XML configuration file,
1、配置c3p0-config.xml:下载jar包,打开c3p0-0.9.1.2\doc下的index.html
QUICKSTART中可以看到
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass( "org.postgresql.Driver" );
cpds.setJdbcUrl( "jdbc:postgresql://localhost/testdb" );
cpds.setUser("dbuser");
cpds.setPassword("dbpassword");
点击查看XML configuration file,并将上面set的内容修改并放到下面中,即可用配置文件为:
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/day04_09_jdbc</property>
<property name="user">root</property>
<property name="password">root123</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
</default-config>
2、创建C3P0Util类,从连接池得到连接方法getConnection()和重新放入连接池方法release()
public class C3P0Util {
private static ComboPooledDataSource datasource = new ComboPooledDataSource();
public static Connection getConnection(){
try {
return datasource.getConnection();
} catch (SQLException e) {
throw new ExceptionInInitializerError("服务器错误!");
}
}
public static void release(ResultSet rs, PreparedStatement ps, Connection conn){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs=null;
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
ps=null;
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn=null;
}
}
}
3、创建测试类
public class TestCRUD {
@Test
public void testselect(){
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
try {
conn = C3P0Util.getConnection();
pst = conn.prepareStatement("select * from users where id=?");
pst.setInt(1, 1);
rs = pst.executeQuery();
List<users> list = new ArrayList<users>();
while(rs.next()){
users u = new users();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
u.setPassword(rs.getString("password"));
u.setEmail(rs.getString("email"));
u.setBirthday(rs.getDate("birthday"));
list.add(u);
}
for (users users : list) {
System.out.println(users);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
C3P0Util.release(rs, pst, conn);
}
}
}
- c3p0简单使用
- C3P0简单使用
- JDBC和c3p0的简单使用
- c3p0数据库连接池的简单使用
- c3p0使用
- c3p0使用
- c3p0使用
- JDBC之连接池DBCP与c3p0的简单使用
- JNDI、DBCP、C3P0三种数据源的简单使用
- [c3p0] 第二篇:使用c3p0
- MySql+c3p0简单例子
- c3p0(简单通用式)
- C3P0简单配置
- c3p0简单实现方式
- c3p0的简单说明
- [c3p0] 第一篇:简单认识c3p0
- C3P0使用的笔记
- c3p0的使用
- 读<天才在左疯子在右>
- Verilog HDL作业1_2
- The Linux Command Line读书笔记(一)
- 关于线上bug
- shell中通配符的简单应用
- C3P0简单使用
- 对象的序列化----将对象转化成二进制数据流(读书摘录)
- 关于OpenStack构架说明
- Python>pip安装模块解决
- 山东省第五届ACM省赛 Colorful Cupcakes(记忆化搜索)
- 设备树学习之(三)Clock
- 迟到的前端,不迟的博文
- c++作业5
- map的嵌套