Search code examples
javaclassdynamicclass-designjavabeans

How can I create a class file dynamically?


I want to create a class file dynamically. Here it goes... With the given ResultSet, extracting the metadata I want to build a class file dynamically with getter and setter methods for all the columns that exist in ResultSet. Also I should be able to use this class file generated where ever I want in my later use. Can any body suggest me a better way to implement this. Also if any existing jar files available to implement this, that would be helpful.


Solution

  • Perhaps Apache Beanutils might suit your requirements?

    See the section on Dynabeans

    In particular:

    3.3 ResultSetDynaClass (Wraps ResultSet in DynaBeans)

    A very common use case for DynaBean APIs is to wrap other collections of "stuff" that do not normally present themselves as JavaBeans. One of the most common collections that would be nice to wrap is the java.sql.ResultSet that is returned when you ask a JDBC driver to perform a SQL SELECT statement. Commons BeanUtils offers a standard mechanism for making each row of the result set visible as a DynaBean, which you can utilize as shown in this example:

      Connection conn = ...;
      Statement stmt = conn.createStatement();
      ResultSet rs = stmt.executeQuery
        ("select account_id, name from customers");
      Iterator rows = (new ResultSetDynaClass(rs)).iterator();
      while (rows.hasNext()) {
        DynaBean row = (DynaBean) rows.next();
        System.out.println("Account number is " +
                           row.get("account_id") +
                           " and name is " + row.get("name"));
      }
      rs.close();
      stmt.close();
    

    3.4 RowSetDynaClass (Disconnected ResultSet as DynaBeans)

    Although ResultSetDynaClass is a very useful technique for representing the results of an SQL query as a series of DynaBeans, an important problem is that the underlying ResultSet must remain open throughout the period of time that the rows are being processed by your application. This hinders the ability to use ResultSetDynaClass as a means of communicating information from the model layer to the view layer in a model-view-controller architecture such as that provided by the Struts Framework, because there is no easy mechanism to assure that the result set is finally closed (and the underlying Connection returned to its connection pool, if you are using one).

    The RowSetDynaClass class represents a different approach to this problem. When you construct such an instance, the underlying data is copied into a set of in-memory DynaBeans that represent the result. The advantage of this technique, of course, is that you can immediately close the ResultSet (and the corresponding Statement), normally before you even process the actual data that was returned. The disadvantage, of course, is that you must pay the performance and memory costs of copying the result data, and the result data must fit entirely into available heap memory. For many environments (particularly in web applications), this tradeoff is usually quite beneficial.

    As an additional benefit, the RowSetDynaClass class is defined to implement java.io.Serializable, so that it (and the DynaBeans that correspond to each row of the result) can be conveniently serialized and deserialized (as long as the underlying column values are also Serializable). Thus, RowSetDynaClass represents a very convenient way to transmit the results of an SQL query to a remote Java-based client application (such as an applet).