从数据库中获取数据

时间:2014-06-30 07:58:58

标签: mysql jsf primefaces

我试图从Primefaces数据表中的数据库中检索数据。我的数据库表名是dept,它有两列id(int not null主键)和name(varchar 255 not null)。我使用println来显示记录的大小。但是在启动项目后没有任何显示。在数据库表中有2条记录。

DeptTest.java

package logon;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;

@ViewScoped
@SessionScoped
@javax.faces.bean.ManagedBean(name = "depTest")
public class DeptTest implements Serializable {
    private static final long serialVersionUID = 1L;

    private static final ArrayList<Department> orderList = new ArrayList<Department>();

    public ArrayList<Department> getOrderList() {
        return orderList;
    }

    @PersistenceUnit(unitName = "Webbeans_RESOURCE_LOCAL")
    private EntityManagerFactory emf;

    public List<Department> getDeptList() {
        return deptList;
    }

    public void setDeptList(List<Department> deptList) {
        this.deptList = deptList;
    }

    public List<Department> deptList = new ArrayList();

    @PostConstruct
    public void init() {
        EntityManager em = emf.createEntityManager();
        // Read the existing entries and write to console
        Query q = em.createQuery("SELECT d FROM Dept d");
        deptList = q.getResultList();
        System.out.println("Size dept: " + deptList.size());
    }
}

0 个答案:

没有答案