Index out of range when performing an update

0

When executing the statement:

ps.setString(i ,text[i].getText());  

I get the following error:

  

java.sql.SQLException Parameter index out of range (1 > number of   parameters, which is 0);

This is the code:

void Guardar (JTable table , String tablebd , String [] sentencia , String clave , JTextField[] text , int numero ){
   //Jtable table = el nombre de la tabla en el frame
   //table bd = nombre de la tabla en la bd
   // String [] Sentancia = Será el query , van los nombres de las columnas segruidos con un =?, , excepto el ultimo que no tendra ,
        // Ej : {"nombre=? , apellido=? , telefono=? , direccion=? , sexo=? , email=?"}
   //clave = id de la tabla ; IDinvitado , IDevento , etc
   // TextField [] = lista de los txtfiend que hay en el frame , deben ser nombrados en el orden correspondiente a su lugar en el String [] sentencia
   // Numero = Cantidad de columnas en la tabla , que debe ser la misma cantidad de TExtField
   try{
       Concectar cc = new Concectar();
        Connection cn = cc.conexion();
        int fila = table.getSelectedRow();
        String sql = "UPDATE "+ tablebd + " SET " + sentencia.toString() +" WHERE "+ clave + " = " +table.getValueAt(fila,0);

       String dao =  (String) table.getValueAt(fila, 0);
       PreparedStatement ps = cn.prepareStatement(sql);

       for (int i = 1 ; i < numero ; i++){
       ps.setString(i ,text[i].getText());  
       }
       ps.executeUpdate();
      // Llenar(new String[]{"IDinvitado","nombre","apellido","telefono","direccion","sexo","email"},"invitados",7,table);
      // Limpiar(new JTextField []{txtNombreInvitado ,txtApellidoInvitado , txtTelefonoInvitado ,txtDireccionInvitado,txtEmailInvitado},5);
      bttnAgregarInvitado.setEnabled(true);
      bttnEliminarInvitado.setEnabled(true);
      bttnEditarInvitado.setEnabled(true);
      bttnGuardarInvitado.setEnabled(false);

   }catch (Exception e){JOptionPane.showMessageDialog(null, "Error "+ e.getMessage());};

}

The error occurs when I invoke the Save method with the following parameters:

 Guardar(jTable1 , "eventos", new String[]{" nombre=? , ","fecha = ? ,"," ubicacion = ? ,"," tipo = ? "},"IDevento", new JTextField[]{txtNombre , txtFecha , txtUbicacion,txtTipo},4);
'
    
asked by Ameno 02.08.2017 в 02:14
source

2 answers

0

To build the SQL statement from the array you have several options, but since you already manage the commas you can do:

String sql = "UPDATE "+ tablebd + " SET ";
for( String str : sentencia )
  sql += str;
sql += " WHERE "+ clave + " = " +table.getValueAt(fila,0);
    
answered by 02.08.2017 в 04:06
0

Dear, your error is in the following instruction: String sql = "UPDATE "+ tablebd + " SET " + sentencia.toString() +" WHERE "+ clave + " = " +table.getValueAt(fila,0);

Specifically in sentencia.toString() , in order to get all the parameters it is better to do this:

    String array[] = {"Hola","Mundo"};
    String str = "";
    for(String name : array){
        str += name;
    }
    
answered by 02.08.2017 в 04:48