Olá a todos,
Estou tempo problema para listar alguns objetos do meu banco no jsp, quando uso jsp:useBean o seguinte erro eh apresentando:
org.apache.jasper.JasperException: /listProdutos.jsp (line: 4, column: 0) The value for the useBean class attribute br.com.avaliacao.dao.ProdutoDAO is invalid.
No dao estou usando o PP singleton:
package br.com.avaliacao.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import br.com.avaliacao.dao.generic.IDAO;
import br.com.avaliacao.factory.ConnectionFactory;
import br.com.avaliacao.modelo.Produto;
public class ProdutoDAO implements IDAO<Produto>{
private Connection connection;
private static ProdutoDAO unico = null;
private ProdutoDAO() {
try{
this.connection = new ConnectionFactory().getConnection();
}catch(SQLException e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static ProdutoDAO getInstance(){
if(unico == null){
unico = new ProdutoDAO();
}
return unico;
}
@Override
public void insert(Produto prod) {
String sql = "insert into produto(nome,descricao,valorUnitario) values (?,?,?)";
PreparedStatement preparedStatement;
try{
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, prod.getNomeProduto());
preparedStatement.setString(2, prod.getDescricao());
preparedStatement.setDouble(3, prod.getValorUnitario());
preparedStatement.execute();
System.out.println("Produto cadastrado com sucesso");
preparedStatement.close();
connection.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void delete(Produto prod) {
String sql ="delete from produto where idProduto = ?";
PreparedStatement preparedStatement;
try{
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, prod.getIdProduto());
preparedStatement.close();
connection.close();
}catch(SQLException e ){
e.printStackTrace();
}
}
@Override
public void update(Produto prod) {
String sql ="update produto set nome = ? ,descricao = ?, valorUnitario = ? where idProduto = ?";
PreparedStatement preparedStatement;
try{
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, prod.getNomeProduto());
preparedStatement.setString(2, prod.getDescricao());
preparedStatement.setDouble(3, prod.getValorUnitario());
preparedStatement.setInt(4, prod.getIdProduto());
preparedStatement.execute();
preparedStatement.close();
connection.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public List<Produto> lista() {
String sql = "select * from produto";
PreparedStatement preparedStatement;
List<Produto> produtos = new ArrayList<Produto>();
try{
preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
produtos.add(populaProduto(resultSet));
}
resultSet.close();
connection.close();
}catch (SQLException e) {
e.printStackTrace();
}
return produtos;
}
public Produto populaProduto(ResultSet resultSet){
Produto produto = new Produto();
try {
produto.setNomeProduto((resultSet.getString("nome")));
produto.setDescricao(resultSet.getString("descricao"));
produto.setValorUnitario((resultSet.getDouble("valorUnitario")));
} catch (SQLException e) {
e.printStackTrace();
}
return produto;
}
@Override
public Produto findByPk(Integer id) {
String sql = "select * from produto where idProduto = ?";
PreparedStatement preparedStatement;
try{
preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
return populaProduto(resultSet);
}
resultSet.close();
preparedStatement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
e o jsp é apenas uma listagem dos items:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<jsp:useBean id="produtoDao" class="br.com.avaliacao.dao.ProdutoDAO" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Produtos Cadastrados</title>
<link rel="stylesheet" type="text/css" href="css/borda.css">
<link rel="stylesheet" type="text/css" href="css/geral.css">
</head>
<body>
<c:import url="jsp/cabecalho.jsp"/>
<label><h1>Produtos</h1></label>
<div id="formListP" class="formBorda">
<c:forEach var="produtos" items="${produtoDao.lista}">
<tr>
<td>${produto.nomeProduto}</td>
<td>${produto.valorUnitario}</td>
</tr>
</c:forEach>
</div>
<c:import url="jsp/rodape.jsp"/>
</body>
</html>
Acredito que o problema se por não ter um construtor publico, mas como eu posso usar o DAO??
Agradeço.