参考下面的代码吧:
    /**
    *  从给定的 ResultSet 里按指定的 properties 读出数据填写到给定的 object 中。<br>
    *  根据 obj 中 property 的数据类型决定读取数据时所使用的类型。<br>
    *  现在支持的数据类型包括 int、long、boolean、float、double、String、sql.Timestamp、sql.Date、sql.Time、util.Date<br>
    *  NOTE: 要求 object 中 property 的名字与结果集中的字段名一致。<br>
    *  HINT: 结果集中的字段名是 case-insensitive 的,而 bean object 是 case-sensitive 的<br>
    */
    protected void readObjectFromResultSet(ResultSet rs, Object obj, String[] props) throws SQLException {
        for (int i=0; i<props.length; i++) {
            Object value = null;
            Class theClass = null;
            try {
                theClass = PropertyUtils.getPropertyType(obj, props[i]);                if (theClass.getName().equals("int")) {
                    value = new Integer(rs.getInt(props[i]));                } else if (theClass.getName().equals("long")) {
                    value = new Long(rs.getLong(props[i]));                } else if (theClass.getName().equals("boolean")) {
                    value = new Boolean(rs.getBoolean(props[i]));                } else if (theClass.getName().equals("float")) {
                    value = new Float(rs.getFloat(props[i]));                } else if (theClass.getName().equals("double")) {
                    value = new Double(rs.getDouble(props[i]));                } else if (Class.forName("java.lang.String").isAssignableFrom(theClass)) {
                    value = rs.getString(props[i]);                } else if (Class.forName("java.sql.Timestamp").isAssignableFrom(theClass)) {
                    value = rs.getTimestamp(props[i]);                } else if (Class.forName("java.sql.Date").isAssignableFrom(theClass)) {
                    value = rs.getDate(props[i]);                } else if (Class.forName("java.sql.Time").isAssignableFrom(theClass)) {
                    value = rs.getTime(props[i]);                } else if (Class.forName("java.util.Date").isAssignableFrom(theClass)) {
                    Timestamp t = rs.getTimestamp(props[i]);
                    if (t==null)
                        value = null;
                    else
                        value = new java.util.Date(t.getTime());                } else {
                    throw new Exception("un-supported class : " + theClass.getName());
                }
            } catch (Exception e) {
                String msg = getClass().getName() + "::BaseDAO::readObjectFromResultSet - can not read property named [" + props[i] + "] - " + e.getMessage();
                Debug.error(msg);
                throw new SQLException(msg);
            }            try {
                if (value!=null) PropertyUtils.setProperty(obj, props[i], value);
            } catch (Exception e) {
                String msg = getClass().getName() + "::BaseDAO::readObjectFromResultSet - no property named [" + props[i] + "] in obj can be write";
                Debug.error(msg);
                throw new SQLException(msg);
            }

解决方案 »

  1.   

    非常感谢advanced(超越),我调试一下看看,按这个办法应该可以的。
    回头好好谢你。
      

  2.   

    advanced(超越):
        你能否再详细地给我说明一下,如何从给定的 ResultSet 里按指定的 properties 读出数据填写到给定的 object 中,是指 obj = rs.getObject(i)吗?PropertyUtils.getPropertyType(obj, props[i]) 和setProperty(obj, props[i])这两个类你能否给我一份参考一下, 尤其是 setProperty(obj, props[i])该如何写?
        望能再给指导!!!    方便的话发邮件给我,邮箱: [email protected].
        万分感谢!!!