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. SQL常用语句和函数

    从一个表中选取数据插入到另一个表中: select column_name(s) into new_table_name from old_table_name --new_table_name表不必 ...

  2. Django框架之ORM的相关操作(一)

    一.一般操作 from django.db import models import datetime # Create your models here. class Author(models.M ...

  3. VS常用快捷键(2012)

    Ctrl+K,D ----格式化全部代码 Ctrl+K,F ----格式化选中的代码 Ctrl+K,C ----注释选定内容 Ctrl+K,U ----取消注释选定内容 Ctrl+J或者 Ctrl+S ...

  4. 17,a:img的alt和title有何异同? b:strong与en的异同?

    alt(alt text):为不能显示的图像,窗体或者applets的用户代理,alt属性用来指定替换文字.替换文字的语言用lang属性来指定. eg:下例中将图像作为链接来使用 <a href ...

  5. java 限制每隔15分钟才允许执行一次程序

    由于公司订餐平台,有个用户催单业务,每当用户点击催单按钮时,商家就会收到消息提示,如果用户频繁的发起催单请求,这样商家就会不停的收到消息提醒,所以想限制用户至少每隔15分钟才可以催单一次 我采取了以下 ...

  6. mysql 远程连接不上,bind-address参数配置要求,以及怎么去使得mysql能够允许远程的客户端访问

    刚安装了MySQL服务器,使用远程管理工具总是连接不上,因为知道mysql的默认端口是3306,于是使用telnet连接这个端口,(从这里可以学到telnet是可以这样用的) telnet 192.1 ...

  7. 【MYSQL建库和建表】MYSQL建库和建表

    1.Navicat创建Mysql数据库 2.创建创建用户表和索引 CREATE TABLE `t_user` ( `id` ) NOT NULL AUTO_INCREMENT COMMENT '主键' ...

  8. 《JavaScript高级程序设计》读书笔记(三)基本概念第六小节理解函数

    内容---语法---数据类型---流程控制语句 上一小节---理解函数 本小节 函数--使用function关键字声明,后跟一组参数以及函数体 function functionName(arg0, ...

  9. Try-Catch无法正确定位异常位置,我推荐2个有效技巧

    宇宙第一开发IDE Visual Studio的调试功能非常强大,平常工作debug帮助我们解决不少问题.今天分享两个异常捕获的技巧,希望能够帮助解决一些问题. 以下两种情况,我相信大家都会遇到过. ...

  10. WLC HA模式下的注意事项

    管理控制器:1.控制器默认开启的是SSH (CLI),Secure Web/https (GUI)2.登录控制器的管理地址为Active设备所控制(主备的配置同步,所以管理地址一致)3.WLC HA状 ...