Error: Parameter index out of range (1 > number of Parameters, which is 0)

Asked

Viewed 587 times

1

I am trying to remove a data from the bank, but the following error happens:

Parameter index out of range (1 > number of Parameters, which is 0)

Can you help me?

public void remove(Bean e) {
     try {

         PreparedStatement stmt = ConexaoMysql.getConexao()
                 .prepareStatement("DELETE FROM coluna WHERE id = 5");


         stmt.setInt(1,e.getId());
         stmt.execute();
         stmt.close();
         System.out.println("Removido!");
     } catch (SQLException e) {
         throw new RuntimeException(e);
     }
 }
public class TesteRemover {

    public static void main(String[] args) {

        Bean p = new Bean ();  

         Dao dao = new Dao (); 

          p.setIdexemplo(5); 

          dao.remove(p);  


    }

}

1 answer

3


You must use "?" to indicate the parameters to be assigned in the Preparedstatement, in the case you explained the literal value "5" and is trying to bind, so the "Parameter index out of range".

PreparedStatement stmt = ConexaoMysql.getConexao()
                 .prepareStatement("DELETE FROM coluna WHERE id = ?");
  • But still my die is not removed

  • @Joãosouza try to force the id value (integer) to be removed from preparedStatement for example stmt.setInt(1,5); It seems to me that your e.getId(); does not refer to the same attribute of p.setIdexemplo(5); but to be sure of this you would need to add the source of the Bean class;

  • I realized that Voce changed the initial SQL of the question I will change the answer to maintain coherence.

  • "Forcing" the dice is removed but I wanted to remove by my main.

  • try to replace stmt.setInt(1,e. getId()); for something like stmt.setInt(1,e. getIdexemplo());

  • Nothingness............

  • add the source of your question Bean Class

  • private int id; private String name; private float number; private Calendar data;

  • This is my bean

  • Joao Oce needs to add the font to the question completely so I can analyze and help you, what is probably happening is that you are not setting the same attribute that you are giving get.

  • the problem was in the bean, already solved thank you :)

Show 6 more comments

Browser other questions tagged

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