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的更多相关文章
随机推荐
- C语言中二维数组如何申请动态分配内存
C语言中二维数组如何申请动态分配内存: 使用malloc函数,先分配第一维的大小,然后再循环分配每一维的大小 #include <stdio.h> #include <malloc. ...
- opencv:截取 ROI 区域
Rect roi; roi.x = 100; roi.y = 100; roi.width = 250; roi.height = 200; // 截取 ROI 区域 // 这种方式改变 sub,原图 ...
- PTA的Python练习题(十五)
第4章-12 求满足条件的斐波那契数 a=eval(input()) b=c=1 d=1 for i in range(a): c=b b=d d=b+c if d>a: print('{}'. ...
- GridView 列表组件 以及动态 GridView
1.通过 GridView.builder 实现网格布局 Widget getList(context, index) { return Container( margin: EdgeInse ...
- 【PAT甲级】1089 Insert or Merge (25 分)(插入排序和归并排序)
题意: 输入一个正整数N(<=100),接着输入两行N个整数,第一行表示初始序列,第二行表示经过一定程度的排序后的序列.输出这个序列是由插入排序或者归并排序得到的,并且下一行输出经过再一次排序操 ...
- chrome 2行换行省略号 ... text-ellipse
display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; overflow: hidden; 谷歌内部项目 ...
- 《记一次Linux被入侵全过程》阅读笔记
此前从未了解过关于网络安全相关方面的内容,仅仅知道安全性是软件必不可少的质量属性之一,而由于自己所做项目对安全性需求基本为无,所以很少对此进行关注.今天看到作者系统被入侵的经验,于是点开来读,以积累他 ...
- hackme.inndy.tw - pyyy
0x01 反编译 1.第一次尝试的时候我直接在线反编译,部分结果如下. for (i, f) in enumerate(F): n = pow(f, m, g) this_is = 'Y-Combin ...
- SQLite3约束介绍
SQLite 约束 约束是在表的数据列上强制执行的规则.这些是用来限制可以插入到表中的数据类型.这确保了数据库中数据的准确性和可靠性. 约束可以是列级或表级.列级约束仅适用于列,表级约束被应用到整个表 ...
- powerbuilder连接oracle数据库
一.打开已经安装好的pb9.0,主界面菜单栏有个两个圆柱形就行数据库连接,点击database. 二.选择oracle版本,由于数据库版本是9i,可以使用084 oracle8/8i.右键--选择ne ...