ApacheDbUtilsTest
ApacheDbUtilsTest
package p1; import com.DataSourceUtil;
import entity.Student;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;
import javax.sql.DataSource;
import java.util.List;
import java.util.Map; public class ApacheDbUtilsTest {
public static void main(String[] args) throws Exception {
// ArrayHandler();
// ArrayListHandler();
// ArrayListHandlerStudent();
// ArrayListHandlerStudentList();
// ArrayListHandlerStudentMap();
// MapHandler();
// MapListHandler();
// KeyedHandler();
MapHandlerParams();
} public static void MapHandlerParams() throws Exception {
DataSource dataSource = DataSourceUtil.getDataSourceC3p0();
QueryRunner runner = new QueryRunner(dataSource);
String sql = "select * from student where id>? and name like ?";
Map<String, Object> students = runner.query(sql, new MapHandler(), 1, "%w%");
System.out.println(students);
} public static void KeyedHandler() throws Exception {
DataSource dataSource = DataSourceUtil.getDataSourceC3p0();
QueryRunner runner = new QueryRunner(dataSource);
String sql = "select * from student where id>?";
Map<String, Map<String, Object>> students = runner.query(sql, new KeyedHandler<>("name"), 1);
System.out.println(students);
} public static void MapListHandler() throws Exception {
DataSource dataSource = DataSourceUtil.getDataSourceC3p0();
QueryRunner runner = new QueryRunner(dataSource);
String sql = "select * from student where id>?";
List<Map<String, Object>> students = runner.query(sql, new MapListHandler(), 1);
System.out.println(students);
} public static void MapHandler() throws Exception {
DataSource dataSource = DataSourceUtil.getDataSourceC3p0();
QueryRunner runner = new QueryRunner(dataSource);
String sql = "select * from student where id>?";
Map<String, Object> students = runner.query(sql, new MapHandler(), 1);
System.out.println(students);
} public static void ArrayListHandlerStudentMap() throws Exception {
DataSource dataSource = DataSourceUtil.getDataSourceC3p0();
QueryRunner runner = new QueryRunner(dataSource);
String sql = "select * from student where id>?";
//此处坑 oracle默认数值类型为BigDecimal Integer更换为BigDecimal students.get(new BigDecimal(2))
Map<Integer, Student> students = runner.query(sql, new BeanMapHandler<Integer, Student>(Student.class, "id"), 1);
Student student = students.get(2);
System.out.println(student.getId() + " " + student.getName());
} public static void ArrayListHandlerStudentList() throws Exception {
DataSource dataSource = DataSourceUtil.getDataSourceC3p0();
QueryRunner runner = new QueryRunner(dataSource);
String sql = "select * from student where id>?";
List<Student> students = runner.query(sql, new BeanListHandler<>(Student.class), 1);
for (Student student : students) {
System.out.println(student.getId() + " " + student.getName());
}
} public static void ArrayListHandlerStudent() throws Exception {
DataSource dataSource = DataSourceUtil.getDataSourceC3p0();
QueryRunner runner = new QueryRunner(dataSource);
String sql = "select * from student where id>?";
Student student = runner.query(sql, new BeanHandler<>(Student.class), 1);
System.out.println(student.getId() + " " + student.getName());
} public static void ArrayListHandler() throws Exception {
DataSource dataSource = DataSourceUtil.getDataSourceC3p0();
QueryRunner runner = new QueryRunner(dataSource);
String sql = "select * from student where id>?";
List<Object[]> objects = runner.query(sql, new ArrayListHandler(), 1);
for (Object[] objs : objects) {
System.out.println(objs[0] + " " + objs[1]);
}
} public static void ArrayHandler() throws Exception {
DataSource dataSource = DataSourceUtil.getDataSourceC3p0();
QueryRunner runner = new QueryRunner(dataSource);
String sql = "select * from student where id>?";
Object[] objs = runner.query(sql, new ArrayHandler(), 1);
System.out.println(objs[0]);
System.out.println(objs[1]);
}
}
ApacheDbUtilsTest的更多相关文章
随机推荐
- PHP基础学习笔记1
一.基本语法 1.1 形式 PHP 脚本以 <?php 开始,以 ?> 结束: <?php //php代码 ?> 1.2 注释 单行注释 //这是单行注释 多行注释 /* 这是 ...
- blog主题——田野(1)
贮存一下,blog代码 QAQ 页首html <link rel='stylesheet' href='https://blog-static.cnblogs.com/files/elkyo/c ...
- MBA 报考
1. 作者:MBA薛老师链接:https://www.zhihu.com/question/277811289/answer/397083199来源:知乎著作权归作者所有.商业转载请联系作者获得授 ...
- Python--比较两个字典部分value是否相等(可以用于接口自动化)
eg:例如你调用了一个新增的接口,以往功能测试的话,你再web端新增一个店铺之后,你肯定要去数据库中查看,这些数据插入的对不对,是否正确的插入了每个字段 # 比较两个字典部分是否相等 def comp ...
- re模块、正则表达式
一.正则表达式 1.正则表达式不是Python独有的,它是一门独立的技术,所有的编程语言都可以使用正则表达式,在Python中使用正则表达式就要借助于re模块,或者是支持正则表达式书写的方法. 2.用 ...
- nginx介绍以及nginx的反向代理
什么是nginx? Nginx 是一个高性能的轻量级的HTTP和反向代理服务器,也是一个邮件服务器. 下载地址 本人使用的是Tengine,它是由淘宝网发起的Web服务器项目.它在Nginx的基础上, ...
- 计算机二级-C语言-对文件的读写操作。链表的定义与赋值。对字符串的遍历和处理。
//程序填空题:在此程序中,通过定义学生结构体变量,存储了学生的学号,姓名和三门课的成绩.所有学生数据均以二进制方式输出到文件中.函数fun()函数的功能是重写形参filename所指文件中最后一个学 ...
- 获取SqlServer存储过程定义的三种方法
declare @p_text varchar(max) SELECT @p_text= text FROM syscomments WHERE id = ( SELECT id FROM sysob ...
- Flask - Flask高级技巧(Advanced Flask Patterns)
传送门 来自作者的PPT https://speakerdeck.com/mitsuhiko/advanced-flask-patterns?slide=46
- Fluent_Python_Part4面向对象,09-pythonic-obj,Python风格的对象
第四部分第9章,Python风格的对象 这一章接第1章,说明常见的特殊方法实现. 本章包括以下话题: 支持用于生成对象其它表示形式的内置函数(如repr().bytes(),等等) 使用一个类方法实现 ...