the index 2 out of range

0

Hi, I'm trying to run a software but when I try to execute it, I get a console error: index 2 out of range.

my code is as follows:

public void seleccionanombre() {

    ObservableList <Persona> busqueda =FXCollections.observableArrayList();
     String consulta="select * from cliente where nombre like ?" ;
     String nombre = bq.getText();
     Connection conn=null;{

          try {

              conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=prueba", "sa", "milkas87");
              PreparedStatement ps =conn.prepareStatement(consulta);
              ps.setString(1, nombre);
              ResultSet rs =ps.executeQuery();

             while ( rs.next() ) 
              {
                 busqueda.add(new Persona(
                         (rs.getString("nombre")),
                         (rs.getString("apellido")),
                         (rs.getInt("id"))
                         ));

              }

          } catch (SQLException e) {
              e.printStackTrace();
          }

          tablacliente.setItems(busqueda); 
     }

}

public void seleccionapellido() {
    ObservableList <Persona> busquedape =FXCollections.observableArrayList();
     String consulta2="select * from cliente where apellido like ? " ;
     String apellido = bq.getText();
     Connection conn=null;{
          try {

              conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=prueba", "sa", "milkas87");
              PreparedStatement ps2 =conn.prepareStatement(consulta2);
              ps2.setString(2, apellido);
              ResultSet rs =ps2.executeQuery();

             while ( rs.next() ) 
              {
                 busquedape.add(new Persona(
                         rs.getString("nombre"),
                         rs.getString("apellido"),
                         rs.getInt("id")
                         ));

              }

          } catch (SQLException e) {
              e.printStackTrace();
          }

          tablacliente.setItems(busquedape); 
     }

}
    
asked by Rafael 13.10.2018 в 22:11
source

1 answer

1

The problem is that you only have one symbol, in the code?

String consulta2="select * from cliente where apellido like ?";

then you should assign parameter one:

ps2.setString(1, apellido);
    
answered by 13.10.2018 в 22:43