Galera,
Ja estou desesperado. O que tem de errado nesse código para nao atualizar o registro. Eu mando atualizar o registro e nao acontece nada! Estou desestimulado com o estudo por conta desse erro. Ja estou a dois dias tentando enteder o que esta acontencendo e cansei. Nao sei mais para onde olhar nesse codigo fonte. :(
alterar.xhtml<f:view>
<ui:composition template="/app/template/templateMaster.xhtml">
<p:messages/>
<ui:define name="informacao">
<h:outputText value="Adicionar escola"/>
</ui:define>
<ui:define name="content">
<c:if test="#{UsuarioBEAN.verificaSessao()}">
<h:form>
<h:panelGrid columns="2" border="1">
<h:outputLabel value="ID:"/>
<h:outputLabel value="#{EscolaBEAN.escola.idEscola}"/>
<h:outputText value="Nome: "/>
<h:inputText size="40" value="#{EscolaBEAN.escola.nome}" required="true" requiredMessage="O campo NOME é obrigatório"/>
<h:outputText value="Municipio:"/>
<h:selectOneMenu id="tipo" value="#{EscolaBEAN.municipio.idMunicipio}" required="true" requiredMessage="O campo MUNICÍPIO é obrigatório">
<f:selectItems id="setMunicipio" value="#{MunicipioBEAN.combo}"/>
</h:selectOneMenu>
<h:outputText value="Código Mec "/>
<h:inputText size="10" value="#{EscolaBEAN.escola.codMec}" required="true" requiredMessage="O campo Código é obrigatório"/>
<h:outputText value="Rua:"/>
<h:inputText size="60" value="#{EscolaBEAN.escola.rua}" required="true" requiredMessage="O campo RUA é obrigatório"/>
<h:outputText value="Bairro:"/>
<h:inputText size="40" value="#{EscolaBEAN.escola.bairro}" required="true" requiredMessage="O campo BAIRRO é obrigatório"/>
<h:outputText value="Numero:"/>
<h:inputText size="10" value="#{EscolaBEAN.escola.numero}" required="true" requiredMessage="O campo NUMERO é obrigatório"/>
<h:outputText value="Complemento:"/>
<h:inputText size="40" value="#{EscolaBEAN.escola.complemento}" required="true" requiredMessage="O campo COMPLEMENTO é obrigatório"/>
<h:outputText value="Email:"/>
<h:inputText size="30" value="#{EscolaBEAN.escola.email}" required="true" requiredMessage="O campo EMAIL é obrigatório"/>
<h:outputText value="Modalidade:"/>
<h:selectOneMenu id="ativo" value="#{EscolaBEAN.escola.modalidade}" required="true" requiredMessage="O campo MODALIDADE é obrigatório">
<f:selectItem id="selectModalidade" itemValue="" itemLabel="Selecione..."/>
<f:selectItem id="item2" itemLabel="CEEP" itemValue="CEEP"/>
<f:selectItem id="item3" itemLabel="CETEP" itemValue="CETEP"/>
</h:selectOneMenu>
<h:commandButton value="Salvar" action="#{EscolaBEAN.alterar}"/>
</h:panelGrid>
</h:form>
</c:if>
</ui:define>
</ui:composition>
</f:view>
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.pablo.bean;
import br.com.pablo.dao.EscolaDAO;
import br.com.pablo.entity.Escola;
import br.com.pablo.entity.Municipio;
import java.sql.SQLException;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
/**
*
* @author alex
*/
@ManagedBean(name = "EscolaBEAN")
@RequestScoped
public class EscolaBEAN {
private Escola escola;
private List<Escola> escolas;
private Municipio municipio = new Municipio();
public Escola getEscola() {
return escola;
}
public Municipio getMunicipio() {
return municipio;
}
public void setMunicipio(Municipio municipio) {
this.municipio = municipio;
}
public void setEscola(Escola escola) {
this.escola = escola;
}
public List<Escola> getEscolas() {
return escolas;
}
public void setEscolas(List<Escola> escolas) {
this.escolas = escolas;
}
/** Creates a new instance of EscolaBEAN */
public EscolaBEAN() {
if (escola == null) {
escola = new Escola();
escola.setMunicipio(municipio);
}
}
public List<Escola> getListaTodos() throws SQLException {
return this.escolas = new EscolaDAO().getTodos();
}
public String cadastrar() throws SQLException {
EscolaDAO dao = new EscolaDAO();
dao.adcionarEscola(escola);
return "irParaListar";
}
public String deletar() throws SQLException {
EscolaDAO dao = new EscolaDAO();
dao.deletarEscola(escola);
return "irParaListar";
}
public String alterar() throws SQLException {
EscolaDAO dao = new EscolaDAO();
dao.atualizarEscola(escola);
return "irParaListar";
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.pablo.bean;
import br.com.pablo.dao.MunicipioDAO;
import br.com.pablo.entity.Municipio;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.model.SelectItem;
/**
*
* @author alex
*/
@ManagedBean(name="MunicipioBEAN")
@RequestScoped
public class MunicipioBEAN {
private Municipio municipio;
private List<Municipio> municipios = null;
private List<SelectItem> combo;
/** Creates a new instance of MunicipioBEAN */
public MunicipioBEAN() {
if (municipio == null) {
municipio = new Municipio();
}
}
public void setCombo(List<SelectItem> combo) {
this.combo = combo;
}
public Municipio getMunicipio() {
return municipio;
}
public void setMunicipio(Municipio municipio) {
this.municipio = municipio;
}
public List<Municipio> getMunicipios() {
return municipios;
}
public void setMunicipios(List<Municipio> municipios) {
this.municipios = municipios;
}
public List<Municipio> getTodos() throws SQLException
{
return municipios = new MunicipioDAO().getTodos();
}
public List<SelectItem> getCombo() throws SQLException {
if (combo == null) {
ArrayList<Municipio> todosMunicipios = (ArrayList<Municipio>) this.getTodos();
combo = new ArrayList<SelectItem>();
combo.add(new SelectItem("", "Selecione..."));
for (Municipio set : todosMunicipios) {
combo.add(new SelectItem(set.getIdMunicipio(), set.getDescricao()));
}
}
return combo;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.pablo.dao;
import br.com.pablo.entity.Atendimento;
import br.com.pablo.entity.Escola;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author pablosouza
*/
public class EscolaDAO extends GenericDAO {
private Escola escola = new Escola();
private List<Escola> escolas = null;
@Override
public Object preencherEntidade(ResultSet rs) throws SQLException {
Escola esc = new Escola();
esc.setIdEscola(rs.getInt("ID_ESCOLA"));
esc.setNome(rs.getString("NOME"));
esc.setRua(rs.getString("RUA"));
esc.setBairro(rs.getString("BAIRRO"));
esc.setNumero(rs.getInt("NUMERO"));
esc.setComplemento(rs.getString("COMPLEMENTO"));
esc.setEmail(rs.getString("EMAIL"));
esc.setModalidade(rs.getString("MODALIDADE"));
esc.setCodMec(rs.getInt("COD_MEC"));
esc.setMunicipio(new MunicipioDAO().getMunicipio(rs.getInt("ID_MUNICIPIO")));
//escola.setTelefones(new TelefoneEscolaDAO().getTelefones(rs.getInt("ID_ESCOLA")));
//escola.setAtendimentos(new AtendimentoDAO().getAtendimentos(rs.getInt("ID_ESCOLA")));
return esc;
}
public Escola getEscola(Integer idEscola) throws SQLException {
ResultSet rs = executarQuery("SELECT * FROM TB_ESCOLA WHERE ID_ESCOLA = ?", idEscola);
if (rs.next()) {
escola = (Escola) preencherEntidade(rs);
}
return escola;
}
public void adcionarEscola(Escola escola) throws SQLException {
String sql = "INSERT INTO TB_ESCOLA("
+ "COD_MEC,"
+ "NOME,"
+ "RUA,"
+ "BAIRRO,"
+ "NUMERO,"
+ "COMPLEMENTO,"
+ "EMAIL,"
+ "MODALIDADE,"
+ "ID_MUNICIPIO) "
+ "VALUES (?,?,?,?,?,?,?,?,?)";
executarComando(sql,
escola.getCodMec(),
escola.getNome(),
escola.getRua(),
escola.getBairro(),
escola.getNumero(),
escola.getComplemento(),
escola.getEmail(),
escola.getModalidade(),
escola.getMunicipio().getIdMunicipio());
}
public void atualizarEscola(Escola escola) throws SQLException {
String sql = "UPDATE TB_ESCOLA SET COD_MEC=?, NOME=?, RUA=?,BAIRRO=?, "
+ "NUMERO=?, COMPLEMENTO=?, EMAIL=?, MODALIDADE=?, ID_MUNICIPIO=? WHERE ID_ESCOLA=?";
executarComando(sql,
escola.getCodMec(),
escola.getNome(),
escola.getRua(),
escola.getBairro(),
escola.getNumero(),
escola.getComplemento(),
escola.getEmail(),
escola.getModalidade(),
escola.getMunicipio().getIdMunicipio(),
escola.getIdEscola());
}
public void deletarEscola(Escola escola) throws SQLException{
String sql = "DELETE FROM TB_ESCOLA WHERE ID_ESCOLA = ?";
executarComando(sql, escola.getIdEscola());
}
public List<Escola> getTodos() throws SQLException{
ResultSet rs = executarQuery("SELECT * FROM TB_ESCOLA");
List<Escola> escolas = null;
if(rs.next()){
escolas = new ArrayList<Escola>();
do {
Escola escola = (Escola) preencherEntidade(rs);
escolas.add(escola);
} while (rs.next());
}
rs.close();
return escolas;
}
public List<Atendimento> getAtendimentos(Escola esc) throws SQLException{
ArrayList<Atendimento> atendimentos = new ArrayList<Atendimento>();
ResultSet rs = executarQuery("SELECT * FROM TB_ATENDIMENTO WHERE ID_ESCOLA = ?", esc.getIdEscola());
if(rs.next()){
do {
} while (rs.next());
}
return null;
}
}
