Erro Besta (Nenhum resultado foi retornado pela consultar)

5 respostas
ArkanSephin

To fazendo para faculdade um trabalho cadastro de clientes.
ele ta cadastrando normal so que assim que ele bota no banco de dados as informação ele entra no catch e da essa msg "Nenhum resultado foi retornado pela consultar" quando vejo no banco esta cadastrado, não sei oque é =/ rodei no PostgreSQL a instrução do SQL e rodo.

Classe Repositorio
public void cadastra(Cliente cliente) throws Exception {
        try {
            //codigo para execução de uma instrução sql
            Statement stmt = conectar();

            String sql = "";

            if (!cliente.getComplemento().equals("")) {
                sql = "insert into cliente(nome,cpf,telefone,rua,bairro,cidade,estado,numeroRes,complemento)"
                        + "values('" + cliente.getNome() + "','" + cliente.getCpf() + "','" + cliente.getTelefone() + "','" + cliente.getRua() + "','" + cliente.getBairro() + "','" + cliente.getCidade() + "','" + cliente.getEstado() + "'," + cliente.getNumeroRes() + ",'" + cliente.getComplemento() + "')";
            }

            if (cliente.getComplemento().equals("")) {
                sql = "insert into cliente(nome,cpf,telefone,rua,bairro,cidade,estado,numeroRes)"
                        + "values('" + cliente.getNome() + "','" + cliente.getCpf() + "','" + cliente.getTelefone() + "','" + cliente.getRua() + "','" + cliente.getBairro() + "','" + cliente.getCidade() + "','" + cliente.getEstado() + "'," + cliente.getNumeroRes() + ")";
            }

            //executando a instrucao sql
            stmt.executeQuery(sql);
            //fechando a conexao com o banco
            desconectar();

        } catch (SQLException e) {
            throw new Exception(e.getMessage());

        }
    }

Classe Verificar(Validações)

public class VerificarClientes implements InterfaceCliente {

    private RepositorioCliente repCliente = new RepositorioCliente();

    

    @Override
    public void cadastra(Cliente cliente) throws Exception {
        

        if (cliente == null) {
            throw new Exception("O refereido cliente não está instanciado");
        }
        //verifica Nome

        if (cliente.getNome().trim().equals("")) {
            throw new Exception("Informar o Nome do Cliente");
        }

        if (cliente.getNome().equals("")) {
            throw new Exception("Informar o Nome do Cliente");
        }


        if (cliente.getNome() == null) {
            throw new Exception("Informar o Nome do Cliente");
        }


        //verifica CPF



        if (cliente.getCpf() == null) {
            throw new Exception("Informar o CPF do Cliente");
        }


        if (cliente.getCpf().trim().equals("")) {
            throw new Exception("Informar o CPF do Cliente");

        }




        if (cliente.getCpf().length() < 11) {
            throw new Exception("Informar o CPF do Cliente com 11 digitos");

        }

        if (cliente.getCpf().length() > 11) {



            throw new Exception("Informar o CPF do Cliente com 11 digitos");
        }

        for (int i = 0; i < cliente.getCpf().length(); i++) {
            if (!Character.isDigit(cliente.getCpf().charAt(i))) {
                throw new Exception("O CPF so contem numeros");
            }

        }




        //verifica telefone

        if (cliente.getTelefone().trim().equals("")) {
            throw new Exception("Informar o Telefone do Cliente");
        }

        if (cliente.getTelefone().isEmpty()) {
            throw new Exception("Informar o Telefone do Cliente");
        }

        if (cliente.getTelefone().isEmpty()) {
            throw new Exception("Informar o Telefone do Cliente");
        }

        for (int i = 0; i < cliente.getTelefone().length(); i++) {
            if (!Character.isDigit(cliente.getTelefone().charAt(i))) {
                throw new Exception("O Telefone so contem numeros");
            }

        }




//verificar rua
        if (cliente.getRua() == null) {
            throw new Exception("Informar a Rua do Cliente");

        }
        if (cliente.getRua().trim().equals("")) {
            throw new Exception("Informar a Rua do Cliente");
        }





        //verifica Bairro

        if (cliente.getBairro() == null) {
            throw new Exception("Informar o Bairro do Cliente");

        }
        if (cliente.getBairro().trim().equals("")) {
            throw new Exception("Informar o Bairro do Cliente");
        }

        //verifica Cidade

        if (cliente.getCidade() == null) {
            throw new Exception("Informar a Cidade do Cliente");
        }
        if (cliente.getCidade().trim().equals("")) {
            throw new Exception("Informar a Cidade do Cliente");
        }

        //verifica Estado
        if (cliente.getEstado() == null) {
            throw new Exception("Informar o Estado do Cliente");
        }
        if (cliente.getEstado().trim().equals("")) {
            throw new Exception("Informar o Estado do Cliente");
        }

        //verifica Numero Residencia

        if (cliente.getNumeroRes() <= 0) {
            throw new Exception("Informar o Numero da Residencia do Cliente");
        }


        for (int i = 0; i < cliente.getTelefone().length(); i++) {
            if (!Character.isDigit(cliente.getTelefone().charAt(i))) {
                throw new Exception("O Telefone so contem numeros");
            }

        }



        this.repCliente.cadastra(cliente);
    }

Classe Fachada

public class Fachada {
    private VerificarClientes verificarClientes;
     private static Fachada instancia;

