This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes JSF and the fly likes Showing results of dynamic database query in jsf Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » JSF
Reply Bookmark "Showing results of dynamic database query in jsf" Watch "Showing results of dynamic database query in jsf" New topic
Author

Showing results of dynamic database query in jsf

ajaay shereen
Greenhorn

Joined: Feb 09, 2010
Posts: 3

Hi,
Sorry if i am posting for a previously answered question or a basic one. I am a newbie to jsf.
I want to process a query passed at runtime through webpage and display the results in another page. i have tried passing resultset directly to the datatable tag and it didnt give me desired reults.
somewhere i saw that said to change resultset to result. that didnt work for all the query results as well.
please help me on this. please tell which is the right way to show database results in jsf

Thanks in advance.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 14486
    
    7

Welcome to the JavaRanch, ajaay!

You cannot pass a ResultSet directly to a dataTable. DataTables display what they get from a DataModel object, so you need to construct a ResultsetDataModel and supply your resultset to it. You construct a dataTable tag to reference that DataModel object.


Customer surveys are for companies who didn't pay proper attention to begin with.
Nick Potter
Ranch Hand

Joined: May 07, 2008
Posts: 125
Hello, I'm curious, why does dataTable needs a DataModel? It works with Lists for example, so as long as you assign the entries to a list there shouldn't be any problem. Or am I misunderstanding the question?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 14486
    
    7

The DataModel provides the cursor services that the controller uses to iterate the rows in the table. Among other things, it allows you to bind actions to items in the table and know which item was selected.

You can't supply a List directly to a DataTable. There's a ListDataModel class that can be used for wrapping Lists, however.
Nick Potter
Ranch Hand

Joined: May 07, 2008
Posts: 125
For binding purposes and additional functionality, yes, but for simply displaying the results of a query, a list is enough.



This works and accomplishes its goal.
Bart Kummel
author
Ranch Hand

Joined: Nov 30, 2007
Posts: 81
Hi Nick,

You are right. However, in the example in your last post, the dataTable component still wraps the list in a javax.faces.model.ListDataModel internally. Most of the time you do not notice this, but it is good to know that this is happening behind the scenes.

Best regards,
Bart


SCJP 1.4 | SCJD 1.6 | Visit my website | Author of the book Apache MyFaces 1.2 Web Application Development
Nick Potter
Ranch Hand

Joined: May 07, 2008
Posts: 125
Aha! ... good to know then.

Thanks,
ajaay shereen
Greenhorn

Joined: Feb 09, 2010
Posts: 3

Thank you guys for clarifying my doubt.
@Tim: I will try what you have said about datamodel and post the result tomorrow.
Bart,
From what you have said about list converting into listdatamodel internally, i take it that all the Collection types change to corresponding DataModels when using in JSF. Please tell whether it is so.
Thanks once again.
Bart Kummel
author
Ranch Hand

Joined: Nov 30, 2007
Posts: 81
Hi Ajaay,

No, not all Collection types are wrapped in a DataModel automatically. According to the JSF 1.2 specification, only java.util.List or array of Objects are autmatically wrapped. I just read that part of the spec, and it actually says:

The current value identified by the value property is normally of type DataModel.
However, a DataModel wrapper instance must automatically be provided by the JSF
implementation if the current value is of one of the following types:
java.util.List
■ Array of java.util.Object
java.sql.ResultSet (which therefore also supports javax.sql.RowSet)
javax.servlet.jsp.jstl.sql.Result
■ Any other Java object is wrapped by a DataModel instance with a single row.

So it should be possible to use a ResultSet, as you tried. However, I never tried this and I think it isn't common use to do so.

Best regards,
Bart
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Showing results of dynamic database query in jsf
 
Similar Threads
SQL ResultSet count Problem
problem in Returning ResultSet in a EJB
How to re-sort the HTML table without requery the database?
Values not retrieved from the DB
Result Set display in JSP