dbUtil复习

实体类

package cn.dbutils.test;

import java.util.Date;

public class Product {
	private int pid;
	private String pname;
	private String password;
	private String price;
	private Date pdate;
	private String category_id;
	public int getPid() {
		return pid;
	}
	public void setPid(int pid) {
		this.pid = pid;
	}
	public String getPname() {
		return pname;
	}
	public void setPname(String pname) {
		this.pname = pname;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getPrice() {
		return price;
	}
	public void setPrice(String price) {
		this.price = price;
	}
	public Date getPdate() {
		return pdate;
	}
	public void setPdate(Date pdate) {
		this.pdate = pdate;
	}
	public String getCategory_id() {
		return category_id;
	}
	public void setCategory_idd(String category_id) {
		this.category_id = category_id;
	}
	@Override
	public String toString() {
		return "Product [pid=" + pid + ", pname=" + pname + ", password=" + password + ", price=" + price + ", pdate="
				+ pdate + ", categoryId=" + category_id + "]";
	}
	
	
}

dbUtil测试

package cn.dbutils.test;

import java.util.List;

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

import cn.itdbutil.test.C3P0Utils;

public class TestDbutils {

	@Test
	public void add () {
		try {
			QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
			
			String sql = "insert into product values(?,?,?,?,?,?)";
			
			Object[] params = {6,"壮壮","1233454","999999999",null,2};
			
			queryRunner.update(sql,params);
			
		} catch(Exception e) {
			
		}
	}
	
	@Test
	public void find () {
		try {
			QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
			
			String sql = "Select * from product where category_id = ?";
			
			Object[] params = {1};
			
			List<Product> product  = queryRunner.query(sql, new BeanListHandler<Product>(Product.class),params );
			//BeanHandler一条结果,BeanListHander一个list结果集,scalarHandler件数
			for(Product p : product) {
				System.out.println(p);
			}
		}catch(Exception e) {
			
		}
	}
			
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值