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. argparse 模块使用

    import argparse,os data_func=["upload","download"]req_func=["getfunc", ...

  2. JAVA 爬虫框架webmagic 初步使用Demo

    一想到做爬虫大家第一个想到的语言一定是python,毕竟python比方便,而且最近也非常的火爆,但是python有一个全局锁的概念新能有瓶颈,所以用java还是比较牛逼的, webmagic 官网 ...

  3. 吴裕雄 PYTHON 人工智能——智能医疗系统后台智能分诊模块及系统健康养生公告简约版代码展示

    #coding:utf-8 import sys import cx_Oracle import numpy as np import pandas as pd import tensorflow a ...

  4. Python学习(三)——Python的运算符和数值、字符的类中方法

    Python开发IDE PyCharm,eclipse PyCharm的基础用法 全部选中后 Ctrl+?全部变为注释 运算符 结果为值的运算符 算术运算符: + - * / % // ** 赋值运算 ...

  5. 使用PIE.htc 进行IE兼容CSS3

    步骤: 1.引入文件.注意PIE.htc文件和css文件要放在同一个目录下: 2.在css元素中加上  behavior:url(pie.htc); 3.可以愉快的写css hack啦 ,这时需要的圆 ...

  6. zookeeper的安装(单机版)

    一.获取zookeeper的安装包 zookeeper的官网下载:wget  https://archive.apache.org/dist/zookeeper/zookeeper-3.4.10/zo ...

  7. input type=range 进度条的自定义样式

    /* 自定义进度条样式 */ .v_my input[type=range] { -webkit-appearance: none;/*清除系统默认样式*/ width: .8rem; backgro ...

  8. tp5的输入和验证

    规则和模板 好像要写一样名字,只需要引入模板

  9. P1893山峰瞭望

    传送门 看完这个题,大家都懂意思吧,然后代码呢,emmmmm #include <bits/stdc++.h> using namespace std; const int maxn = ...

  10. tf.app.run()的作用

    tf.app.run() 如果你的代码中的入口函数不叫main(),而是一个其他名字的函数,如test(),则你应该这样写入口tf.app.run(test) 如果你的代码中的入口函数叫main(), ...