• Post Reply Bookmark Topic Watch Topic
  • New Topic

Transferring ResultSet from Bean to Servlet???

 
Sagar Sharma
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am accessing the database from the bean and getting a ResultSet object. My question is that how do i transfer this object to the servlet so that the search results could be displayed by the servlet onto an HTML page.
Thanks
Sagar
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a well-known issue. I recommend you do a search here for ResultSet and you'll get back lots of helpful stuff.
The main things you need to think about are how large your result set can be, and thus whether you need to split it into separate pages, and whether you are happy coding HTML into a servlet or whether you would prefer a templating or page-compiling solution.
If your problem is simply one of how to get data out of a ResultSet, you just need to use the "next()" method for each row, then getString(), getObject() etc. as appropriate for each column.
 
Ben Roy
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just ran into this problem recently. If you are using Jbuilder, they supply some classes which can help. In particular the way I handled this was to create a DataSetData object, is a disconnected type recordset which can be returned from a function. In the calling method I used a StorageResultSet which you can load the DataSetData into and then use like you would have the ResultSet. Something like:
DataSetData myData = myEJBRemote.getDataSetData();
StorageResultSet mySet;
myData.load(mySet);
I'm not sure load is the actual method but it should be easy to find out. Then in your ejb:
public DataSetData getDataSetData()
{
Database db = new Database();
QueryDataSet qds = new QueryDataSet();
DataSetData mySet;
try{
String queryText = "SELECT * FROM " + table;
db.setConnection(new ConnectionDescriptor(connection, user, " ", false, driver));
qds.setQuery(new QueryDescriptor(db, queryText, null, true, Load.ALL));
qds.open();
DataSetData data = DataSetData.extractDataSet(qds);
return data;
}
Even though this uses JBuilder specific classes, it can still be deployed to any environment. I'm sure there are other ways, but this is the simplest and quickest way I could find.
 
Rishi Kant
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way which I have been following is by using helper classes.
For example:
my method in the bean is
public helperObject getSomeData(param you pass){}
The helperObject would have get and set methods for the fields you are trying to retrieve.In the bean the set methods would be used to set the values of result set.The servlet would call the getSomeData() method and use the get methods to get those values.
This would work if you know the db structure and the fields you want to retrieve.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!