Olá estou tentando efetuar uma busca no MYSQL, mas não está saindo como o esperado…
A primeira vez que eu faço a consulta ela funciona normal, mas quando tento fazer outra consulta sem fechar o sistema ele da um evento de NULLEXCEPTION
aqui esta a classe de conexao:
package Refeitorio.DAO;
import java.sql.*;
import javax.swing.*;
public class Conexao{
final private String driver = "com.mysql.jdbc.Driver";
final private String url = "jdbc:mysql://localhost:3306/Refeitorio";
final private String usuario = "root";
final private String senha = "123";
private Connection conn;
public Statement statement;
public ResultSet resultset;
public boolean conecta()
{
boolean result = true;
try
{
Class.forName(driver);
conn = DriverManager.getConnection(url, usuario, senha);
// JOptionPane.showMessageDialog(null, "conectou");
}
catch(ClassNotFoundException Driver)
{
JOptionPane.showMessageDialog(null,"Driver nao localizado: "+Driver);
result = false;
}
catch(SQLException Fonte)
{
//JOptionPane.showMessageDialog(null,"Deu erro na conexão com a fonte de dados: "+Fonte);
result = false;
}
return result;
}
public Connection getConexao() {
return conn;
}
public void desconecta()
{
boolean result = true;
try
{
conn.close();
//JOptionPane.showMessageDialog(null,"banco de dados fechado");
}
catch(SQLException fecha)
{
result = false;
}
}
public void executeSQL(String sql)
{
try
{
statement = conn.createStatement();
resultset = statement.executeQuery(sql);
}
catch(SQLException sqlex)
{
JOptionPane.showMessageDialog(null,"Não foi possível executar o comando sql,"+sqlex+ ", o sql passado foi"+sql);
}
}
}
O método de busca
package Refeitorio.DAO;
/**
*
* @author Isabel
*/
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import Refeitorio.VO.ClienteVO;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.JTable;
public class ClienteDAO {
public Statement statement;
public CreditoDAO creditoDAO;
public ResultSet resultset;
public Conexao con;
boolean result;
public ClienteDAO() throws PersistenciaException {
this.con = new Conexao();
con.conecta();
}
public void desconectarBD() throws PersistenciaException {
con.desconecta();
}
// ......Outros metodos.........
public void preenche_jtable(JTable MostraTabela, String nome) throws PersistenciaException
{
ArrayList lista = new ArrayList();
ClienteVO cli = null;
String comandoSQL = "SELECT idCliente, nCartao, nome, telefone,celular, email FROM Cliente WHERE nome LIKE '" + nome.trim() + "%' ORDER BY nome";
try {
PreparedStatement comando = con.getConexao().prepareStatement(comandoSQL);// na segunda tentativa de busca dá erro aqui
ResultSet rs = comando.executeQuery();
while (rs.next()) {
cli = new ClienteVO();
//.set(rs.get(""));
cli.setId(rs.getInt("idCliente"));
cli.setnCartao(rs.getString("nCartao"));
cli.setNome(rs.getString("nome"));
cli.setTelefone(rs.getString("telefone"));
cli.setCelular(rs.getString("celular"));
cli.setEmail(rs.getString("email"));
lista.add(cli);
// desconectarBD();
}
comando.close();
ClienteTableModel modelo = new ClienteTableModel(lista);
MostraTabela.setModel(modelo);
MostraTabela.getColumnModel().getColumn(0).setPreferredWidth(5);
MostraTabela.getColumnModel().getColumn(1).setPreferredWidth(50);
MostraTabela.getColumnModel().getColumn(2).setPreferredWidth(175);
MostraTabela.getColumnModel().getColumn(3).setPreferredWidth(50);
MostraTabela.getColumnModel().getColumn(4).setPreferredWidth(50);
MostraTabela.getColumnModel().getColumn(5).setPreferredWidth(85);
} catch (Exception ex) {
throw new PersistenciaException(ex,"Erro na seleção por Nome");
}
}
}
///..............
Pelos testes que eu fiz aqui a conexao nao está nula, mas parece que tá abrindo conexao d+…
Quem puder ajudar, agradeço,
Isabel.