Pessoal, estou tentando mostrar em uma Jtable todos os Empréstimos realizados a partir de um CPF, porém, na tabela só aparece o ultimo Empréstimo, e não a lista completa como era pra ser. Alguém poderia me ajudar ?
[code]public class PessoaDAO {
…
public List pesquisarAberto(Pessoa pessoa){
List<Emprestimo> emp = new ArrayList<Emprestimo>();
Emprestimo empres = new Emprestimo();
Produto pro = new Produto();
List<Integer> id = new ArrayList<Integer>();
try{
String pegarId = "select all(id) from emprestimo where cpf_pessoa = ?";
conexao = getConnection();
conexao.setAutoCommit(false);
ps = conexao.prepareStatement(pegarId);
ps.setString(1, pessoa.getCpf());
visualizar = ps.executeQuery();
while (visualizar.next()) {
id.add(visualizar.getInt(1));
}
String pesTdPes = "select emprestimo.id, emprestimo.statu, produto.codigo, produto.titulo, pessoa.cpf, pessoa.nome, emprestimo.data_saida, emprestimo.data_entrega "
+ "from ((pessoa inner join emprestimo on pessoa.cpf = emprestimo.cpf_pessoa) "
+ "inner join emp_de_pro on emprestimo.id = emp_de_pro.id_emprestimo) "
+ "inner join produto on produto.codigo = emp_de_pro.cod_produto where emprestimo.id = ?";
PreparedStatement pesTodos = conexao.prepareStatement(pesTdPes);
for (Integer ids : id) {
pesTodos.setInt(1, ids);
}
visualizar = pesTodos.executeQuery();
while(visualizar.next()){
empres.setId(visualizar.getInt(1));
empres.setStatus(visualizar.getString(2));
pro.setCodigo(visualizar.getString(3));
pro.setTitulo(visualizar.getString(4));
pessoa.setCpf(visualizar.getString(5));
pessoa.setNome(visualizar.getString(6));
empres.setDataSaida(visualizar.getDate(7));
empres.setDataEntrega(visualizar.getDate(8));
empres.setPessoa(pessoa);
empres.setPro(pro);
emp.add(empres);
}
conexao.commit();
}catch(SQLException e){
e.printStackTrace();
}finally{
finalizarConexao();
}
return emp;
}
public void finalizarConexao(){
try{
if(ps != null){
ps.close();
}
if(conexao != null){
conexao.close();
}
}catch(SQLException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}[/code]
Esse é o meu Jtable.
[code]table = new JTable();
table.setFont(new Font(“Arial”, Font.PLAIN, 12));
table.setEnabled(false);
scrollPane.setViewportView(table);
table.setModel(new DefaultTableModel(
new Object[][] {
},
new String [] {
"ID", "STATUS", "CODIGO" ,"LIVRO" ,"CPF", "NOME", "DATA DE SAÍDA", "DATA DE ENTREGA"
}
));
format = new DefaultTableCellRenderer();
format.setBackground(Color.CYAN);
for (int i = 0; i < table.getModel().getColumnCount(); i++){
table.getColumnModel().getColumn(i).setHeaderRenderer(format);
}
}
private void btnPesquisarActionPerformed(ActionEvent evt) {
modelo = (DefaultTableModel) table.getModel();
while(modelo.getRowCount() > 0) {
modelo.removeRow(0);
}
pessoa.setCpf(tfCpfPesquisa.getText());
List<Emprestimo> emp = pessoaDAO.pesquisarAberto(pessoa);
for (Emprestimo emprestimo : emp) {
if (emprestimo.getDataSaida() == null && emprestimo.getDataEntrega() == null) {
Object [] linha = {
emprestimo.getId(), emprestimo.getStatus(), emprestimo.getPro().getCodigo(), emprestimo.getPro().getTitulo(), emprestimo.getPessoa().getCpf(), emprestimo.getPessoa().getNome(),
" "," "
};
modelo.addRow(linha);
}else if (emprestimo.getDataEntrega() == null){
Object [] linha = {
emprestimo.getId(), emprestimo.getStatus(), emprestimo.getPro().getCodigo(), emprestimo.getPro().getTitulo(), emprestimo.getPessoa().getCpf(), emprestimo.getPessoa().getNome(),
dataParaString(emprestimo.getDataSaida()), " "
};
modelo.addRow(linha);
}else{
Object [] linha = {
emprestimo.getId(), emprestimo.getStatus(), emprestimo.getPro().getCodigo(), emprestimo.getPro().getTitulo(), emprestimo.getPessoa().getCpf(), emprestimo.getPessoa().getNome(),
dataParaString(emprestimo.getDataSaida()), dataParaString(emprestimo.getDataEntrega())
};
modelo.addRow(linha);
}
}
tfCpfPesquisa.setText("");
}
private static String dataParaString(Date data) {
DateFormat formatador = new SimpleDateFormat("dd/MM/yyyy");
return formatador.format(data);
}
}
[/code]