0
I have the following problem, at first run my Defaulttablemodel loads normally as can be seen below in red:
After registering, changing or deleting once, the table updates normally, but when I perform these operations again the following error occurs:
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 hiscatch
and see on the console what is the exception being released. Then edit your question and add the stacktrace here.– Felipe Marinho
Please provide a [mcve] so that we can execute the code and simulate your problem.
– user28595
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.
– moises.santos
If you found the solution, post as answer.
– user28595