Error returning response with search method

1

I'm having trouble trying to do a search on the bank and returning the response on the screen.

This is my main class:

public static void main(String[] args)  {
    pessoaDAO ps = new pessoaDAO();
    try {
        Pessoa pessoa = ps.pesquisarcpf("02317825277");
        if(pessoa == null){
            System.out.println("CPF não encontrado");
        }else{

            System.out.println("CPF: "+pessoa.getCpf());
            System.out.println("Nome: "+pessoa.getNome());
            System.out.println("Bairro: "+pessoa.getBairro().getNome());
        }

    } catch (SQLException ex) {
        Logger.getLogger(ProjetoIntermediário2.class.getName()).log(Level.SEVERE, null, ex);
    }
}

I've done a method to search using the person's CPF. >:

public Pessoa pesquisarcpf(String cpf) throws SQLException{
    connection = FabricaConexao.pegarConexao();
    sql = "select * from pessoa inner join bairro where pesbaicep = baicep";
    Pessoa pessoa = null;
    preparedstatement = connection.prepareStatement(sql);
    preparedstatement.setString(1, cpf);
    resultset = preparedstatement.executeQuery();
     if(resultset.next()){
         pessoa = new Pessoa();
         pessoa.setCpf(resultset.getString("pescpf"));
         pessoa.setNome(resultset.getString("pesnome"));
         Bairro bairro = new Bairro();
         bairro.setNome(resultset.getString("bainome"));
         pessoa.setBairro(bairro);
     }
     return pessoa;
}

When I ask to compile the code it returns the following errors: >

  

Successful connection Mar 29, 2017 10:24:24 AM   projectintermediary2.ProjectIntermediary2 main GRAVE: null   java.sql.SQLException: Parameter index out of range (1 > number of   parameters, which is 0). at   com.mysql.jdbc.SQLError.createSQLException (SQLError.java:957) at   com.mysql.jdbc.SQLError.createSQLException (SQLError.java:896) at   com.mysql.jdbc.SQLError.createSQLException (SQLError.java:885) at   com.mysql.jdbc.SQLError.createSQLException (SQLError.java:860) at   com.mysql.jdbc.PreparedStatement.checkBounds (PreparedStatement.java:3319)     at   com.mysql.jdbc.PreparedStatement.setInternal (PreparedStatement.java:3304)     at   com.mysql.jdbc.PreparedStatement.setString (PreparedStatement.java:4016)     at modelo.DAO.People.Acquisitecpf (personDAO.java:55) at   projectintermediary2.ProjectIntermediary2.main (ProjectIntermediary2.java: 17)

    
asked by anonymous 29.03.2017 / 16:41

2 answers

2

Your sql variable is wrong. Change% by% by% by%

sql = "select * from pessoa inner join bairro where pesbaicep = ?";
    
29.03.2017 / 16:46
0

The answer is to fix the SQL code, getting

sql = "select * from pessoa inner join bairro on pesbaicep = baicep where pescpf = ?";
    
29.03.2017 / 16:54