`
phantom
  • 浏览: 162681 次
社区版块
存档分类
最新评论

我自己的数据对象处理类 LoadRs2P

阅读更多

package com.jh.core.db.jdbc;

import java.lang.reflect.Field;

import com.jh.app.util.CommUtil;

class LoadRs2P {
 int columnType;

 String columnName;

 Field field;
/**
 * 自动匹配列值
 * @param rs
 * @param obj
 * @throws Exception
 */
 public void set(java.sql.ResultSet rs, Object obj) throws Exception {

  if (0 == (columnType)) {

  } else if (ColumnType.TYPE_INTEGER == columnType) {
   int iValue = rs.getInt(columnName);
   if (iValue == 0) {

   } else if (field.getType().equals(int.class)) {
    field.setInt(obj, iValue);
   } else if (field.getType().equals(long.class)) {
    field.setLong(obj, iValue);
   } else if (field.getType().equals(String.class)) {
    field.set(obj, ""+iValue);
   } else if (field.getType().equals(double.class)) {
    field.setDouble(obj, iValue);
   } else if (field.getType().equals(float.class)) {
    field.setFloat(obj, iValue);
   } else if (field.getType().equals(java.util.Date.class)) {
    field.set(obj, new java.util.Date(iValue));
   } else if (field.getType().equals(Long.class)) {
    field.set(obj, new Long(iValue));
   } else if (field.getType().equals(Integer.class)) {
    field.set(obj, new Integer(iValue));
   } else if (field.getType().equals(Double.class)) {
    field.set(obj, new Double(iValue));
   } else if (field.getType().equals(Float.class)) {
    field.set(obj, new Float(iValue));
   }

  } else if (ColumnType.TYPE_LONG == (columnType)) {
   long iValue = rs.getLong(columnName);
   if (iValue == 0) {

   } else if (field.getType().equals(long.class)) {
    field.setLong(obj, iValue);
   } else if (field.getType().equals(String.class)) {
    field.set(obj, ""+iValue);
   } else if (field.getType().equals(int.class)) {
    field.setInt(obj, (int) iValue);
   } else if (field.getType().equals(double.class)) {
    field.setDouble(obj, iValue);
   } else if (field.getType().equals(float.class)) {
    field.setFloat(obj, iValue);
   } else if (field.getType().equals(java.util.Date.class)) {
    field.set(obj, new java.util.Date(iValue));
   } else if (field.getType().equals(Long.class)) {
    field.set(obj, new Long(iValue));
   } else if (field.getType().equals(Integer.class)) {
    field.set(obj, new Integer((int) iValue));
   } else if (field.getType().equals(Double.class)) {
    field.set(obj, new Double(iValue));
   } else if (field.getType().equals(Float.class)) {
    field.set(obj, new Float(iValue));
   }

  } else if (ColumnType.TYPE_TIMESTAMP == (columnType)) {
   java.sql.Timestamp date = rs.getTimestamp(columnName);

   if (date != null) {
    if (field.getType().equals(java.util.Date.class)) {
     field.set(obj, new java.util.Date(date.getTime()));
    } else if (field.getType().equals(String.class)) {
     field.set(obj, "" + date);
    } else if (field.getType().equals(long.class)) {
     field.setLong(obj, date.getTime());
    } else if (field.getType().equals(Long.class)) {
     field.set(obj, new Long(date.getTime()));
    } else if (field.getType().equals(String.class)) {
     field.set(obj, "" + date);
    }
   }
  } else if (ColumnType.TYPE_STRING == (columnType)) {
   String objValue = rs.getString(columnName);
   if (objValue == null) {

   } else if (field.getType().equals(String.class)) {
    field.set(obj, objValue);
   } else if (field.getType().equals(long.class)) {
    field.setLong(obj, new Long(objValue));
   } else if (field.getType().equals(int.class)) {
    field.setInt(obj, new Integer(objValue));
   } else if (field.getType().equals(double.class)) {
    field.setDouble(obj, new Double(objValue));
   } else if (field.getType().equals(float.class)) {
    field.setFloat(obj, new Float(objValue));
   } else if (field.getType().equals(java.util.Date.class)) {
    field.set(obj, CommUtil.formatDate(objValue));
   } else if (field.getType().equals(Long.class)) {
    field.set(obj, new Long(objValue));
   } else if (field.getType().equals(Integer.class)) {
    field.set(obj, new Integer(objValue));
   } else if (field.getType().equals(Double.class)) {
    field.set(obj, new Double(objValue));
   } else if (field.getType().equals(Float.class)) {
    field.set(obj, new Float(objValue));
   }

  } else if (ColumnType.TYPE_DOUBLE == (columnType)) {
   double iValue = rs.getDouble(columnName);
   if (field.getType().equals(double.class)) {
    field.setDouble(obj, new Double(iValue));
   } else if (field.getType().equals(float.class)) {
    field.setFloat(obj, new Float((float) iValue));
   } else if (field.getType().equals(String.class)) {
    field.set(obj, ""+iValue);
   } else if (field.getType().equals(long.class)) {
    field.setLong(obj, new Long((long) iValue));
   } else if (field.getType().equals(int.class)) {
    field.setInt(obj, new Integer((int) iValue));
   } else if (field.getType().equals(Long.class)) {
    field.set(obj, new Long((long) iValue));
   } else if (field.getType().equals(Integer.class)) {
    field.set(obj, new Integer((int) iValue));
   } else if (field.getType().equals(Double.class)) {
    field.set(obj, new Double(iValue));
   } else if (field.getType().equals(Float.class)) {
    field.set(obj, new Float((float) iValue));
   }
  } else if (ColumnType.TYPE_BIGDECIMAL == (columnType)) {
   java.math.BigDecimal iValue = rs.getBigDecimal(columnName);
   if (iValue == null) {

   } else if (field.getType().equals(double.class)) {
    field.setDouble(obj, iValue.doubleValue());
   } else if (field.getType().equals(float.class)) {
    field.setFloat(obj, iValue.floatValue());
   } else if (field.getType().equals(String.class)) {
    field.set(obj, ""+iValue);
   } else if (field.getType().equals(long.class)) {
    field.setLong(obj, iValue.longValue());
   } else if (field.getType().equals(int.class)) {
    field.setInt(obj, iValue.intValue());
   } else if (field.getType().equals(Long.class)) {
    field.set(obj, new Long(iValue.longValue()));
   } else if (field.getType().equals(Integer.class)) {
    field.set(obj, new Integer(iValue.intValue()));
   } else if (field.getType().equals(Double.class)) {
    field.set(obj, new Double(iValue.doubleValue()));
   } else if (field.getType().equals(Float.class)) {
    field.set(obj, new Float(iValue.floatValue()));
   }
  }
 }

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics