java 学习笔记之 DBUtils 工具类
DBUtils 工具类
一. 介绍
DBUtils 是 Apache 组织开源的数据库工具类。
二. 使用步骤
①. 创建 QueryRunner 对象
②. 调用 update() 方法或者 query() 方法执行 sql 语句
三. 构造方法及静态方法
QueryRunner 类
1. 构造方法
①. 无参构造
QueryRunner qr =new QueryRunner();
使用无参构造的时候,调用 update 方法和 query 方法时就需要使用带 Connection 类型参数的重载形式
②. 有参构造
QueryRunner qr= new QueryRunner(DataSource dataSource);
这个参数是连接池对象
2. 静态方法
①.int update(Connection con ,String sql ,Param);
该方法用于增删改语句的操作
参数介绍:
参数一:连接池对象(这个在无参构造的时候使用)
参数二:sql 语句
参数三:可变参数(就是 sql 占位符的值)
返回值:int 类型的 返回受影响的行数
简单 update demopublicclassDemo{
publicstaticvoidmain(String[]args)throwsException{
/*
* 演示有参构造的update()方法
*
* 首先得导入jar包
* 配置好C3P0的配置文件与准备好C3P0工具类
* 然后创建QueryRunner对象
* 调用update方法
* 最后处理结果
*/
QueryRunnerqr=newQueryRunner(C3P0Utils.getDataSource());
intre=qr.update("update user set name=? where uid=?","张三",2);
if(re>0){
System.out.println("修改成功");
}else{
System.out.println("修改失败");
}
}
}
附上简单的 C3P0 工具类publicclassC3P0Utils{
privatestaticDataSourcedataSource=newComboPooledDataSource();
/**
* 获得DataSource实现类对象
* @return
*/
publicstaticDataSourcegetDataSource(){
returndataSource;
}
/**
* 获得连接
* @return
* @throws Exception
*/
publicstaticConnectiongetConnection()throwsException{
returndataSource.getConnection();
}
}
②.query(Connection con , String sql ,Param ...)
该方法用于出查询操作
参数介绍:
参数一:Connection 数据库连接对象, 使用带参构造时可以不用
参数二:sql 语句
参数三:表示对结果集的处理方式 (ResultSetHandler 接口)ArrayHandler: 表示将结果集第一行的数据存入数组
ArrayListHandler 将结果集每一行的数据存入数组,多个数组存入集合 List
BeanHandler 表示将结果集第一行的数据存入 Java Bean 对象
BeanListHandler 表示将结果集每一行的数据存入 Java Bean 对象 ,多个对象存入集合
ColumnListHandler 表示将某一列的数据存入集合MapHandler 表示将结果集第一行的数据存入 Map 集合 : 键:列名 值:列的值
MapListHandler 表示将结果集每一行的数据存入 Map 集合 多个 Map 存入 List 集合 List>
ScalarHandler 获取一个值: count(*) sum(price)
参数四:可变参数(就是 sql 占位符的值)
使用 BeanListHandler 处理方式的 demo:publicvoiddemo1()throwsException{
QueryRunnerqr=newQueryRunner(MyC3P0Utils.getDataSource());
Listlist=qr.query("select * from car where price<20 order by price desc",newBeanListHandler<>(Car.class));
for(Carcar:list){
System.out.println(car);
}
}
javaBean 类的编写:publicclassCar{
privateintcid;
privateStringcname;
privateStringcompany;
privateStringgrade;
privatedoubleprice;@OverridepublicStringtoString(){
return"Car [cid="+cid+", cname="+cname+", company="+company+", grade="+grade+", price="+price+"]";
}
publicintgetCid(){
returncid;
}
publicvoidsetCid(intcid){
this.cid=cid;
}
publicStringgetCname(){
returncname;
}
publicvoidsetCname(Stringcname){
this.cname=cname;
}
publicStringgetCompany(){
returncompany;
}
publicvoidsetCompany(Stringcompany){
this.company=company;
}
publicStringgetGrade(){
returngrade;
}
publicvoidsetGrade(Stringgrade){
this.grade=grade;
}
publicdoublegetPrice(){
returnprice;
}
publicvoidsetPrice(doubleprice){
this.price=price;
}
publicCar(intcid,Stringcname,Stringcompany,Stringgrade,doubleprice){
super();
this.cid=cid;
this.cname=cname;
this.company=company;
this.grade=grade;
this.price=price;
}
publicCar(){
super();
// TODO Auto-generated constructor stub
}
}
来源: http://www.bubuko.com/infodetail-2451462.html