Apache DBUtils

摘要:
“,newBeanHandler<Student>,1);System.out.println;}实现类BeanListHandler:从结果集中返回多行数据,并用object()接受它们//查询多行数据publicstaticvoidtestBeanListHandler()throws SQLException{QueryRunnerrunner=newQueryRunner;//自动提交事务列表<Student˃students=runner.Query(”从学生那里选择*˃?

Apache DBUtils

下载Commons-dbutils-1.7.jar,其中包含一下几个重点类:
Dbutils、QueryRunner、ResultSetHandler

1.DuUtils:辅助

2.QueryRunner:增删改查
update()
query()
oracle:dml,commit
mysql:dml自动提交

3.如果是查询,则需要ResultSetHandler接口,有很多实现类,一个实现类对应于一种不同的查询类型

--Object[]
实现类ArrayHandler:返回结果集的第一行数据,并用Object[]接收
//查询单行数据
public static void testArrayHandler() throws SQLException {
QueryRunner runner=new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());//自动提交事务
Object[] student = runner.query("select * from student where sno>?", new ArrayHandler(), 1);
System.out.println(student[0]+","+student[1]+","+student[2]+","+student[3]);
}
实现类ArrayListHandler:返回结果的多行数据,List<Object[]>
//查询多行数据
public static void testArrayListHandler() throws SQLException {
QueryRunner runner=new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());//自动提交事务
List<Object[]> students = runner.query("select * from student where sno>?", new ArrayListHandler(), 1);
for(Object[] student:students){
System.out.println(student[0]+","+student[1]+","+student[2]+","+student[3]);
}
}
--Student
实现类BeanHandler:返回结果集的第一行数据,用对象()接受
//查询单行数据(放入对象中)
public static void testBeanHandler() throws SQLException {
QueryRunner runner=new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());//自动提交事务
Student student = runner.query("select * from student where sno>?", new BeanHandler<Student>(Student.class), 1);
System.out.println(student.getSno()+","+student.getSname()+","+student.getSage()+","+student.getSaddress());
}
实现类BeanListHandler:返回结果集的多行数据,用对象()接受
//查询多行数据(放入对象中)
public static void testBeanListHandler() throws SQLException {
QueryRunner runner=new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());//自动提交事务
List<Student> students= runner.query("select * from student where sno>?", new BeanListHandler<Student>(Student.class), 1);
for(Student student:students){
System.out.println(student.getSno()+","+student.getSname()+","+student.getSage()+","+student.getSaddress());
}
}
实现类BeanMapHandler:返回结果集的多行数据,用对象()接受
//查询多行数据(map) ---jav中oracle默认的数值类型 BigDecimal
public static void testBeanMapHandler() throws SQLException {
QueryRunner runner=new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());//自动提交事务
Map<BigDecimal, Student> students = runner.query("select * from student where sno>?", new BeanMapHandler<BigDecimal,Student>(Student.class,"sno"), 1);
for(Map.Entry<BigDecimal, Student> student:students.entrySet()){
System.out.println(student.getKey()+":"+student.getValue());
}
Student student=students.get(new BigDecimal(3));//int
System.out.println(student.getSno()+","+student.getSname()+","+student.getSage()+","+student.getSaddress());
}

//反射会通过无参构造来创建对象
--Map

MapHandler:返回结果集的第一行数据 Map<String,Object>
{id=1,name=zs}
//查询单行数据 map
public static void testMapHandler() throws SQLException {
QueryRunner runner=new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());//自动提交事务
Map<String,Object> student = runner.query("select * from student where sno>?", new MapHandler(), 1);
System.out.println(student);
}
MapListHandler:返回结果集的多行数据 List<Map<String,Object>>
{id=2,name=ls},{id=3,name=ww}
//查询多行数据 map
public static void testMapListHandler() throws SQLException {
QueryRunner runner=new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());//自动提交事务
List<Map<String,Object>> students = runner.query("select * from student where sno>?", new MapListHandler(), 1);
System.out.println(students);
}

KeyedHandlder Map<String, Map<String, Object>>
{ls={id=2,name=ls},ww={id=3,name=ww}}
//查询多行数据 keyed
public static void testKeyedHandler() throws SQLException {
QueryRunner runner=new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());//自动提交事务
Map<String, Map<String, Object>> students = runner.query("select * from student where sno>?", new KeyedHandler<String>("sname"), 1);
System.out.println(students);
}
ColumnListHandler:把结果集的某一列保存到List中
2,ls
3,ww
//查询多行数据中的某一列 list
public static void testColumnListHandler() throws SQLException {
QueryRunner runner=new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());//自动提交事务
List<String> students = runner.query("select * from student where sno>?", new ColumnListHandler<String>("sname") ,1);
System.out.println(students);
}

-->
select count(1) from xxx;
select name from student where id =2;
ScalarHandler:单值结果
//查询单值数据
public static void testScalarHandler() throws SQLException {
QueryRunner runner=new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());//自动提交事务
BigDecimal result = runner.query("select count(1) from student where sno>?", new ScalarHandler<BigDecimal>(),1);
System.out.println(result);
String name= runner.query("select sname from student where sno=?", new ScalarHandler<String>(),1);
System.out.println(name);
}

Apache DBUtils第1张

Apache DBUtils第2张

免责声明:文章转载自《Apache DBUtils》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇jQuery插件--图片文字向上向左循环滚动Form中动态创建PopList下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

运维配置环境中间件

日常Linux运维环境配置笔记---不定期更新 声明:本文为个人维护笔记,当中的例子或者步骤都是借鉴网络上的方法或者方案,后自己一步步的进行试验后得出来的。有可能会写错,也有可能是缺少某部分没有记录,如发现请大家指出。谢谢 版权声明:本文为博主原创文章,未经博主允许不得转载。 1.环境建立 输入用户、密码登录Luinx 查看网络情况如何,有没有加载网...

Apache与Nginx优缺点比较

本文来源:收集、整理自互联网 1、nginx相对于apache的优点:轻量级,同样起web服务,比apache 占用更少的内存及资源抗并发,nginx处理请求是异步非阻塞的,而apache则是阻塞型的,在高并发下nginx能保持低资源低消耗高性能 高度模块化的设计,编写模块相对简单 社区活跃,各种高性能模块出品迅速 apache相对于nginx的优点...

org.apache.commons.httpclient工具类

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethod; import org.ap...

二、Apache MPM三种工作模式

一、Apache即现阶段比较流行的Web服务,是一个多模块化的Web服务,使用简单,速度快,稳定性好,可以做负载均衡及代理服务器来使用。 二、Apache有三种工作模式分别是 prefork、work、even Apache prefork模型 1、prefork介绍 prefork模型,有一个主控制进程,然后生成多个子进程,使用select模型,最...

httpclient4例子

参考:http://hc.apache.org/httpclient-3.x/tutorial.html importorg.apache.http.HttpEntity; importorg.apache.http.client.methods.CloseableHttpResponse; importorg.apache.http.client.me...

Redhat下 Apache, php, mysql的默认安装路径

Redhat下 Apache, php, mysql的默认安装路径 apache:如果采用RPM包安装,安装路径应在 /etc/httpd目录下apache配置文件:/etc/httpd/conf/httpd.confApache模块路径:/usr/sbin/apachectlweb目录:/var/www/html如果采用源代码安装,一般默认安装在/usr...