Pessoal, estou precisando buscar os dados através de um código, o código é obtido atraves dos objetos que estão em um combo, o problema é que ao carregar o JSP da erro…
Classe DAO
public class ClienteDAO {
public static boolean save(Cliente cliente) {
String sql = "insert into cliente (clinome,clifone,clidivida,clirua,bairro,cidade)values(?,?,?,?,?,?)";
try {
PreparedStatement pstm = (PreparedStatement) new Conexao().getConexao().prepareStatement(sql);
pstm.setString(1, cliente.getCliNome());
pstm.setString(2, cliente.getCliFone());
pstm.setDouble(3, cliente.getCliDivida());
pstm.setString(4, cliente.getCliRua());
pstm.setString(5, cliente.getCliBairro());
pstm.setString(6, cliente.getCliCidade());
pstm.executeUpdate();
return true;
} catch (Exception e) {
System.out.println("Erro ao inserir o Cliente\n" + e);
return false;
}
}
public boolean delete(Cliente cliente) {
String sql = "DELETE cliente WHERE idcliente = ?";
try {
PreparedStatement pstm = (PreparedStatement) new Conexao().getConexao().prepareStatement(sql);
pstm.setInt(1, cliente.mostrar_id());
pstm.executeUpdate();
return true;
} catch (Exception e) {
return false;
}
}
public static boolean update(Cliente cliente) {
String sql = "UPDATE cliente SET clinome = ? WHERE idcliente = ?";
try {
PreparedStatement pstm = (PreparedStatement) new Conexao().getConexao().prepareStatement(sql);
pstm.setInt(1, cliente.mostrar_id());
pstm.setString(2, cliente.mostrar_nome());
pstm.executeUpdate();
return true;
} catch (Exception e) {
return false;
}
}
public static Cliente recuperate(int codigo) {
Cliente cli;
String sql = "SELECT idcliente, clinome, clifone, clidivida ,clirua,bairro, cidade FROM cliente WHERE idcliente = " + codigo;
try {
Statement stm = (Statement) new Conexao().getConexao().createStatement();
ResultSet rs = (ResultSet) stm.executeQuery(sql);
if (rs.next()) {
cli = new Cliente();
cli.setCodigo(rs.getInt("idcliente"));
cli.setNome(rs.getString("clinome"));
cli.setCliFone(rs.getString("clifone"));
cli.setCliDivida(Double.valueOf(rs.getString("clidivida")));
cli.setCliRua(rs.getString("clirua"));
cli.setCliBairro(rs.getString("bairro"));
cli.setCliCidade(rs.getString("cidade"));
return cli;
} else {
return null;
}
} catch (Exception e) {
return null;
}
}
public static Cliente recuperate(String nome) {
Cliente cli;
String sql = "SELECT idcliente, clinome FROM cliente WHERE clinome = " + nome;
try {
Statement stm = (Statement) new Conexao().getConexao().createStatement();
ResultSet rs = (ResultSet) stm.executeQuery(sql);
if (rs.next()) {
cli = new Cliente();
cli.setCodigo(rs.getInt("idcliente"));
cli.setNome(rs.getString("clinome"));
return cli;
} else {
return null;
}
} catch (Exception e) {
return null;
}
}
public static List<Cliente> recuperateAll(){
List<Cliente> lista = new ArrayList<Cliente>();
String sql;
Cliente client;
sql = "select clinome, clifone, clidivida, clirua, bairro, cidade from cliente ORDER BY clinome";
try {
Statement stm = (Statement) new Conexao().getConexao().createStatement();
ResultSet rs = (ResultSet) stm.executeQuery(sql);
while(rs.next()){
client = new Cliente();
client.setNome(rs.getString("clinome"));
client.setCliFone(rs.getString("clifone"));
client.setCliDivida(Double.valueOf(rs.getString("clidivida")));
client.setCliRua(rs.getString("clirua"));
client.setCliBairro(rs.getString("bairro"));
client.setCliCidade(rs.getString("cidade"));
lista.add(client);
}
} catch (Exception e) {
lista = null;
System.out.println("Erro ao montar lista de Clientes\nErro:\n" + e);
}
return lista;
}
}
JSP
<%
int cod;
cod = Integer.valueOf(request.getParameter("cmbCliente"));
Cliente cliente;
cliente = ClienteDAO.recuperate(cod);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<h1>Alteração de Cliente</h1>
<form id="form1" name="form1" method="post" action="SrvAlteraCli">
<p> </p>
<table width="489" border="0">
<tr>
<td><strong>Código</strong></td>
<td><input type="text" name="txtId" readonly value="<%= cliente.getIdCliente() %>" /></td>
</tr>
<tr>
<td width="100"><strong>Nome</strong></td>
<td width="379"><label>
<input type="text" name="txtNome" readonly value="<%= cliente.getCliNome() %>" />
</label></td>
</tr>
<tr>
<td><strong>Fone</strong></td>
<td><label>
<input type="text" name="txtFone" readonly value="<%= cliente.getCliFone() %>" />
</label></td>
</tr>
<tr>
<td><strong>Rua</strong></td>
<td><label>
<input type="text" name="txtRua" readonly value="<%= cliente.getCliRua() %>" />
</label></td>
</tr>
<tr>
<td><strong>Bairro</strong></td>
<td><label>
<input type="text" name="txtBairro" readonly value="<%= cliente.getCliBairro() %>" />
</label></td>
</tr>
<tr>
<td><strong>Cidade</strong></td>
<td><input type="text" name="txtCidade" readonly value="<%= cliente.getCliCidade() %>" /></td>
</tr>
<tr>
<td><strong>Dívida</strong></td>
<td><label>
<input type="text" name="txtDivida" readonly value="<%= cliente.getCliDivida() %>" />
</label></td>
</tr>
<tr>
<td><label></label></td>
<td><input type="submit" name="Submit" value="CADASTRAR" /></td>
</tr>
</table>
<p> </p>
</form>
</html>
Servlet
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String nome, fone, divida , rua, bairro, cidade;
int cod;
cod = Integer.valueOf(request.getParameter("txtId"));
nome = request.getParameter("txtNome");
fone = request.getParameter("txtFone");
rua = request.getParameter("txtRua");
bairro = request.getParameter("txtBairro");
cidade = request.getParameter("txtCidade");
divida = request.getParameter("txtDivida");
Cliente cli;
cli = new Cliente();
cli.setCodigo(Integer.valueOf(cod));
cli.setCliNome(nome);
cli.setCliFone(fone);
cli.setCliRua(rua);
cli.setCliBairro(bairro);
cli.setCliCidade(cidade);
cli.setCliDivida(Double.valueOf(divida));
boolean resultado = false;
resultado = ClienteDAO.update(cli);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet SrvSalvaCliente</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet SrvSalvaCliente at " + request.getContextPath () + "</h1>");
if(resultado)
out.println("<h1>Cliente salvo com Sucesso!</h1>");
else
out.println("<h1>Erro ao Salvar Cliente.</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="Métodos HttpServlet. Clique no sinal de + à esquerda para editar o código.">
/**
* 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 {
processRequest(request, response);
}
/**
* 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 {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Erro:
org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at org.apache.jsp.AlteraCli_jsp._jspService(AlteraCli_jsp.java:80)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)