You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Darrell <sk...@rogers.com> on 2003/06/09 19:29:52 UTC

Beans and database

hi,

This is my first posting to struts.  After much thought and reading I 
guess I nees a little inside help in populating beans from database 
queries.  I have reviewed Husted's use of creating ProcessBeans using 
Scaffold utilities which in return get their data from a collection of 
sql queries kept in a resource file.

I am using torque as an O/R tool and need to expose the data object 
properties to jsp.  I am looking for the best way to achieve this.
I was thinking of modifying the package
org.apache.commons.scaffold.sql.ResultSetUtils methods to create a map 
which would populate the beans so they would be strictly used as 
databeans to expose their properties to jsp using tags.  The following 2 
methods could be changed to get the result I wanted, but my question is 
what other ways could be used to get the same result?  Are there any 
other packages or utilities to create databeans?

  /**
       * Returns next record of result set as a Map.
       * The keys of the map are the column names,
       * as returned by the metadata.
       * The values are the columns as Objects.
       *
       * @param resultSet The ResultSet to process.
       * @exception SQLException if an error occurs.
       */
      public static Map getMap(ResultSet resultSet)
         throws SQLException {

             // Acquire resultSet MetaData
         ResultSetMetaData metaData = resultSet.getMetaData();
         int cols = metaData.getColumnCount();

             // Create hashmap, sized to number of columns
         HashMap row = new HashMap(cols,1);

             // Transfer record into hashmap
         if (resultSet.next()) {
             for (int i=1; i<=cols ; i++) {
                 row.put(metaData.getColumnName(i),
                     resultSet.getObject(i));
             }
         } // end while

         return ((Map) row);

      } // end getMap


/**
      * Populate the JavaBean properties of the specified bean, based on
      * the specified name/value pairs.  This method uses Java 
reflection APIs
      * to identify corresponding "property setter" method names. The 
type of
      * the value in the Map must match the setter type. The setter must
      * expect a single arguement (the one on the Map).
      * <p>
      * The particular setter method to be called for each property is
      * determined using the usual JavaBeans introspection mechanisms. Thus,
      * you may identify custom setter methods using a BeanInfo class 
that is
      * associated with the class of the bean itself. If no such BeanInfo
      * class is available, the standard method name conversion ("set" plus
      * the capitalized name of the property in question) is used.
      * <p>
      * <strong>NOTE</strong>:  It is contrary to the JavaBeans 
Specification
      * to have more than one setter method (with different argument
      * signatures) for the same property.
      * <p>
      * This method adopted from the Jakarta Commons BeanUtils.populate.
      *
      * @author Craig R. McClanahan
      * @author Ralph Schaer
      * @author Chris Audley
      * @author Rey François
      * @author Gregor Raýman
      * @author Ted Husted
      *
      * @param bean JavaBean whose properties are being populated
      * @param properties Map keyed by property name, with the
      *  corresponding value to be set
      *
      * @exception IllegalAccessException if the caller does not have
      *  access to the property accessor method
      * @exception InvocationTargetException if the property accessor method
      *  throws an exception
      * @deprecated Use BeanUtils.CopyProperties instead.
      */
     public static void setProperties(Object bean, Map properties)
         throws IllegalAccessException, InvocationTargetException {

         if ((bean == null) || (properties == null))
             return;

         /*
         if (debug >= 1)
             System.out.println("BeanUtils.populate(" + bean + ", " +
                                properties + ")");
         */

         // Loop through the property name/value pairs to be set
         Iterator names = properties.keySet().iterator();
         while (names.hasNext()) {

             // Identify the property name and value(s) to be assigned
             String name = (String) names.next();
             if (name == null)
                 continue;

             // Get the property descriptor of the requested property 
(if any)
             PropertyDescriptor descriptor = null;
             try {
                 descriptor = PropertyUtils.getPropertyDescriptor(bean, 
name);
             } catch (Throwable t) {
                 /*
                 if (debug >= 1)
                     System.out.println("    getPropertyDescriptor: " + t);
                 */
                 descriptor = null;
             }
             if (descriptor == null) {
                 /*
                 if (debug >= 1)
                     System.out.println("    No such property, skipping");
                 */
                 continue;
             }

             /*
             if (debug >= 1)
                 System.out.println("    Property descriptor is '" +
                                    descriptor + "'");
             */

             // Identify the relevant setter method (if there is one)
             Method setter = descriptor.getWriteMethod();
             if (setter == null) {
                 /*
                 if (debug >= 1)
                     System.out.println("    No setter method, skipping");
                 */
                 continue;
             }

             // Obtain value to be set
             Object[] args = new Object[1];
             args[0] = properties.get(name); // This MUST match setter type

             /*
             if (debug >= 1)
                 System.out.println("  name='" + name + "', value.class='" +
                                    (value == null ? "NONE" :
                                    value.getClass().getName()) + "'");
             */
             /*
             if (debug >= 1)
                 System.out.println("    Setting to " +
                                    (parameters[0] == null ? "NULL" :
                                     "'" + parameters[0] + "'"));
             */

             // Invoke the setter method
             setter.invoke(bean,args);
         }

         /*
         if (debug >= 1)
 
System.out.println("============================================");
         */

     } // end setProperties


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org