Bom dia,
Consegui resolver o problema para popular minha JTable com dados do banco, ficou da seguinte forma:
public class ClienteTabelaModelo extends AbstractTableModel {
private List<Cliente> clientes;
private List<String> colunas;
private ClienteDAO dao;
// Construtores
public ClienteTabelaModelo() {
}
public ClienteTabelaModelo(ClienteDAO dao) throws SQLException {
this.dao = dao;
this.clientes = dao.retornaTodas();
colunas = Arrays.asList("CPF", "Nome", "Telefone");
}
// Métodos Geters
public List<String> getColunas() {
return colunas;
}
// Retorna o numero de colunas no modelo
public int getColumnCount() {
return colunas.size();
}
// Retorna o numero de linhas existentes no modelo
public int getRowCount() {
return clientes.size();
}
public String getColumnName(int i) {
return colunas.get(i);
}
// Obtem o valor na linha e coluna
public Object getValueAt(int r, int c) {
Cliente cliente = clientes.get(r);
switch (c) {
case 0:
return cliente.getCpf();
case 1:
return cliente.getNome();
case 2:
return cliente.getTelefone();
}
return null;
}
//MÉTODOS PARA EDITAR TABELA
/*public void setValueAt(Object aValue, int r, int c) {
Cliente cliente = clientes.get(r);
switch (c) {
case 0:
cliente.setCpf((String) aValue);
case 1:
cliente.setNome((String) aValue);
default:
throw new IndexOutOfBoundsException("columnIndex out of bounds");
}
}
@Override
public boolean isCellEditable(int r, int c){
return true;
}*/
}
public class ClienteDAO {
private Connection conexao;
public ClienteDAO() {
this.conexao = new ConnectionFactory().getConnection();
}
public List<Cliente> retornaTodas() throws SQLException {
List<Cliente> clientes = new ArrayList<Cliente>();
String sql = "select * from cliente";
try {
PreparedStatement stmt = conexao.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Cliente cliente = new Cliente();
cliente.setNome(rs.getString("nome"));
cliente.setCpf(rs.getString("cpf"));
cliente.setEndereco(rs.getString("endereco"));
cliente.setNumero(rs.getString("numero"));
cliente.setBairro(rs.getString("bairro"));
cliente.setTelefone(rs.getString("telefone"));
cliente.setEmail(rs.getString("email"));
clientes.add(cliente);
}
rs.close();
stmt.close();
} catch (Error e) {
throw new RuntimeErrorException(e);
}
return clientes;
}
public void insereBanco(Cliente pessoa) {
String sql = "insert into cliente (nome, cpf, bairro, email, endereco, numero, telefone) values(?,?,?,?,?,?,?)";
java.sql.PreparedStatement stmt;
try {
stmt = conexao.prepareStatement(sql);
stmt.setString(1, pessoa.getNome());
stmt.setString(2, pessoa.getCpf());
stmt.setString(3, pessoa.getBairro());
stmt.setString(4, pessoa.getEmail());
stmt.setString(5, pessoa.getEndereco());
stmt.setString(6, pessoa.getNumero());
stmt.setString(7, pessoa.getTelefone());
stmt.execute();
conexao.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void atualiza(Cliente cliente){
String sql = "update cliente set nome=?, " +
"bairro=?, email=?, endereco=?, numero=?, telefone=? where cpf=?";
try {
PreparedStatement stmt = conexao.prepareStatement(sql);
stmt.setString(1, cliente.getNome());
stmt.setString(2, cliente.getBairro());
stmt.setString(3, cliente.getEmail());
stmt.setString(4, cliente.getEndereco());
stmt.setString(5, cliente.getNumero());
stmt.setString(6, cliente.getTelefone());
stmt.setString(7, cliente.getCpf());
//stmt.setInt(8, cliente.getId());
stmt.execute();
stmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void excluirCliente(Cliente c){
String sql = "delete from cliente where cpf=?";
try {
PreparedStatement stmt = conexao.prepareStatement(sql);
stmt.setString(1, c.getCpf());
stmt.execute();
stmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
E fiz um método para montar minha tabela, funcionou beleza....
public void atualizaTabela() {
ClienteDAO dao = new ClienteDAO();
ClienteTabelaModelo tm = null;
try {
tm = new ClienteTabelaModelo(dao);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JTable t = new JTable(tm);
scroll = new JScrollPane(t);
scroll.setBounds(70, 305, 470, 100);
scroll.setVisible(true);
this.add(scroll);
}
Agora me apareceu outro problema, por exemplo se adiciono o registro na tabela chamo o Método atualizarTabela, mas o scroll não funciona correramente... se alguém souber como posso fazer me de uma ajuda.
Desde já agradeço....