Error returning reply with search method

Asked

Viewed 56 times

1

I’m having trouble trying to do a search in the bank and return the answer 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);
    }
}

Then I made a method to search using the person’s number. >:

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 projectOwners2.Projectertermediar2 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.personDAO.pesquisarcpf(personDAO.java:55) at projectworksedics2.Projectworksediary2.main(Projectworksediary2.java:17)

2 answers

2

Your sql variable is wrong. Switch = baicep for = ?

sql = "select * from pessoa inner join bairro where pesbaicep = ?";
  • My friend, actually I was much more wrong, to envés de pesbaicep, is pescpf that is where he will carry out the research, I switched here and it worked as expected. Very grateful.

0

The answer lies in correcting the SQL code, getting

sql = "select * from pessoa inner join bairro on pesbaicep = baicep where pescpf = ?";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.