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

1

I'm trying to remove a data from the database, but the following error occurs:

  

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);  


    }

}
    
asked by anonymous 07.07.2015 / 04:39

1 answer

3

You should use "?" to indicate the parameters that will be assigned in PreparedStatement , in case you have explicitly declared the " 5 " literal value and are attempting to bind, so the "Parameter index out of range" occurs.

PreparedStatement stmt = ConexaoMysql.getConexao()
                 .prepareStatement("DELETE FROM coluna WHERE id = ?");
    
07.07.2015 / 04:57