O'Reilly logo

JavaServer Faces by Hans Bergsten

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A Word About JSF and Databases

I have already mentioned that the <h:dataTable> action supports values of type java.sql.ResultSet and javax.servlet.jsp.jstl.sql.Result, i.e., the JDBC and JSTL database query result data types. This means you can use the JSTL <sql:query> action to execute an SQL query directly in a JSP page or use JDBC in an action method and expose the ResultSet as a request, session or application scope variable and display the result with <h:dataTable>.

For a very simple application, either of those approaches typically works fine. But when you use JSF, you already have to do quite a bit of Java programming so I recommend that you go the extra mile and create bean classes to represent the database data instead. The ReportEntry, Report, and ReportRegistry classes used in the sample application illustrate one way to represent tabular data as beans. Using beans makes the application much more maintainable than direct database access from JSP or application logic code—if you need to change the database schema to improve performance, or change the SQL statements because you’re moving to a different database vendor, the main application code remains the same and only the database abstraction classes need to be modified (in the sample application, all you need to do in a case like this is replace the ReportRegistry implementation).

That’s all I’m going to say about JSF and databases, because that’s all there is to say about databases that is JSF-specific. To learn more ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required