G |
Displaying the Rock Survey Example Results |
|
Get |
Get |
Example: The Survey Results Application |
The RockSurvey Results application (see The Survey Results Application) displays the information garnered from the data collected and stored by the Rock Survey application developed during the course of the tutorial. It also reinforces what you've learned about working with beans, and shows how to use JDBC with session beans for read-only access.
RockResultsBean
is a stateless session bean that manages data access and information return to the client. Its home and component interfaces look rather innocuous and at first glance don't appear to offer much functionality:
// Remote Home interface for RockResultsBean
import javax.ejb.*;
import java.rmi.RemoteException;
public interface RockResultsRemoteHome extends EJBHome
{
// required
RockResultsRemote create()
throws RemoteException,
CreateException;
} // end RockResultsRemoteHome
// Remote component interface for RockResultsBean
import java.rmi.RemoteException;
import javax.ejb.*;
public interface RockResultsRemote extends EJBObject
{
public RockResultsData getData( int maxNames )
throws RemoteException;
} // end RockResultsRemote
However, the bean provides everything the client needs to interpret and display the information from the database tables "SurveyBeanTable" and SurveyNames.
The client code to obtain the bean's component interface should be familiar by now:
public RockResultsRemote createRRBean()
{
InitialContext ic;
Object oRef;
RockResultsRemote rr = null;
RockResultsRemoteHome rrHome = null;
String sMsg = "Couldn't create RockResultsBean.";
try
{
ic = new InitialContext();
oRef =
ic.lookup( "java:comp/env/ejb/RockResultsBean" );
rrHome =
(RockResultsRemoteHome)PortableRemoteObject.
narrow( oRef, RockResultsRemoteHome.class );
rr = rrHome.create();
}
catch( RemoteException re )
{
System.out.println( sMsg +
re.getMessage() );
}
catch( CreateException ce )
{
System.out.println( sMsg +
ce.getMessage() );
}
catch( NamingException ne )
{
System.out.println( "Unable to lookup home: " +
"RockResultsBean. " + ne.getMessage() );
}
return rr;
} // end createRRBean
The component interface only exposes the getData()
method, which takes an integer argument and returns a RockResultsData
object. The input argument is used both to size the name arrays and to set the fetch size for the prepared statement that accesses the SurveyNames table. RockResultsData
is a helper class, similar to the RockSurveyData
class used in the Rock Survey application. Once this object is obtained, no other remote calls to RockResultsBean
are necessary. The client uses RockResultsData
like this:
// load RockSurvey data from datastore, get a RockResultsData object
rrd = rr.getData( 3 ); // max three last names
...
// load data
asGM = rrd.loadGenderMarital();
asAW = rrd.loadAmountAndWeights();
asMFN = rrd.getLastNames();
asFav = rrd.loadFavorites( aiFav );
adFav = rrd.toPercentArray( aiFav );
asFrom = rrd.loadLocations( aiFrom );
adFrom = rrd.toPercentArray( aiFrom );
...
While RockResultsData
has a complete set of getters, the methods referenced above all return arrays and contain everything the RockSurvey Results client needs. The remainder of the JSP is mostly concerned with displaying the elements of the arrays.
Internally, RockResultsBean.getData()
obtains a JDBC DataSource
, then uses a Connection
and a PreparedStatement
to access the "SurveyBeanTable" and SurveyNames data. Once that is done, it creates and returns a RockResultsData
object to the client.
As you might imagine from the brief, but complete, description of RockResultsBean.getData()
, RockResultsData
is the real workhorse object. In its constructor, it loads all the fields for the getters, calculates necessary totals and a kilogram equivalent for the total rock weight, as well as the average weights. The other methods are somewhat involved, but basically set up the lines for client display.
![]() ![]() ![]() ![]() |