Skip to main content

JSF Tables with databases in NetBeans 6.0

Posted by ddevore on February 5, 2008 at 6:42 PM PST

This is an addition to the JSF Visual Web JavaServer Faces in NetBeans Tutorial. For this information to be useful to you you will have to have a JSF Visual Web JavaServer Faces project created. To create the project please refer to the earlier blog. This is the project I will be working with in this tutorial.

Empty Table

This is what you would do to start with most of the components in JSF. First open Page1.jsp and you will see the Design selected with the nice grid.

The next step is to select the Table from the Palette and click in the Designer. At this point you will have a page that looks like this.

If you run the project at this point you will get a page that looks just like the Designer above. This is because the default table data object DefaultTableDataProvider provides some data by default. The next step is to put some data into the table that is not simply the default.

Database data in Table

For this part you will need a database connection defined, please check here to create a connection. Once the database is defined open the page designer with the table in it and select the Services tab. For this tutorial I have a database named JSFTutorial in MySQL with a table named address.

I will be using the address table, since it is the only table I currently have defined. To bind the JSF table to the address table simply drag and drop the table from the Services tab to the JSF table in the designer. You will now be asked to choose a target, select table1. Once do this you will see the table change to reflect the same number of rows as the address table with the same names as the columns.

That is all there is to binding a database to a JSF Table in NetBeans 6.0.

You can now change the column names and the table name by simply clicking on them or selecting the JSP tab of Page1 and change the headerText attributes. You can also select the table and check out the properties and see what they do, pagination is really cool. Next time I will discuss how to use different types of data for the table.

Related Topics >>


I have a JSF table that exceeds my page width. How can I make the table scroll horizontally?

The next blog on working with POJOs has been posted. It can be found here.

Funny you should say that. My next blog is exactly that topic because I agree with you, it is convenient to bind directly to a DB but in the world of services and large applications we work more with objects and not directly with DBs. Give me a day or two and you will have the information you need.

It is quite handy to bind table directly to db, but I'd rather have binding to some service layer which gets data from db, webservice, filesystem... Is anyone willing to provide an example of binding table (or any other JSF component) to a result of my service method? I guess instance of service class should be injected somehow, right? I am new to JSF and worked a lot with servlets and struts, and would like to start working with JSF. Any hints would be appreciated. thanx

I am able to display my tables correctly but would like to change the color of the rows to change depending on the value of one of the columns. What would be the easiest way for me to do that?