commons-dbutils的研究和开发应用(三)

 /**

     * 将一个ResultSet转换为一个List对象

     */

    public List toBeanList(ResultSet rs, Class type) throws SQLException {

        List results = new ArrayList();

         if (!rs.next()) {

            return results;

        } 

        PropertyDescriptor[] props = this.propertyDescriptors(type);

        ResultSetMetaData rsmd = rs.getMetaData();

        int[] columnToProperty = this.mapColumnsToProperties(rsmd, props);

         do {

            results.add(this.createBean(rs, type, props, columnToProperty));

        } while (rs.next());

         return results;

    }   

    private Object createBean(ResultSet rs, Class type,

            PropertyDescriptor[] props, int[] columnToProperty)

            throws SQLException {

         Object bean = this.newInstance(type);

         for (int i = 1; i < columnToProperty.length; i++) {

             if (columnToProperty[i] == PROPERTY_NOT_FOUND) {

                continue;

            }

                       PropertyDescriptor prop = props[columnToProperty[i]];

            Class propType = prop.getPropertyType();

             Object value = this.processColumn(rs, i, propType);

             if (propType != null && value == null && propType.isPrimitive()) {

                value = primitiveDefaults.get(propType);

            }

             this.callSetter(bean, prop, value);

        }

         return bean;

    }

    /**

     *设置对象的setter方法的应用

     */

    private void callSetter(Object target, PropertyDescriptor prop, Object value)

            throws SQLException {

 

        Method setter = prop.getWriteMethod();

        if (setter == null) {

            return;

        }

        Class[] params = setter.getParameterTypes();

        try {

            // convert types for some popular ones

            if (value != null) {

                if (value instanceof java.util.Date) {

                    if (params[0].getName().equals("java.sql.Date")) {

                        value = new java.sql.Date(((java.util.Date) value).getTime());

                    } else

                    if (params[0].getName().equals("java.sql.Time")) {

                        value = new java.sql.Time(((java.util.Date) value).getTime());

                    } else

                    if (params[0].getName().equals("java.sql.Timestamp")) {

                        value = new java.sql.Timestamp(((java.util.Date) value).getTime());

                    }

                }

            }

            // Don't call setter if the value object isn't the right type

            if (this.isCompatibleType(value, params[0])) {

                setter.invoke(target, new Object[] { value });

            } else {

              throw new SQLException(

                  "Cannot set " + prop.getName() + ": incompatible types.");

            }

        } catch (IllegalArgumentException e) {

            throw new SQLException(

               "Cannot set " + prop.getName() + ": " + e.getMessage());

        } catch (IllegalAccessException e) {

            throw new SQLException(

                "Cannot set " + prop.getName() + ": " + e.getMessage());

        } catch (InvocationTargetException e) {

            throw new SQLException(

                "Cannot set " + prop.getName() + ": " + e.getMessage());

        }

    }

你可能感兴趣的:(sql,bean)