O'Reilly logo

Jakarta Struts Cookbook by Bill Siggelkow

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

10.2. Displaying Relational Data

Problem

You want to display data from a relational database, but you don't know the structure of the data.

Solution

Use the RowSetDynaClass class (org.apache.commons.beanutils.RowSetDynaClass) provided by the Jakarta Commons BeanUtils project.

Tip

The JAR files for BeanUtils are included with the Struts distribution so no additional download is needed.

Start by creating a data access object, like the one shown in Example 10-1, which performs the database query and returns a BeanUtils RowSetDynaClass created from a JDBC result set.

Example 10-1. RowSetDynaClass-based data access object

package com.oreilly.strutsckbk.ch05;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;

import org.apache.commons.beanutils.RowSetDynaClass;

public class UserDao {

    public RowSetDynaClass getUsersRowSet( ) throws Exception {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        RowSetDynaClass rowSet = null;
        try {
            conn = getConnection( );
            stmt = conn.createStatement( );
            rs = stmt.executeQuery("select * from users");
            rowSet = new RowSetDynaClass(rs);            
        }
        finally {
            if (conn != null) conn.close( );
        }
        return rowSet;
    }

    private Connection getConnection( ) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        return DriverManager.getConnection("jdbc:mysql://localhost/test");
    }

}

Create an Action that retrieves the RowSetDynaClass from the data access object and stores it in the servlet request. The Action ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required