Problem when reloading Table

Asked

Viewed 76 times

0

I have the following problem, at first run my Defaulttablemodel loads normally as can be seen below in red:

inserir a descrição da imagem aqui

After registering, changing or deleting once, the table updates normally, but when I perform these operations again the following error occurs:

inserir a descrição da imagem aqui

How can I solve this problem?

This is my Bean:

public class Edicao {
    private String isbn;
    private double valor;
    private Integer qtde;
    private Integer numPag;
    private Integer ano;
    private Livro livro;
    private Editora editora;
    private Integer addQtde;

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public String getIsbn() {
        return isbn;
    }

    public void setValor(double valor) {
        this.valor = valor;
    }

    public double getValor() {
        return valor;
    }

    public void setQtde(Integer qtde) {
        this.qtde = qtde;
    }

    public Integer getQtde() {
        return qtde;
    }

    public void setAno(Integer ano) {
        this.ano = ano;
    }

    public Integer getAno() {
        return ano;
    }

    public void setLivro(Livro livro) {
        this.livro = livro;
    }

    public Livro getLivro() {
        return livro;
    }

    public void setNumPag(Integer numPag) {
        this.numPag = numPag;
    }

    public Integer getNumPag() {
        return numPag;
    }

    public void setEditora(Editora editora) {
        this.editora = editora;
    }

    public Editora getEditora() {
        return editora;
    }

    public void setAddQtde(Integer addQtde) {
        this.addQtde = addQtde;
    }

    public Integer getAddQtde() {
        return addQtde;
    }

}

This is my Dao method that lists the issues

public List<Edicao> listarEdicao(){

        Connection conn = null;
    PreparedStatement ps   = null;
    ResultSet rs   = null;
    String sql  = "";
    Livro livro = null;
    Editora editora = null;
    EditoraControle editoraControl = new EditoraControle();
    LivroControle livroControl = new LivroControle();

        conn = Conexao.getConexao();

        sql = "SELECT edic_isbn, edic_valor, edic_qtde, edic_numpag, edic_ano, edic_livro, edic_editora\n" +
                      "FROM public.edicao";

        List<Edicao> listaEdicao = new ArrayList<Edicao>();

        Edicao edicao = null;
        livro = null;
                editora = null;

        try {

            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();

            while(rs.next()){

                            edicao = new Edicao();

                            edicao.setIsbn(rs.getString("edic_isbn"));
                            edicao.setValor(rs.getDouble("edic_valor"));
                            edicao.setQtde(rs.getInt("edic_qtde"));
                            edicao.setNumPag(rs.getInt("edic_numpag"));
                            edicao.setAno(rs.getInt("edic_ano"));

                            livro = livroControl.consultar(rs.getInt("edic_livro"));
                            editora = editoraControl.consultar(rs.getString("edic_editora"));

                            edicao.setLivro(livro);
                            edicao.setEditora(editora);
                            listaEdicao.add(edicao);

            }
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("Erro na Consulta !!"+e.getMessage());
        }
        return listaEdicao;
    }

This is my control

public class EdicaoControle {
    Edicao edicao = new Edicao();
    EdicaoDao edicaoDao = new EdicaoDao();

    public boolean salvar(Edicao edicao){
        boolean salvou = edicaoDao.salvar(edicao);
        return salvou;
    }

    public Edicao consultar(String cnpj){
        edicao = edicaoDao.consultar(cnpj);
        return edicao;
    }

    public boolean alterar(Edicao edicao){
        boolean alterou = edicaoDao.alterar(edicao);
        return alterou;
    }

    public boolean excluir (Edicao edicao){
        boolean excluiu = edicaoDao.excluir(edicao);
        return excluiu;
    }

    public List<Edicao> listar(){
        List<Edicao> lista = new ArrayList<Edicao>();
        lista = edicaoDao.listarEdicao();
        return lista;
    }
}

This is the method in the VIEW responsible for loading the table

public void preencherTabelaEdicao(){   

       Tabela_Edicao.getColumnModel().getColumn(0).setPreferredWidth(80);
       Tabela_Edicao.getColumnModel().getColumn(1).setPreferredWidth(220);
       Tabela_Edicao.getColumnModel().getColumn(2).setPreferredWidth(4);
       Tabela_Edicao.getColumnModel().getColumn(3).setPreferredWidth(5);
       Tabela_Edicao.getColumnModel().getColumn(4).setPreferredWidth(5);
       Tabela_Edicao.getColumnModel().getColumn(5).setPreferredWidth(7);
       Tabela_Edicao.getColumnModel().getColumn(6).setPreferredWidth(220);
       DefaultTableModel modelo = (DefaultTableModel)Tabela_Edicao.getModel();
          modelo.setNumRows(0);

        List<Edicao> listaEdicao = new ArrayList<Edicao>();
        listaEdicao = edicaoControl.listar();

        try{
            for(Edicao e: listaEdicao){
                modelo.addRow(new Object[]{e.getIsbn(), e.getLivro().getLivnome(),e.getAno(),e.getNumPag(),e.getQtde(),e.getValor(),
                    e.getEditora().getEdit_nome()});                  
            }
        }catch(Exception erro){
             JOptionPane.showMessageDialog(null, "A tabela EDIÇÃO não pôde ser carregada");
         }

    }

