使用C3P0获取jdbc的连接(相关的总结)

1.首先的创建一个Dynamic Web Project

2.导包

3.在src包下创建c3p0-config.xml ,配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
	<default-config>
	<property name="driverClass">com.mysql.jdbc.Driver</property>
	<property name="jdbcUrl">jdbc:mysql://localhost:3306/account</property>
					//3306后面的是要连接的数据库的名
	<property name="user">root</property>
	<property name="password">123</property>
	<property name="initialPoolSize">10</property>
	<property name="maxIdleTime">30</property>
	<property name="maxPoolSize">100</property>
	<property name="minPoolSize">10</property>
	
	</default-config>
</c3p0-config>

4.编写数据源工作类C3P0Util,程序如下:

package cn.uplooking.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mysql.jdbc.Statement;

public class C3P0Util {
private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
	
	public static DataSource getDataSource() {
		return dataSource;
	}

	
	
	public static Connection getConnection(){
		
		try {
			return dataSource.getConnection();
		} catch (Exception e) {
			throw new RuntimeException("服务器连接错误");
		}         
		
	}
	public static void release(Connection conn,Statement stmt,ResultSet rs){
		//关闭资源
				if(rs!=null){
					try {
						rs.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
					rs = null;
				}
				if(stmt!=null){
					try {
						stmt.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
					stmt = null;
				}
				if(conn!=null){
					try {
						conn.close();//关闭
					} catch (Exception e) {
						e.printStackTrace();
					}
					conn = null;
				}
	}



	public static void closes(Connection conn, PreparedStatement ps, Object object) {
		
		
	}
	
 
}

5.创建Test测试类,进行增 删 改 查。

package cn.uplooking.testDBUtils;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.Test;

import cn.uplooking.pojo.Account;
import cn.uplooking.utils.C3P0Util;


public class test01 {
	//添加
	@Test
	public void test02() throws SQLException{
			//获取queryRunner对象
			QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
			//执行sql
			qr.update("insert into account (id,name,money) values(null,'你好','50')");
			//打印结果
	}
	//修改
	@Test
	public void test03() throws SQLException{
		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
		String  sql = "update account set name=?,money=? where id=?";
		qr.update(sql,"小米","9000",1);
	
	}
	//删除
	@Test
	public void test04() throws SQLException{
		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
		qr.update("delete from account where id=2");
	}
	//查询
	@Test
	public void test05() throws SQLException{
		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
		//这里用到BeanListHandler 意思是返回list集合
		List<Account> list = qr.query("select * from  account", new BeanListHandler<Account>(Account.class));
		for(Account a :list){
				System.out.println(a);
		}
	}
	
}

所对应的数据库如下:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值