Seguinte to criando uma agenda para fins de aprendizado.
Só esta acontecendo um erro, quando tento atualizar os dados que estão listados na minha JTable.
o Processo do programa é o seguinte:
1 -> inserir os dados
2 -> faz busca no banco
3 -> adiciona os dados na JTable
Só que nesse terceiro passo ele provoca uma Exception.
O código é:
package acoes;
import java.sql.ResultSetMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import db.ConnectionDB;
import db.Constantes;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.util.Formatter;
public class GerenciaContato {
private String nome = null;
private String email = null;
private String endereco = null;
private String telefone = null;
private String celular = null;
private ConnectionDB connectionDB;
private ResultSetMetaData dados;
private JTable tabela;
private JLabel textoResposta;
private ResultSet resultado;
// <editor-fold defaultstate="collapsed" desc="Método set's e get's">
public void setNome(String nome) {
this.nome = nome;
}
public void setEmail(String email) {
this.email = email;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
public void setCelular(String celular) {
this.celular = celular;
}
public void setTabela(JTable tabela) {
this.tabela = tabela;
}
public String getNome() {
return nome;
}
public String getEmail() {
return email;
}
public String getEndereco() {
return endereco;
}
public String getTelefone() {
return telefone;
}
public String getCelular() {
return celular;
}
private JTable getTabela() {
return tabela;
}
// </editor-fold>
public GerenciaContato(JTable tabela, JLabel textoResposta) {
this.textoResposta = textoResposta;
setTabela(tabela);
busca();
}
private void limpaBuffer() {
nome = null;
email = null;
endereco = null;
telefone = null;
celular = null;
connectionDB = null;
dados = null;
tabela = null;
resultado = null;
}
public void inseri() {
connectionDB = new ConnectionDB();
String sql = "INSERT INTO contatos " +
"(Nome, Email, Endereco, Telefone, Celular) VALUES " +
"('"+ nome +"', '"+ email +"', '"+ endereco +"', '"+ telefone +"', '"+ celular +"')";
connectionDB.update(sql);
limpaBuffer();
busca();
}
public void busca() {
boolean where = false;
String sql = "SELECT * FROM contatos ";
if(nome != null) {
sql += "WHERE Nome LIKE '%"+ nome +"%'";
where = true;
}
if(email != null) {
if(where)
sql += "WHERE ";
else
sql += " AND ";
sql += " Email LIKE '%"+ email +"%'";
where = true;
}
if(endereco != null) {
if(where)
sql += "WHERE ";
else
sql += " AND ";
sql += " Endereco LIKE '%"+ email +"%'";
where = true;
}
if(telefone != null) {
if(where)
sql += "WHERE ";
else
sql += " AND ";
sql += " Telefone LIKE '%"+ email +"%'";
where = true;
}
if(celular != null) {
if(where)
sql += "WHERE ";
else
sql += " AND ";
sql += " Celular LIKE '%"+ email +"%'";
where = true;
}
try {
connectionDB = new ConnectionDB();
connectionDB.query(sql);
dados = connectionDB.getDados();
resultado = connectionDB.getResultados();
} catch (Exception exception) {
JOptionPane.showMessageDialog(null, Constantes.ERRO_PADRAO,
"Erro", JOptionPane.ERROR_MESSAGE);
}
showTable();
} // fim do método de busca
// método que exibe os valores da pesquisa na JTable
private void showTable() {
try {
Object[][] objDados = new Object[connectionDB.getRow()][6];
int i = 0;
int numeroColunas = dados.getColumnCount();
if(numeroColunas != 0) {
while(resultado.next()) {
for(int count=0;count<numeroColunas;i++)
objDados[i][count] = resultado.getObject(count + 1);
}
}
textoResposta.setText("Foram encontrados ("+ connectionDB.getRow() +") resultados:");
String[] campos = {"ID","Nome","Email","Endereco","Telefone","Celular"};
DefaultTableModel modeloPadrao = new DefaultTableModel(objDados, campos);
tabela.setModel(modeloPadrao);
} catch (SQLException exception) {
JOptionPane.showMessageDialog(null, Constantes.ERRO_PADRAO +"\nError: "+ exception.getMessage(),
"Erro", JOptionPane.ERROR_MESSAGE);
} finally {
connectionDB.closingConnection();
}
limpaBuffer();
} // fim do método showTable
}