码迷,mamicode.com
首页 > 数据库 > 详细

ApacheDbUtilsTest

时间:2019-11-03 01:34:10      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:for   int   run   import   port   col   get   默认   bean   

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();
    }

    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

标签:for   int   run   import   port   col   get   默认   bean   

原文地址:https://www.cnblogs.com/kikyoqiang/p/11784772.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!