Boa Tarde.
Meu professor passou um código pré-pronto e pediu eu fazer uma gráfico de pizza pegando os dados do banco e uma tabela com os dados do banco,
Eu consegui fazer o gráfico mas a tabela não estou conseguindo colocar os dados, alguém poderia me dar um luz, já tentei pesquisar mas o modo que fazem não está dando certo.
Segue o código:
<?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://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<p:chart type="pie" model="#{chartView.pieModel1}" style="width:700px;height:600px" />
<input type='button' value='Voltar' onclick='history.go(-1)' />
<p:dataTable var="voto" value="#{pesquisaMB.voto}">
<p:column headerText="ID">
<h:outputText value="#{voto.getVoto()}" />
</p:column>
<p:column headerText="Tecnologia">
<h:outputText value="#{voto.getVoto()}" />
</p:column>
<p:column headerText="Votos">
<h:outputText value="#{voto.getVoto()}" />
</p:column>
</p:dataTable>
</h:body>
</html>
Classe PesquisaMB
@ManagedBean
@RequestScoped
public class PesquisaMB {
private Integer voto;
EntityManagerFactory emf = Persistence.createEntityManagerFactory("pesqjsfPU");
PesquisaJpaController daoPesquisa = new PesquisaJpaController(emf);
/**
* Creates a new instance of PesquisaMB
*/
public PesquisaMB() {
}
public Integer getVoto() {
return voto;
}
public String salvaVoto() throws Exception{
Pesquisa ps = daoPesquisa.findPesquisa(voto);
ps.setVotos(ps.getVotos()+1);
daoPesquisa.edit(ps);
return "resultado";
}
public void setVoto(Integer voto) {
this.voto = voto;
System.out.println(voto);
}
}
Classe Pesquisa
@Entity
@Table(name = "PESQUISA")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Pesquisa.findAll", query = "SELECT p FROM Pesquisa p"),
@NamedQuery(name = "Pesquisa.findById", query = "SELECT p.escolha FROM Pesquisa p WHERE p.id = :id"),
@NamedQuery(name = "Pesquisa.findByEscolha", query = "SELECT p FROM Pesquisa p WHERE p.escolha = :escolha"),
@NamedQuery(name = "Pesquisa.findByVotos", query = "SELECT p FROM Pesquisa p WHERE p.votos = :votos")})
public class Pesquisa implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "ID")
private Integer id;
@Column(name = "ESCOLHA")
private String escolha;
@Column(name = "VOTOS")
private Integer votos;
public Pesquisa() {
}
public Pesquisa(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEscolha() {
return escolha;
}
public void setEscolha(String escolha) {
this.escolha = escolha;
}
public Integer getVotos() {
return votos;
}
public void setVotos(Integer votos) {
this.votos = votos;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Pesquisa)) {
return false;
}
Pesquisa other = (Pesquisa) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "entidade.Pesquisa[ id=" + id + " ]";
}
}
Classe PesquisaJpaController
public class PesquisaJpaController implements Serializable {
public PesquisaJpaController(EntityManagerFactory emf) {
this.emf = emf;
}
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Pesquisa pesquisa) throws PreexistingEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(pesquisa);
em.getTransaction().commit();
} catch (Exception ex) {
if (findPesquisa(pesquisa.getId()) != null) {
throw new PreexistingEntityException("Pesquisa " + pesquisa + " already exists.", ex);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(Pesquisa pesquisa) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
pesquisa = em.merge(pesquisa);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
Integer id = pesquisa.getId();
if (findPesquisa(id) == null) {
throw new NonexistentEntityException("The pesquisa with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(Integer id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Pesquisa pesquisa;
try {
pesquisa = em.getReference(Pesquisa.class, id);
pesquisa.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The pesquisa with id " + id + " no longer exists.", enfe);
}
em.remove(pesquisa);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public List<Pesquisa> findPesquisaEntities() {
return findPesquisaEntities(true, -1, -1);
}
public List<Pesquisa> findPesquisaEntities(int maxResults, int firstResult) {
return findPesquisaEntities(false, maxResults, firstResult);
}
private List<Pesquisa> findPesquisaEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Pesquisa.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public Pesquisa findPesquisa(Integer id) {
EntityManager em = getEntityManager();
try {
return em.find(Pesquisa.class, id);
} finally {
em.close();
}
}
public int getPesquisaCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<Pesquisa> rt = cq.from(Pesquisa.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
}