package com.pk.xjgs.util;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;


public class JDBCTemplate {
    private QueryRunner queryRunner = new QueryRunner();

    /**
     * @param sql
     *            sql语句
     * @param params
     *            参数列表 按照sql中出现的顺序去写
     * @return
     * @function:处理增删改的sql语句的方法
     */
    public int update(String sql, Object[] params) {
        int i = 0;
        Connection conn = null;
        try {
            conn = DBConn.getConn();
            i = queryRunner.update(conn, sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBConn.closeConn(conn);
        }
        return i;
    }

    /**
     * @param sql
     * @param params
     * @return
     * @function:执行批处理的方法
     */
    public int[] bacth(String sql, Object[][] params) {
        int[] i = null;
        Connection conn = null;
        try {
            conn = DBConn.getConn();
            i = queryRunner.batch(conn, sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBConn.closeConn(conn);
        }
        return i;
    }

    /**
     * @param sql
     * @param params
     * @param requiredType
     *            返回的对象类
     * @return 返回对象
     * @function:返回对象的查询sql
     */
    @SuppressWarnings("unchecked")
    public Object queryForBean(String sql, Object[] params, Class requiredType) {
        Object object = null;
        Connection conn = null;
        try {
            conn = DBConn.getConn();
            object = queryRunner.query(conn, sql,
                    new BeanHandler(requiredType), params);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBConn.closeConn(conn);
        }
        return object;
    }

    /**
     * @param sql
     * @param params
     * @param requiredType
     * @return
     * @function:返回对象的集合
     */
    @SuppressWarnings("unchecked")
    public List queryForBeanList(String sql, Object[] params, Class requiredType) {
        Connection conn = null;
        List list = null;
        try {
            conn = DBConn.getConn();
            list = (List) queryRunner.query(conn, sql, new BeanListHandler(
                    requiredType), params);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBConn.closeConn(conn);
        }
        return list;
    }

    /**
     * @param sql
     * @param params
     * @param colName
     *            要查询的字段的名字
     * @return
     */
    public Object queryForObject(String sql, Object[] params) {
        Connection conn = null;
        Object object = null;
        try {
            conn = DBConn.getConn();
            object = queryRunner.query(conn, sql, new ScalarHandler(1),
                    params);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBConn.closeConn(conn);
        }
        return object;
    }

    /**
     * @param sql
     * @param params
     * @return
     * @function:查询组函数的方法  比如 select count(id) from userinfo
     */
    public int count(String sql, Object[] params) {

        Object o = queryForObject(sql, params);
        if (o instanceof Integer) {
            return (Integer) o;
        }
        if (o instanceof Long) {
            Long l = (Long) o;
            return l.intValue();
        }

        String s = (String) o;
        try {
            return Integer.parseInt(s);
        } catch (NumberFormatException e) {
            return 0;
        }
    }
}
View Code