JTable + Persistence

3 respostas
helise

Quero criar uma tabela com os dados vindos do banco, mas ta difícil, não to conseguindo recuperar os dados.

Criei o seguinte esquema:

Aluno
AlunoTableModel
AlunoDAO
ListaAlunoTela

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.model;


import java.util.List;
import javax.swing.table.AbstractTableModel;
import br.persistencia.AlunoDAO;


@SuppressWarnings("serial")
public class AlunoTableModel extends AbstractTableModel{

    
    private List<Aluno> alunos;
    
    public AlunoTableModel (AlunoDAO dao) throws Exception{
    super();
     this.alunos=dao.todosAlunos();
             
    }



    @Override
    public String getColumnName(int col){
        switch (col){
            case 0: return "Matrícula";
            case 1: return "Nome";
            case 2: return "Endereço";
            case 3: return "Telefone";
            case 4: return "Email";
            default: return null;
        }
    }
    @Override
    public int getRowCount() {
        return alunos != null ? alunos.size() :0;
        }
    @Override
    public int getColumnCount() {
        return 5;
    }
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (alunos != null && rowIndex >=0 && rowIndex < alunos.size()) {
            Aluno aluno = alunos.get(rowIndex);
            switch (columnIndex){
                case 0: return aluno.getMatricula();
                case 1: return aluno.getNome();
                case 2: return aluno.getEndereco();
                case 3: return aluno.getTelefone();
                case 4: return aluno.getEmail();
            }
        }
        return null;
    }
}
public class TelaListaAlunos extends javax.swing.JInternalFrame {

public TelaListaAlunos() throws Exception {
        initComponents();
(..)

tabela.setModel(new AlunoTableModel(new AlunoDAO())); 
            jScrollPane1.setViewportView(tabela);

            jButton1.setText("Fechar");

            jButton2.setText("Atualizar");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });


(..)
}

isto näo está dando certo

3 Respostas

W

Voce esta usando hibernate ou jdbc, sua sessao esta aberta ?

helise

jdbc

está aberta

G

Mas qual o problema que acontece?
Dá algum erro ou só não retorna o resultado esperado?

Criado 8 de setembro de 2013
Ultima resposta 9 de set. de 2013
Respostas 3
Participantes 3