Estou com dificuldades em atualizar um campo boolean. Eu marco o campo checkbox na pagina jsp, mas o sistema não passa para o banco como true, sempre vai como FALSE.
Abaixo está o arquivo jsp:
<%@page import="br.com.jairo.modelo.Venda"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib tagdir="/WEB-INF/tags" prefix="tagsJairo" %>
<tagsJairo:verificaUsuario/>
<c:import url="cabecalho.jsp"/>
<h1>Atualização de Pedidos</h1>
<form id="formAlteraPedido" method="post" action="AlteraPedido">
<div class="campos">
<label for="vencodigo">Codigo do Pedido.:</label>
<input type="text" name="vencodigo" value="${param.vencodigo}" readonly size="10"<br><br>
</div>
<div class="campos">
<label for="vendata">Data do Pedido.:</label>
<input type="date" onkeypress="formata_mascara(this,'##/##/####'); return Numero(event);" name="vendata" id="vendata" value="${param.vendata}" size="10" <br><br>
</div>
<div class="campos">
<label for="vencli">Nome do Cliente.:</label>
<input type="text" name="vencli" value="${param.vencli}" readonly size="10"<br><br>
</div>
<div class="campos">
<label for="venvaltotal">Valor Total do Pedido.:</label>
<input type="text" name="venvaltotal" value="${param.venvaltotal}" readonly size="10"<br><br>
</div>
<div class="campos">
<label for="venobs">Observações do Pedido.:</label>
<input type="text" name="venobs" value="${param.venobs}" size="100" <br>
</div>
<div class="campos">
<label for="vensituacao">Pedido Concluido.:</label>
<input type="checkbox" name="vensituacao" value="${param.vensituacao}" é neste campo que quero fazer o update<br>
</div>
<input type="submit" name="Atualizar" value="Atualizar"
</form>
<c:import url="rodape.jsp"/>
Abaixo está Classe Modelo:
public class Venda {
private int venCodigo;
private Date venData;
private String venCli;
private double venValTotal;
private String venObs;
private boolean venSituacao;
/**
* @return the venCodigo
*/
public int getVenCodigo() {
return venCodigo;
}
/**
* @param venCodigo the venCodigo to set
*/
public void setVenCodigo(int venCodigo) {
this.venCodigo = venCodigo;
}
/**
* @return the venData
*/
public Date getVenData() {
return venData;
}
/**
* @param venData the venData to set
*/
public void setVenData(Date venData) {
this.venData = venData;
}
/**
* @return the venCli
*/
public String getVenCli() {
return venCli;
}
/**
* @param venCli the venCli to set
*/
public void setVenCli(String venCli) {
this.venCli = venCli;
}
/**
* @return the venValTotal
*/
public double getVenValTotal() {
return venValTotal;
}
/**
* @param venValTotal the venValTotal to set
*/
public void setVenValTotal(double venValTotal) {
this.venValTotal = venValTotal;
}
/**
* @return the venObs
*/
public String getVenObs() {
return venObs;
}
/**
* @param venObs the venObs to set
*/
public void setVenObs(String venObs) {
this.venObs = venObs;
}
/**
* @return the venSituacao
*/
public boolean isVenSituacao() {
return venSituacao;
}
/**
* @param venSituacao the venSituacao to set
*/
public void setVenSituacao(boolean venSituacao) {
this.venSituacao = venSituacao;
}
}
Abaixo está o método que utilizo na Classe DAO para atualizar:
//metodo que altera o pedido feito pelo usuario
public void alteraPedido(Venda vendas) throws SQLException {
String sql = "update vendas set vendata=?, vencli=?, venvaltotal=?, venobs=?, vensituacao=? where vencodigo=?";
PreparedStatement ps = null;
try {
ps = conexao.prepareStatement(sql);
ps.setDate(1, new java.sql.Date(vendas.getVenData().getTime()));
ps.setString(2, vendas.getVenCli());
ps.setDouble(3, vendas.getVenValTotal());
ps.setString(4, vendas.getVenObs());
ps.setBoolean(5, vendas.isVenSituacao());
ps.setInt(6, vendas.getVenCodigo());
ps.execute();
} catch (SQLException er) {
Logger.getLogger(VendaDAO.class.getName()).log(Level.SEVERE, null, er);
} finally {
ps.close();
}
}
Abaixo está o código que utilizo no servlet:
public class AlteraPedido extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, ParseException, SQLException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String codigo = request.getParameter("vencodigo");
String data = request.getParameter("vendata");
String cliente = request.getParameter("vencli");
String valorTotal = request.getParameter("venvaltotal");
String obs = request.getParameter("venobs");
String sit = request.getParameter("vensituacao");
Venda venda = new Venda();
venda.setVenCodigo(Integer.parseInt(codigo));
DateFormat formatoData = new SimpleDateFormat("dd/MM/yyyy");
if(data != null) {
Date dataFormatada = formatoData.parse(data);
venda.setVenData(dataFormatada);
}
venda.setVenCli(cliente);
venda.setVenValTotal(Double.parseDouble(valorTotal));
venda.setVenObs(obs);
venda.isVenSituacao();//ACHO TAMBÉM QUE DEVO FAZER UMA CORREÇÃO AQUI, MAS NÃO SEI COMO
VendaDAO vendaDAO = new VendaDAO();
vendaDAO.alteraPedido(venda);
RequestDispatcher rd = request.getRequestDispatcher("/PedidosCRUD");
rd.forward(request, response);
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(AlteraPedido.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (ParseException ex) {
Logger.getLogger(AlteraPedido.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(AlteraPedido.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (ParseException ex) {
Logger.getLogger(AlteraPedido.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}