On jeu., 2009-09-10 at 04:41 -0700, satheeshsamiappan wrote:
> Hi,
> Im working on a POC in velocity.
> Want to know whether using velocity can we populate a template based on
> records in a JDBC ResultSet instance?If so kindly provide the link that has
> document or example.
> Typically a resultset will be put in the VelocityContext. In vm file we have
> to write the code in generic way to display the columns, data etc.
> Its urgent for me since i need to complete this poc in 2 days.

What you need is a small java wrapper around your ResultSet to easy
things, so that you can write things like:

<table>
  ## headers
  <tr>
    #foreach($col in $result.colNames)
      <th>$col</th>
    #end
  </tr>
  ## data
  <tr>
    #foreach($row in $result.rows)
      <tr>
        #foreach($col in $result.colNames)
          <td>$result.get($col)</td>
        #end
      </tr>
    #end
  </tr>
</table>

To write your wrapper object, you can check some source files of the
Velosurf project:

For the getColNames() method, check 
http://velosurf.svn.sourceforge.net/viewvc/velosurf/trunk/src/java/velosurf/sql/SqlUtil.java?revision=488&view=markup

-->

/**
  157      * get the column nams of a result set
  158      * @param resultSet result set
  159      * @return list of columns
  160      * @throws SQLException
  161      */
  162     public static List<String> getColumnNames(ResultSet resultSet) throws 
SQLException {
  163         List<String> columnNames = new ArrayList<String>();
  164         ResultSetMetaData meta = resultSet.getMetaData();
  165         int count = meta.getColumnCount();
  166         for (int c=1;c<=count;c++) {
  167             // see http://jira.springframework.org/browse/SPR-3541
  168             //columnNames.add(meta.getColumnName(c));
  169             columnNames.add(meta.getColumnLabel(c));
  170 }
  171         return columnNames;
  172     }

And for the col getters and iteration, you can check the RowIterator class:

http://velosurf.svn.sourceforge.net/viewvc/velosurf/trunk/src/java/velosurf/context/RowIterator.java?revision=487&view=markup

Hope that helps,

  Claude



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to