Package cn.chen.core

Source Code of cn.chen.core.DBhelp

package cn.chen.core;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
/**
* ���ݿ�CURD��װ��ʹ��apache��Dbutil��
* Desc:
* Author:waitfox@qq.com
* Date:2013-5-4 ����7:02:43
*/
public class DBhelp {
  private static QueryRunner qr = null;
  private DBconn dBconn=new DBconn();
  public DBhelp(){
    qr=new QueryRunner(dBconn.getDataSource());
  }

  /**
   * ����,���ظ��¼�¼����
   *
   * @param conn
   * @param sql
   * @param params
   * @return int
   */
  public int update(String sql, Object[] params) {
    int n = 0;
    try {
      if (params == null) {
        n = qr.update(sql);
      } else {
        n = qr.update(sql, params);
      }
    } catch (Exception e) {
      System.err.println("���³����쳣!ԭ��:" + e);
    }
    return n;
  }

  /**
   * ��ѯ,��������ص�bean��,���beanͨ��List��װ����
   *
   * @param <T>
   * @param conn
   * @param entityClass
   * @param sql
   * @param params
   * @return List<T>
   */
  @SuppressWarnings({ "unchecked", "rawtypes" })
  public  <T> List<T> queryForList(Class<T> entityClass, String sql, Object[] params) {
    List<T> list = new ArrayList<T>();
    try {
      if (params == null) {
        list = (List<T>) qr.query(sql, new BeanListHandler(
            entityClass));
      } else {
        list = (List<T>) qr.query(sql, new BeanListHandler(
            entityClass), params);
      }
    } catch (Exception e) {
      System.err.println("��ѯ�����쳣!ԭ��:" + e);
    }
    return list;
  }

  /**
   * ͨ���������Ҽ�¼
   *
   * @param conn
   * @param sql
   * @param params
   * @return Map<String, Object>
   */
  public static Map<String, Object> getById(Connection conn,String sql,
      Object[] params) {
    Map<String, Object> map = null;
    try {
      if (params == null) {
        map = qr.query(conn, sql, new MapHandler());
      } else {
        map = qr.query(conn, sql, new MapHandler(), params);
      }
    } catch (Exception e) {
      System.err.println("��ѯ�����쳣!ԭ��:" + e);
    }
    return map;
  }
 
  /**
     * ��ѯijһ����¼������ָ���е�����ת��ΪObject
     * @param sql
     * @param columnName
     * @param params
     * @return �������
     */ 
    @SuppressWarnings({ "unchecked", "rawtypes" })
  public Object findBy(String sql, String columnName, Object[] params) {  
        Object object = null
        try
            if (params == null) { 
                object = qr.query(sql, new ScalarHandler(columnName))
            } else
                object = qr.query(sql, new ScalarHandler(columnName), params)
           
        } catch (SQLException e) { 
            System.err.println("findBy������" +e)
       
        return object; 
    }
   
    /**
     * ��ѯ��������еĵ�һ����¼������װ��Map����
     * @param sql
     * @param params
     * @return ��װΪMap�Ķ���
     */ 
    public Map<String, Object> findFirst(String sql, Object[] params) {  
        Map<String, Object> map = null
        try
            if (params == null) { 
                map = (Map<String, Object>) qr.query(sql, new MapHandler())
            } else
                map = (Map<String, Object>) qr.query(sql, new MapHandler(), params)
           
        } catch (SQLException e) {  
            System.err.println("findFirst.��ѯһ����¼����" + e)
       
        return map; 
    }
   
    public List<Map<String, Object>> find(String sql, Object[] params) { 
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>()
        try
            if (params == null) { 
                list = (List<Map<String, Object>>) qr.query(sql, new MapListHandler())
            } else
                list = (List<Map<String, Object>>) qr.query(sql, new MapListHandler(), params)
           
        } catch (SQLException e) {   
            System.err.println("map ���ݲ�ѯ����"+e)
       
        return list; 
   
}
TOP

Related Classes of cn.chen.core.DBhelp

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.