[quote=@ThalitaPinheiro]Bota o código…
Possivelmente vc esqueceu de instanciar alguma coisa… =/[/quote]
Que parte do código ±…rs
[code]<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
“http://www.w3.org/TR/html4/loose.dtd”>
<%@page contentType=“text/html” pageEncoding=“UTF-8”%>
<%@page import=“java.util.Set”%>
<%@taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c” %>
<%@taglib uri=“http://java.sun.com/jsp/jstl/fmt” prefix=“fmt”%>
<html>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=windows-1252”/>
<title>CADASTRAR PESSOA GAMBIARRA</title>
<link rel=“StyleSheet” href="<%=request.getContextPath()%>/estilos/Estilos.css" type=“text/css”/>
<style type=“text/css”>
body {
background-color: #ffffff;
}
</style>
<script type="text/javascript">
function cadastrarLivro()
{
document.form.action = "<%=request.getContextPath()%>/livro/cadastrar?CADASTRAR_LIVRO=1";
document.form.submit();
}
function campoNumerico(campo)
{
var digits = "0123456789"
var temp
var ok = true;
for (var i = 0; i < campo.value.length; i++)
{
temp = campo.value.substring(i, i + 1)
if (digits.indexOf(temp) == -1)
{
alert("O campo deve ser preenchido apenas com números!")
campo.focus();
campo.value = "";
return false;
}
}
}
function alterarLivro(codigo)
{
document.form.action = "<%=request.getContextPath()%>/alterar/livro/cleverson?CODIGO_LIVRO=" + codigo;
document.form.submit();
}
function alterar()
{
document.form.action = "<%=request.getContextPath()%>/alterar/livro/cleverson?ALTERAR_LIVRO=1";
document.form.submit();
}
function deletar()
{
document.form.action = "<%=request.getContextPath()%>/excluir/livro/cleverson?EXCLUIR_LIVRO=1";
document.form.submit();
}
function mostrarMensagem()
{
<c:if test="${not empty exception}">
alert('<c:out value="${exception.message}"/>');
</c:if>
}
</script>
</head>
<body onload="mostrarMensagem();">
<form name="form" method="POST" action="">
<table cellspacing="0" cellpadding="0" border="0" width="50%"
align="center">
<tr>
<td width="30%" align="right">NOME:</td>
<td>
<input type="text" name="livro_nome" maxlength="50" size="50"
align="left" id="txtLivro"
value='<c:out value="${livroVo.nome}"/>'
/>
<input type="hidden" name="livro_codigo" value='<c:out value="${livroVo.codigo}"/>'/>
</td>
</tr>
<tr>
<td align="right">AUTOR:</td>
<td>
<input type="text" name="livro_autor" maxlength="100" align="left"
size="50" id="txtAutor"
value='<c:out value="${livroVo.autor}"/>'
/>
</td>
</tr>
<tr>
<td align="right">PRECO:</td>
<td>
<input type="text" name="livro_preco" maxlength="6" align="left"
size="10" id="txtQuant" onblur="campoNumerico(this)"
value='<c:out value="${livroVo.precoUnitario}"/>'
/>
</td>
</tr>
<tr>
<td align="right">ASSUNTO:</td>
<td>
<input type="text" name="livro_ass" maxlength="500" align="left"
size="100" id="txtAss"
value='<c:out value="${livroVo.assunto}"/>'
/>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" value="CADASTRAR" id="btCad" onclick="cadastrarLivro();"/>
<c:if test="${alterar}">
<input type="button" value="ALTERAR" id="btAlt" onclick="alterar();"/>
</c:if>
<c:if test="${deletar}">
<input type="button" value="DELETAR" id="btDel" onclick="deletar();"/>
</c:if>
</td>
</tr>
<tr>
<td colspan="2" align="center"><c:out value="${mensagem}"/></td>
</tr>
<tr>
<td colspan="2" align="center"><c:out value="${mensagemErro}"/></td>
</tr>
</table>
<table cellspacing="0" cellpadding="5" border="1" width="50%" align="center">
<tr>
<td>CODIGO</td>
<td>NOME</td>
<td>AUTOR</td>
<td>PREÇO</td>
<td>ASSUNTO</td>
</tr>
<c:forEach var="livroAtual" items="${listaLivros}" varStatus="status">
<c:set var="LINHA_ESTILO" value="TR-ExibicaoPar"/>
<c:if test="${status.index % 2 != 0}">
<c:set var="LINHA_ESTILO" value="TR-ExibicaoImpar"/>
</c:if>
<tr class="<c:out value='${LINHA_ESTILO}'/>">
<td align="center">
<a ${livroAtual.codigo}"/>);">
<c:out value="${livroAtual.codigo}"/>
</a>
</td>
<td><c:out value="${livroAtual.nome}"/></td>
<td><c:out value="${livroAtual.autor}"/></td>
<td><c:out value="${livroAtual.precoUnitario}"/></td>
<td><c:out value="${livroAtual.assunto}"/></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>[/code]
[code]package br.com.abaco.treinamento.java.noturno.livro.dao;
import br.com.abaco.treinamento.java.noturno.livro.javabean.LivroVo;
import br.com.abaco.treinamento.java.noturno.utilitarios.dao.AbstractDao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class CadastrarLivroCleversonDao extends AbstractDao
{
public CadastrarLivroCleversonDao(Connection conexaoParm) throws SQLException
{
super(conexaoParm);
}
public void cadastrarLivro(LivroVo livro) throws SQLException
{
PreparedStatement ps = null;
String sql = "insert into TB_LIVRO(CODIGO_LIVRO, NOME, AUTOR, PRECO, ASSUNTO) values (?, ?, ?, ?, ?)";
ps = getConexao().prepareStatement(sql);
ps.setLong(1, gerarSequence("SEQUENCE_LIVRO"));
ps.setString(2, livro.getNome());
ps.setString(3, livro.getAutor());
ps.setDouble(4, livro.getPrecoUnitario());
ps.setString(5, livro.getAssunto());
ps.executeUpdate();
}
protected long gerarSequence(String nomeSequence) throws SQLException
{
PreparedStatement ps = null;
ResultSet rs = null;
long codigo = 0;
String sql = "SELECT " + nomeSequence + ".NEXTVAL AS CODIGO FROM DUAL";
ps = getConexao().prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next())
{
codigo = rs.getLong("CODIGO");
}
return codigo;
}
public ArrayList listaLivros() throws SQLException
{
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "SELECT * FROM TB_LIVRO";
ps = getConexao().prepareStatement(sql);
rs = ps.executeQuery();
ArrayList lista = new ArrayList();
while(rs.next())
{
LivroVo livroAtual = new LivroVo();;
livroAtual.setCodigo(rs.getLong("CODIGO_LIVRO"));
livroAtual.setNome(rs.getString("NOME"));
livroAtual.setAutor(rs.getString("AUTOR"));
livroAtual.setPrecoUnitario(rs.getDouble("PRECO"));
livroAtual.setAssunto(rs.getString("ASSUNTO"));
lista.add(livroAtual);
}
return lista;
}
public LivroVo consultaLivros(LivroVo livroVo) throws SQLException
{
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "SELECT * FROM TB_LIVRO WHERE CODIGO_LIVRO = ?";
ps = getConexao().prepareStatement(sql);
ps.setLong(1, livroVo.getCodigo());
rs = ps.executeQuery();
if(rs.next())
{
livroVo.setCodigo(rs.getLong("CODIGO_LIVRO"));
livroVo.setNome(rs.getString("NOME"));
livroVo.setAutor(rs.getString("AUTOR"));
livroVo.setPrecoUnitario(rs.getDouble("PRECO"));
livroVo.setAssunto(rs.getString("ASSUNTO"));
}
return livroVo;
}
public void alterarLivro(LivroVo livroVo) throws SQLException
{
PreparedStatement ps = null;
String sql = "UPDATE TB_LIVRO SET NOME = ?, AUTOR = ?, PRECO = ?, ASSUNTO = ? WHERE CODIGO_LIVRO = ?";
ps = getConexao().prepareStatement(sql);
ps.setString(1, livroVo.getNome());
ps.setString(2, livroVo.getAutor());
ps.setDouble(3, livroVo.getPrecoUnitario());
ps.setString(4, livroVo.getAssunto());
ps.setLong(5, livroVo.getCodigo());
ps.executeUpdate();
}
public void excluirLivro(LivroVo livroVo) throws SQLException
{
PreparedStatement ps = null;
String sql = "DELETE FROM TB_LIVRO WHERE CODIGO_LIVRO = ?";
ps = getConexao().prepareStatement(sql);
ps.setLong(1, livroVo.getCodigo());
ps.executeUpdate();
}
}
[/code]
[code]package br.com.abaco.treinamento.java.noturno.livro.be;
import br.com.abaco.treinamento.java.noturno.exception.CampoObrigatorioException;
import br.com.abaco.treinamento.java.noturno.livro.dao.CadastrarLivroCleversonDao;
import br.com.abaco.treinamento.java.noturno.livro.javabean.LivroVo;
import br.com.abaco.treinamento.java.noturno.utilitarios.be.AbstractBe;
import java.sql.SQLException;
import java.util.ArrayList;
public class CadastrarLivroCleversonBe extends AbstractBe {
public CadastrarLivroCleversonBe() throws SQLException {
super();
}
public void cadastrarLivro(LivroVo livro) throws SQLException, CampoObrigatorioException {
try {
validaCampos(livro);
CadastrarLivroCleversonDao livroDao = new CadastrarLivroCleversonDao(getConexao());
livroDao.cadastrarLivro(livro);
commit();
} catch (Error e) {
e.printStackTrace();
rollBack();
throw e;
} catch (RuntimeException e) {
e.printStackTrace();
rollBack();
throw e;
}
}
public ArrayList listaLivros() throws SQLException {
try {
CadastrarLivroCleversonDao livro = new CadastrarLivroCleversonDao(getConexao());
ArrayList listaLivro = livro.listaLivros();
return listaLivro;
} catch (Error e) {
e.printStackTrace();
rollBack();
throw e;
} catch (RuntimeException e) {
e.printStackTrace();
rollBack();
throw e;
}
}
public LivroVo consultaLivro(LivroVo livroVoParm) throws SQLException {
try {
CadastrarLivroCleversonDao livroDao = new CadastrarLivroCleversonDao(getConexao());
return livroDao.consultaLivros(livroVoParm);
} catch (Error e) {
e.printStackTrace();
rollBack();
throw e;
} catch (RuntimeException e) {
e.printStackTrace();
rollBack();
throw e;
}
}
public void alterarLivroBe(LivroVo livro) throws SQLException, CampoObrigatorioException {
try {
validaCampos(livro);
CadastrarLivroCleversonDao livroDao = new CadastrarLivroCleversonDao(getConexao());
livroDao.alterarLivro(livro);
commit();
} catch (Error e) {
e.printStackTrace();
rollBack();
throw e;
} catch (RuntimeException e) {
e.printStackTrace();
rollBack();
throw e;
}
}
public void excluirLivro(LivroVo livroVo) throws SQLException {
try {
CadastrarLivroCleversonDao livroDao = new CadastrarLivroCleversonDao(getConexao());
livroDao.excluirLivro(livroVo);
commit();
} catch (Error e) {
e.printStackTrace();
rollBack();
throw e;
} catch (RuntimeException e) {
e.printStackTrace();
rollBack();
throw e;
}
}
private void validaCampos(LivroVo livro) throws SQLException, CampoObrigatorioException {
if (livro.getNome() == null || livro.getNome().trim().equals("")) {
throw new CampoObrigatorioException("O campo Nome � Obrigat�rio!");
}
if (livro.getAutor() == null || livro.getAutor().trim().equals("")) {
throw new CampoObrigatorioException("O campo Autor � Obrigat�rio!");
}
if (livro.getPrecoUnitario() <= 0) {
throw new CampoObrigatorioException("O campo Pre�o � Obrigat�rio!");
}
if (livro.getAssunto() == null || livro.getAssunto().trim().equals("")) {
throw new CampoObrigatorioException("O campo Assunto � Obrigat�rio!");
}
}
}
[/code]
[code]package br.com.abaco.treinamento.java.noturno.livro.servlet;
import br.com.abaco.treinamento.java.noturno.exception.CampoObrigatorioException;
import br.com.abaco.treinamento.java.noturno.livro.be.CadastrarLivroCleversonBe;
import br.com.abaco.treinamento.java.noturno.livro.javabean.LivroVo;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.;
import javax.servlet.http.;
public class CadastrarLivroCleverson extends HttpServlet
{
private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
if(request.getParameter("CADASTRAR_LIVRO") != null)
{
cadastrarLivro(request, response);
}
else
{
listarLivros(request, response);
}
}
catch(Throwable e)
{
e.printStackTrace();
String mensagemUsuario = e.getMessage();
request.setAttribute("mensagemErro", mensagemUsuario);
RequestDispatcher rd = request.getRequestDispatcher("/erros/ViewErro.jsp");
rd.forward(request, response);
}
}
public void cadastrarLivro(HttpServletRequest request, HttpServletResponse response) throws SQLException,
ServletException, IOException
{
CadastrarLivroCleversonBe livro = null;
double precoDoub = 0;
try
{
LivroVo livroVo = new LivroVo();
request.setAttribute("livroVo", livroVo);
String nomeLivro = request.getParameter("livro_nome");
String autLivro = request.getParameter("livro_autor");
String precoLivro = request.getParameter("livro_preco");
String assLivro = request.getParameter("livro_ass");
try
{
precoDoub = Double.parseDouble(precoLivro);
}
catch(NumberFormatException e)
{
e.printStackTrace();
precoDoub = 0;
}
livroVo.setNome(nomeLivro);
livroVo.setAutor(autLivro);
livroVo.setPrecoUnitario(precoDoub);
livroVo.setAssunto(assLivro);
livro = new CadastrarLivroCleversonBe();
livro.cadastrarLivro(livroVo);
ArrayList listaLivros = livro.listaLivros();
request.getSession().setAttribute("listaLivros", listaLivros);
request.setAttribute("mensagem", "Aguarde... Cadastrando!!!");
RequestDispatcher rd = request.getRequestDispatcher("/livro/CadastrarLivroCleverson.jsp");
rd.forward(request, response);
}
catch(CampoObrigatorioException e)
{
e.printStackTrace();
String mensagem = e.getMessage();
request.setAttribute("exception", e);
RequestDispatcher rd = request.getRequestDispatcher("/livro/CadastrarLivroCleverson.jsp");
rd.forward(request, response);
}
finally
{
if(livro != null)
{
livro.close();
}
}
}
private void listarLivros(HttpServletRequest request, HttpServletResponse response) throws SQLException,
ServletException, IOException
{
CadastrarLivroCleversonBe livro = null;
try
{
livro = new CadastrarLivroCleversonBe();
ArrayList listaLivros = livro.listaLivros();
request.getSession().setAttribute("listaLivros", listaLivros);
RequestDispatcher rd = request.getRequestDispatcher("/livro/CadastrarLivroCleverson.jsp");
rd.forward(request, response);
}
finally
{
if(livro != null)
{
livro.close();
}
}
}
}
[/code]