File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes Populating Datatable from Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Populating Datatable from Database" Watch "Populating Datatable from Database" New topic

Populating Datatable from Database

Mike Boota
Ranch Hand

Joined: Jul 18, 2002
Posts: 82

I am still learning JSF and need some help. I have a Page with one datatable and one command button to create a new record that takes to a new page where a user creates a record and the out come routes back to the dataable page. Whenever a user comes to a page datatable is populated with values from the database. Here is what I have done in the backing bean constructor I am calling a method that gets the data from database and populates the list that is the source for the datatable. Now if there is no data in the database I only see headers. Now if an error occurs while connecting to the database I want to show an error on the same page and below the empty datatable with command button, how do I handle this.

Next when a user clicks on a command button I have an associated action listener in the backing bean but some how it always executes the constructor first and then the action listener method and if the database fails it never executes the action listener. How do i handle this.

Can someone please help me what is the best way to do this. As when a user comes to page wants the datatable automatically populated. Is doing this in backing bean constructor the only way. Any help is appreciated.


MB<br />Sun Certified Programmer for Java2 Platform
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

Just define a String property in your backing bean that returns a status/error message and set up an output text element in your JSF page to display it. At its simplest, you can just return an empty string if there's no error, or if you prefer you can add additional properties to control whether the output text item is displayed at all if empty, select a highlighting style based on error severity, etc.

Of course, for that to work properly, you have to intercept the database exception and translate it to a message string, since if the exception blows all the way back to the JSF framework you end up with the user seeing an ugly/useless stack trace page, instead.

In addition to catching exceptions, of course, you can set a message string to something like "No Data Available" if the datamodel has a row count of zero.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: Populating Datatable from Database
It's not a secret anymore!