dataTable... No records found.

Components, Ajax Framework, Utilities and More.
Post Reply
analog
Posts: 1
Joined: 29 Apr 2011, 10:13

09 May 2011, 16:26

Hello,

I am new to primefaces and I need some help.

I want to create a simple application with a database connection.
I use dataTable component to list things from a table.
I have this message: "No records found." when I run my project.
I use NetBeans 7, PrimeFaces 2.2.1, MySQL 5 and Tomcat 7.

I have this code:

index.html:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.prime.com.tr/ui">
    <h:head>
    </h:head>
    <h:body>
        <h:form>
        <p:dataTable value="#{customerBean.getCustomerList()}" var="c">
     		<p:column>
                 	<f:facet name="header">
    				Customer ID
                        </f:facet>
    			<h:outputText value="#{c.customerID}"/>
    		</p:column>
     		<p:column>
    			<f:facet name="header">
    				Name
			</f:facet>
    			<h:outputText value="#{c.name}"/>
    		</p:column>
  		<p:column>
    			<f:facet name="header">
    				Address
			</f:facet>
    			<h:outputText value="#{c.address}"/>
    		</p:column> 
    		<p:column>
    			<f:facet name="header">
    				Created Date
			</f:facet>
    			<h:outputText value="#{c.created_date}"/>
    		</p:column>
     	</p:dataTable>
        </h:form>
     </h:body>
 </html>
Customer.java:

Code: Select all

package oribase;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import java.util.Date;

@ManagedBean
@RequestScoped
public class Customer {
    public long customerID;
    public String name;
    public String address;
    public Date created_date;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Date getCreated_date() {
        return created_date;
    }

    public void setCreated_date(Date created_date) {
        this.created_date = created_date;
    }

    public long getCustomerID() {
        return customerID;
    }

    public void setCustomerID(long customerID) {
        this.customerID = customerID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
CustomerBean.java:

Code: Select all

package oribase;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.sql.DataSource;

@ManagedBean(name="customer")
@SessionScoped
public class CustomerBean implements Serializable{
 	
        //resource injection
	@Resource(name="jdbc/mkyongdb")
	private DataSource ds;
 	
        //connect to DB and get customer list
	public List<Customer> getCustomerList() throws SQLException{
 		if(ds==null)
		throw new SQLException("Can't get data source");
                
                //get database connection
		Connection con = ds.getConnection();
 		if(con==null)
		throw new SQLException("Can't get database connection");
 		PreparedStatement ps = con.prepareStatement("select customer_id, name, address, created_date from customer"); 
 		
                //get customer data from database
		ResultSet result =  ps.executeQuery();
 		List<Customer> list = new ArrayList<Customer>();
 		while(result.next()){
			Customer cust = new Customer();
 			cust.setCustomerID(result.getLong("customer_id"));
			cust.setName(result.getString("name"));
			cust.setAddress(result.getString("address"));
			cust.setCreated_date(result.getDate("created_date"));
 			
                        //store all data into a List
			list.add(cust);
		}
 		return list;
	}
}
Can anyone tell me where my code is wrong and why I get this message?

giberius
Posts: 42
Joined: 08 Jan 2011, 14:20

10 May 2011, 14:31

Try to make your list private and make getters/setters for it. I think this is your issue.

First put some records in the list by hand and/or make sure your list is populated.

User avatar
imanol
Posts: 20
Joined: 04 Nov 2010, 13:16

10 May 2011, 15:01

Try changing this:

Code: Select all

<p:dataTable value="#{customerBean.getCustomerList()}" var="c">
with this:

Code: Select all

<p:dataTable value="#{customerBean.customerList}" var="c">

Post Reply
  • Information
  • Who is online

    Users browsing this forum: Baidu [Spider] and 10 guests