February 2005
Intermediate to advanced
528 pages
12h 53m
English
You want to display data from a relational database, but you don't know the structure of the data.
Use the RowSetDynaClass class
(org.apache.commons.beanutils.RowSetDynaClass)
provided by the Jakarta Commons BeanUtils project.
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 ...