Steve Souza - 2003-12-03

On Tue, 2 Dec 2003 13:48:56 -0600 "EVERETT,
> Hello,
>
> Killer library!
>
> One quick question if you have the time: How do I convert a Java
> ResultSet?
>
> Ideally, I'd like to do this:
>
> ResultSet rs = <run some query>
> FormattedDataSet fds = FormattedDataSet.createInstance();
> String html = fds.getFormattedDataSet (rs, "htmlTable");
>
>
> Thanks,
> Mike
>
>

Thanks for the feeback.  I think it is a very powerful api and the more you learn the more powerful and extensible you realize it is.  How did you find out about it?

Out of the box the FormattedDataSet supports several types of TabularData, and can support any that you would add if you implement this interface. 

Out of the box the following will work.  Look in the javadocs as there are many examples.  As you can see you have lots of choices!  I usually use arrays as they are flexible (easily sorted, cached and more.  Look at ResultSetConverter javadocs for other advanatages.

1) ResultSets and ResultSetMetaData
// you would have to close the resultset and connection yourself
String html=fds.getFormattedDataSet(resultSet.getMetaData(), resultSet, "htmlTable");

2) arrays
String[] header={"fname", "lname"};
Object[][]body={{"steve","souza"},{"jeff","beck"}};
String html=fds.getFormattedDataSet(header, body, "htmlTable");

3) ResultSetConverter - A ResultSet Converted to a 2 dimensional array and a header converted from ResultSetMetaData
// I handle all jdbc opens, closes etc.  i.e. details are hidden from you
ResultSetConverter rsc=fds.getResultSetConverter("DataSource", "select * from table"):
String html=fds.getFormattedDataSet(rsc, "htmlTable");

// or the following which is just arrays.
String html=fds.getFormattedDataSet(rsc.getResultSetMetaData(), rsc.getResultSet(), "htmlTable");
// or you can convert the ResultSet to a ResultConverter using a contructor.  I think you take responsibility for closing jdbc resources. 
ResultSetConverter rsc=new ResultSetConverter(resultSet);
String html=fds.getFormattedDataSet(rsc, "htmlTable");

4) Passing a query directly and I open and close jdbc resources
String html=fds.getFormattedDataSet("select * from table", "htmlTable");

5) If you implement TabularData
String html=fds.getFormattedDataSet(myTabularDataHeader, myTabularDataBody, "htmlTable");

Steve