After saving, changing and deleting I just call the method that loads the table, ie I call the method fillTable().

  • Put a erro.printStackTrace(); in his catch and see on the console what is the exception being released. Then edit your question and add the stacktrace here.

  • Please provide a [mcve] so that we can execute the code and simulate your problem.

  • 1

    I’d like to thank you all ok, I identified the problem. My mistake was simply because I was not closed the connection, both in the QUERY methods and in the LIST.

  • If you found the solution, post as answer.

1 answer

0


METHOD OF CONSULTATION OF THE EDITION:

public Edicao consultar(String isbn){
    conn = Conexao.getConexao();

    sql = "SELECT edic_isbn, edic_valor, edic_qtde, edic_numpag, edic_ano, edic_livro, edic_editora, addqtde\n" +
              "FROM public.edicao WHERE edic_isbn = '"+isbn+"'";

    Edicao edicao = null;

    try {

        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();

        if(rs.next()){

            edicao = new Edicao();

                        edicao.setIsbn(rs.getString("edic_isbn"));
                        edicao.setValor(rs.getDouble("edic_valor"));
                        edicao.setQtde(rs.getInt("edic_qtde"));
                        edicao.setNumPag(rs.getInt("edic_numpag"));
                        edicao.setAno(rs.getInt("edic_ano"));

                        livro = livroControl.consultar(rs.getInt("edic_livro"));
            editora = editoraControl.consultar(rs.getString("edic_editora"));
                        edicao.setAddQtde(rs.getInt("addqtde"));
                        edicao.setLivro(livro);
                        edicao.setEditora(editora);

                        conn.close();   // FECHA A CONEXÃO COM O BANCO DE DADOS
                        ps.close();     // FECHA O PREPAREDSTATEMENT
                        rs.close();     // FECHA O RESULTSET

        }

    } catch (Exception e) {
        // TODO: handle exception
                e.printStackTrace();
        System.out.println("Erro na Consulta EDIÇÃO!!"+e.getMessage());
    }

METHOD OF LISTING ISSUES

public List<Edicao> listarEdicao(){
        conn = Conexao.getConexao();

        sql = "SELECT edic_isbn, edic_valor, edic_qtde, edic_numpag, edic_ano, edic_livro, edic_editora\n" +
                      "FROM public.edicao";

        List<Edicao> listaEdicao = new ArrayList<Edicao>();

        Edicao edicao = null;
        livro = null;
                editora = null;

        try {

            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();

            while(rs.next()){

                            edicao = new Edicao();

                            edicao.setIsbn(rs.getString("edic_isbn"));
                            edicao.setValor(rs.getDouble("edic_valor"));
                            edicao.setQtde(rs.getInt("edic_qtde"));
                            edicao.setNumPag(rs.getInt("edic_numpag"));
                            edicao.setAno(rs.getInt("edic_ano"));

                            livro = livroControl.consultar(rs.getInt("edic_livro"));
                            editora = editoraControl.consultar(rs.getString("edic_editora"));

                            edicao.setLivro(livro);
                            edicao.setEditora(editora);
                            listaEdicao.add(edicao);

                            conn.close();  // FECHA A CONEXÃO DO BANCO DE DADOS
            }
        } catch (Exception e) {
            // TODO: handle exception
                        e.printStackTrace();
            System.out.println("Erro na Listar Edição !!"+e.getMessage());
        }
        return listaEdicao;


    }

WHAT WAS CAUSING THE ERROR IN THE TABLE, MORE SPECIFICALLY IN THE METHOD OF LISTING EDITIONS IN THE DAO EDITING CLASS WAS BECAUSE I WAS OPENING SEVERAL SESSIONS WITH A DATABASE INCLUDING THE RESULTSET, AS A SOLUTION I SIMPLY CLOSED THE CONNECTIONS AND THE RESULTSET.

THIS IS MY CONNECTION CLASS:

public class Conexao {
    public static Connection getConexao(){
            Connection con = null;

            String url = "jdbc:postgresql://localhost/Livraria_bk";
            String user = "postgres";
            String senha = "12345";

            try {
                    Class.forName("org.postgresql.Driver");
                    con = DriverManager.getConnection(url, user, senha);
                   // System.out.println("Conexão ok  !");
                 // JOptionPane.showMessageDialog(null, "Conexão Bem - Sucedida !!! ");

            } catch (Exception e) {
                    System.out.println("Erro na conexao !" +e.getMessage());
            }
            return con;
    }

    public static void main(String[] args) {
          Conexao.getConexao();     

    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.