日期:2014-05-16  浏览次数:20573 次

简单实现一种利用反射自动封装JavaBean的JDBC查询
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSetMetaData;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.sql.rowset.CachedRowSet;

public class BeanUtil
{

    private static Logger LOG = Logger.getLogger(Logger.class.getName());

    /**
     * 通过Bean对象获取查询语句
     * 
     * @param obj 对象
     * @param tableName 表名
     * @return SQL
     */
    public static String getSQLOfSelect(Object obj, String tableName)
    {
        StringBuffer strBuffer = new StringBuffer("SELECT ");
        Class<? extends Object> objClass = obj.getClass();
        Field fields[] = objClass.getDeclaredFields();
        for (Field field : fields)
        {
            String methodName = field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
            try
            {
                if (null != objClass.getMethod("get" + methodName))
                {
                    strBuffer.append(field.getName() + ",");
                }
            }
            catch (SecurityException e)
            {
                e.printStackTrace();
            }
            catch (NoSuchMethodException e)
            {
                e.printStackTrace();
            }
        }
        strBuffer.delete(strBuffer.length() - 1, strBuffer.length());
        strBuffer.append(" FROM " + tableName);
        LOG.info(strBuffer.toString());
        return null;
    }

    /**
     * 通过bean对象获取插入时的SQL
     * 
     * @param obj java对象
     * @param tableName 表名
     * @return SQL
     */
    public static String getSQLOfInsert(Object obj, String tableName)
    {
        String strSQL = new String("INSERT INTO " + tableName + "(#COLS) VALUES (#VALS)");
        Class<? extends Object> objClass = obj.getClass();
        Field fields[] = objClass.getDeclaredFields();
        StringBuffer cols = new StringBuffer("");
        StringBuffer values = new StringBuffer("");

        for (Field field : fields)
        {
            String methodName = "get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
            try
            {
                Method method = objClass.getMethod(methodName);
                Object o = method.invoke(obj);
                if (null != o)
                {
                    cols.append(field.getName() + ",");
                    if (o instanceof Integer || o instanceof Long || o instanceof Double)
                    {
                        values.append(o.toString() + ",");
                    }
                    else if (o instanceof String)
                    {
                        values.append("'" + o + "',");
                    }
                    else if (o instanceof java.util.Date)
                    {
                        String datestr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((java.util.Date) o);
                        values.append("'" + datestr + "',");
                    }
                    else
                    {
                        values.append("'" + o.toString() + "',");
                    }
                }
            }
            catch (SecurityException e)
            {
                e.printStackTrace();
                continue;
            }
            catch (NoSuchMethodException e)
            {
                e.printStackTrace();
                continue;
            }
            catch (IllegalArgumentException e)
            {
                e.printStackTrace();
            }
            catch (IllegalAccessException e)
            {
                e.printStackTrace();
            }
            catch (InvocationTargetException e)
            {
                e.printStackTrace();
            }