DBUtils模版CRUD

准备:导包

1.创建c3p0-config.xml配置文件放在src下

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

	<!-- default-config 默认的配置,  -->
  <default-config>
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost/mydb</property>
    <property name="user">root</property>
    <property name="password">root</property>
    
    
    <property name="initialPoolSize">10</property>
    <property name="maxIdleTime">30</property>
    <property name="maxPoolSize">100</property>
    <property name="minPoolSize">10</property>
    <property name="maxStatements">200</property>
  </default-config>
  
   <!-- This app is massive! -->
  <named-config name="oracle"> 
    <property name="acquireIncrement">50</property>
    <property name="initialPoolSize">100</property>
    <property name="minPoolSize">50</property>
    <property name="maxPoolSize">1000</property>

    <!-- intergalactoApp adopts a different approach to configuring statement caching -->
    <property name="maxStatements">0</property> 
    <property name="maxStatementsPerConnection">5</property>

    <!-- he's important, but there's only one of him -->
    <user-overrides user="master-of-the-universe"> 
      <property name="acquireIncrement">1</property>
      <property name="initialPoolSize">1</property>
      <property name="minPoolSize">1</property>
      <property name="maxPoolSize">5</property>
      <property name="maxStatementsPerConnection">50</property>
    </user-overrides>
  </named-config>

 
</c3p0-config>
	

2.创建一个bean

package rick;

public class UserStus {
	private String name;
	private int money;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getMoney() {
		return money;
	}
	public void setMoney(int money) {
		this.money = money;
	}
	@Override
	public String toString() {
		return "UserStus [name=" + name + ", money=" + money + "]";
	}
	

}

3.测试DBUtils

package rick;

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

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

import com.mchange.v2.c3p0.ComboPooledDataSource;


/*
//针对增加  、 删除 、 修改
queryRunner.update(sql)

//针对查询
queryRunner.query(sql, rsh);*/
public class DBUtilsTest {
	@Test
	public void testdemo() throws SQLException {

        //建立连接(C3P0数据库连接池的方式)
		ComboPooledDataSource dataSource = new ComboPooledDataSource();

        //创建DBUtils
		QueryRunner queryRunner = new QueryRunner(dataSource);
		// 添加
		String name = "qirui1";
		int money = 10000;
		queryRunner.update("insert into stus values (?,?)", name, money);

		// 删除
		queryRunner.update("delete from stus where name = ?", "admin");

		// 更新
		queryRunner.update("update stus set money = ? where name = ?", 10000000, name);

		// 查询单个对象

		UserStus userstu = queryRunner.query("select * from stus where name = ?",new BeanHandler<UserStus>(UserStus.class), "lisi");
		System.out.println(userstu.toString());

		//查询所有对象
		List<UserStus> list = queryRunner.query("select * from stus ", new BeanListHandler<UserStus>(UserStus.class));
		for (UserStus userstus : list) {
			System.out.println(userstus.toString());
		}

	}
}

总体结构: 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值