实体类
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) {
}
}
}