    public static Fachada obterInstancia() {
        if (instancia == null) {
            instancia = new Fachada();
        }
        return instancia;
    }
    
     private Fachada() {
        this.verificarClientes = new VerificarClientes();
    }
    
    public void CadastraCliente (Cliente cliente)throws Exception{
       this.verificarClientes.cadastra(cliente);
    }
}
GUI: O Botão Cadastrar
private void bt_cadastraActionPerformed(java.awt.event.ActionEvent evt) {  
try {
        Cliente cadCliente = new Cliente();

        // puxando dados
        cadCliente.setNome(tf_nome.getText().trim());
        cadCliente.setCpf(tf_cpf.getText().trim());
        cadCliente.setTelefone(tf_telefone.getText().trim());
        cadCliente.setRua(tf_rua.getText().trim());
        cadCliente.setComplemento(tf_complemento.getText().trim());
        cadCliente.setBairro(tf_bairro.getText().trim());
        cadCliente.setCidade(tf_cidade.getText().trim());
        cadCliente.setEstado(tf_estado.getText().trim());
        try{
        if (!tf_numerores.getText().trim().equals("")) {
            cadCliente.setNumeroRes(Integer.parseInt(tf_numerores.getText().trim()));
        }
        }catch(Exception e){
            throw new Exception("Para o número residêncial, Informar apenas valores numéricos");
        }


        
        Fachada.obterInstancia().CadastraCliente(cadCliente);
//Tudo certo?
// NÃO APARECE A MSG A SEGUIR        
JOptionPane.showMessageDialog(null, "Cliente Cadastrado com Sucesso");
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage());
    }
}

5 Respostas

Lucas_Abbatepaolo

Amigo,

para executar o seu insert vc esta usando um executeQuery(). Isto é utilizado para realizar consultas, e como vc realizou um insert ele te informa q não retornou nenhum registro.

vc deve usar um executeUpdate() para fazer insert ou update.

E como conselho de uma estudada em PreparedStatement.

flw…

Anime

Imprima a stacktrace do erro, coloque o codigo abaixo no seu catch…

catch(SQLException erro) {
    JOptionPane.showMessageDialog(null,"Erro,ao listar");
    erro.printStackTrace(); 
}
Anime

Nossa, desculpe… :oops:

Anime

:oops:

M

complementando um pouco a pedida para voce estudar, eu usei uma apostila da caelum (nao lembro o nome)
com JDBC que falava sobre PreparedStatement, da uma olhada la, me ajudou bastante (:

Criado 10 de novembro de 2011
Ultima resposta 10 de nov. de 2011
Respostas 5
Participantes 4