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>
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;
}
}
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;
}
}