DBUtils结果集处理

1、BeanHandler查询

复制代码
 1 package jdbc;
 2 
 3 import java.sql.Connection;
 4 import java.sql.SQLException;
 5 
 6 import org.apache.commons.dbutils.DbUtils;
 7 import org.apache.commons.dbutils.QueryRunner;
 8 import org.apache.commons.dbutils.handlers.BeanHandler;
 9 
10 /**
11  * <p>
12  * Description:BeanHandler处理结果集演示
13  * </p>
14  * 
15  * @author Administrator
16  * @date 2018年11月5日下午5:04:55
17  */
18 public class BeanHandlerDemo {
19 
20     public static void main(String[] args) throws SQLException {
21         // 创建sql语句执行对象
22         QueryRunner qr = new QueryRunner();
23         // sql语句
24         String sql = "select * from sort";
25         Object[] params = {};
26         // 获得连接
27         Connection conn = JDBCUtils.getConnection();
28         // 执行sql语句
29         Sort sort = qr.query(conn, sql, new BeanHandler<Sort>(Sort.class), params);
30         // 打印结果集
31         System.out.println(sort);
32         // 关闭资源
33         DbUtils.closeQuietly(conn);
34     }
35 
36 }
复制代码

2、BeanListHandler查询

复制代码
 1 package jdbc;
 2 
 3 import java.sql.Connection;
 4 import java.sql.SQLException;
 5 import java.util.List;
 6 
 7 import org.apache.commons.dbutils.QueryRunner;
 8 import org.apache.commons.dbutils.handlers.BeanListHandler;
 9 
10 /**
11  * <p> Description:BeanListHandler类处理结果集演示</p>
12  * @author Administrator
13  * @date 2018年11月5日下午5:11:32
14  */
15 public class BeanListHandlerDemo {
16 
17     public static void main(String[] args) throws SQLException {
18         // 创建sql语句执行对象
19         QueryRunner qr = new QueryRunner();
20         // sql语句
21         String sql = "select * from sort";
22         Object[] params = {};
23         // 获得连接
24         Connection conn = JDBCUtils.getConnection();
25         // 执行sql语句
26         List<Sort> list = qr.query(conn, sql, new BeanListHandler<Sort>(Sort.class), params);
27         // 结果集处理
28         for (Sort s: list) {
29             System.out.println(s);
30         }
31     }
32 
33 }
复制代码

3、ColumeListHandler查询

复制代码
 1 package jdbc;
 2 
 3 import java.sql.Connection;
 4 import java.sql.SQLException;
 5 import java.util.List;
 6 
 7 import org.apache.commons.dbutils.QueryRunner;
 8 import org.apache.commons.dbutils.handlers.ColumnListHandler;
 9 
10 /**
11  * <p>Description:ColumeListHandler类查询演示 </p>
12  * @author Administrator
13  * @date 2018年11月5日下午5:16:17
14  */
15 public class ColumnListHandlerDemo {
16 
17     public static void main(String[] args) throws SQLException {
18         // 创建sql语句执行对象
19         QueryRunner qr = new QueryRunner();
20         // sql语句
21         String sql = "select * from sort";
22         Object[] params = {};
23         // 获得连接
24         Connection conn = JDBCUtils.getConnection();
25         // 执行sql语句
26         List<Object> list = qr.query(conn, sql, new ColumnListHandler<Object>(), params);
27         // 处理结果集
28         for (Object s: list) {
29             System.out.println(s);
30         }
31     }
32 
33 }
复制代码

4、ScalarHandler查询

复制代码
 1 package jdbc;
 2 
 3 import java.sql.Connection;
 4 import java.sql.SQLException;
 5 
 6 import org.apache.commons.dbutils.QueryRunner;
 7 import org.apache.commons.dbutils.handlers.ScalarHandler;
 8 /**
 9  * <p>Description:ScalarHandler类查询演示 </p>
10  * @author Administrator
11  * @date 2018年11月5日下午5:23:49
12  */
13 public class ScalarHandlerDemo {
14 
15     public static void main(String[] args) throws SQLException {
16         // 创建sql语句执行对象
17         QueryRunner qr = new QueryRunner();
18         // sql语句
19         String sql = "select count(*) from sort";
20         Object[] params = {};
21         // 获得连接
22         Connection conn = JDBCUtils.getConnection();
23         // 执行sql语句
24         Long l = qr.query(conn, sql, new ScalarHandler<Long>(), params);
25         // 处理结果集
26         System.out.println(l);
27     }
28 
29 }
复制代码

5、MapHandler查询

复制代码
 1 package jdbc;
 2 
 3 import java.sql.Connection;
 4 import java.sql.SQLException;
 5 import java.util.Map;
 6 
 7 import org.apache.commons.dbutils.QueryRunner;
 8 import org.apache.commons.dbutils.handlers.MapHandler;
 9 /**
10  * <p>Description:MapHandler处理结果集演示 </p>
11  * @author Administrator
12  * @date 2018年11月5日下午5:28:31
13  */
14 public class MapHandlerDemo {
15 
16     public static void main(String[] args) throws SQLException {
17         // 创建sql语句执行对象
18         QueryRunner qr = new QueryRunner();
19         // sql语句
20         String sql = "select * from sort";
21         // 获得连接
22         Connection conn = JDBCUtils.getConnection();
23         // 执行sql语句
24         Map<String, Object> map = qr.query(conn, sql, new MapHandler());
25         // 处理结果集
26         for (String key: map.keySet()) {
27             System.out.println(key + "..." + map.get(key));
28         }
29     }
30 
31 }
复制代码

6、MapListHandler查询

复制代码
 1 package jdbc;
 2 
 3 import java.sql.Connection;
 4 import java.sql.SQLException;
 5 import java.util.List;
 6 import java.util.Map;
 7 
 8 import org.apache.commons.dbutils.QueryRunner;
 9 import org.apache.commons.dbutils.handlers.MapListHandler;
10 /**
11  * <p>Description:MapListHandler处理结果集演示 </p>
12  * @author Administrator
13  * @date 2018年11月5日下午5:35:22
14  */
15 public class MapListHandlerDemo {
16 
17     public static void main(String[] args) throws SQLException {
18         // 创建sql语句执行对象
19         QueryRunner qr = new QueryRunner();
20         // sql语句
21         String sql = "select * from sort";
22         // 获得连接
23         Connection conn = JDBCUtils.getConnection();
24         // 执行sql语句
25         List<Map<String, Object>> list = qr.query(conn, sql, new MapListHandler());
26         // 处理结果集
27         for (Map<String, Object> map : list) {
28             for (String key : map.keySet()) {
29                 // 打印一条记录
30                 System.out.print(key + "..." + map.get(key));
31             }
32             // 换行
33             System.out.println();
34         }
35 
36     }
37 
38 }
复制代码

 

posted @   AlphaJunS  阅读(418)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示