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的更多相关文章

随机推荐

  1. ARM架构安装Anaconda3出现错误:cannot execute binary file: Exec format error

    ARM架构安装Anaconda3出现错误:cannot execute binary file: Exec format error 原因是:安装包格式不对. 在Anaconda官网上只有x86(32 ...

  2. idea选中文件时左侧菜单自动定位到文件所在位置

    一:设置成自动定位,如图: 二:点击项目工程目录上的阻击按钮,自动定位

  3. 利用单臂路由实现VLAN间的路由

    实验4:利用单臂路由实现VLAN间的路由. 实验原理:  实验内容: 本实验模拟公司网络场景,路由器R1是公司的出口网关,员工PC通过接入层交换机(如S2和S3)接入公司网络,接入层交换机又通过汇聚交 ...

  4. AtCoDeer and Election Report

    问题 G: AtCoDeer and Election Report 时间限制: 1 Sec  内存限制: 128 MB[提交] [状态] 题目描述 AtCoDeer the deer is seei ...

  5. 更改yii框架入口文件位置,修改前后端访问路径

    将frontend/web/index.php复制到项目根目录,修改为: <?php defined('YII_DEBUG') or define('YII_DEBUG', true); def ...

  6. js中的局部函数和全局函数的调用

    //局部函数和全局函数的特点 function fc1(){ var name ="chenhao"; function fc2(){ var age = 30; alert(na ...

  7. 2_2 3n+1问题

    猜想:对于任意大于1的自然数n,若n为奇数,则将n变为3n+1,否则变为n的一半.经过若干次这样的变换,一定会使n变为1.例如:3→10→5→16→8→4→2→1. 输入n,输出变换的次数.n< ...

  8. vb.net 实现多态

    1. 新建一个module,设置public 其他类才可以调用 Public Module Module2 Public Interface MyInterface Property stuName ...

  9. python opencv:像素操作

    图片的像素 像素:组成图片的单位 RGB:颜色由 RGB三种颜色组成 颜色深度:对于8bit的颜色深度来说,它可以表示的颜色范围是 0 ~ 255,对于RGB图片来说,8位颜色深度可以表示 (2^8) ...

  10. 【代码学习】PYTHON 函数

    一.定义函数 def 函数名(): 代码 二.函数调用 #定义函数 def printme(str): print str return #调用函数 printme("SQYY1" ...