In variable, assign value, change it in one function and pass the changed value to another

1

I'll get to the point; I'm doing something like this:

public class clase{//clase principal
int valor;//variable global

if (post.containsKey("xempieza")) { 
            if(valida()){
            bit.bitHtml = recibedato(); //bit.bitHtml obtiene y añade el html que previamente existia. 
            return;
            }
        }

 public string enviadato(){
  String html="<cosas html>;
  Connection conn=null;
  PreparedStatement ps=null;
  ResultSet rs=null;

  try{
  conn=*base datos*;
  ps=conn.preparestatement(select valor, nombre from miTabla)//hago consulta y busco datos.
  rs=ps.executeQuery();

  int fila=0;
  while (rs.next)){
    valor=rs.getInt("valor");//Aqui empiezo; quiero darle el valor extraido de mi consulta a mi variable global.  
    html+="<input type='submit' value='"+rs.getString("nombre")+"' name='xempieza'>"
        +"<input type='hidden' value='"+ rs.getInt("valor") + "' name='xvalor'>";//el principal inconveniente es que traigo varios valores ,para que cree varios input con los datos obtenidos, cada uno con su identificador(valor).
  }
  }catch (Exception e) {}
  return html;
 }

 public boolean valida(){
        String validavalor;
        if (post.containsKey("xvalor")){ //obtiene el hidden
            validavalor=((String[]) post.get("xvalor"))[0].trim(); 
            valor=Integer.parseInt(validavalor); //convierte la cadena obtenida a int
        }
        return true;
    }



 public string recibedato(){
  String html="<otra pagina html>";
  /*coneccion, rs y ps*/
  ...
  ps=conn.prepareStatement("select datos from mitabla2 where datosid=?");
  ps.setInt(valor);
  rs=ps.executeQuery();
  if(rs.next)){
   html+="<iframe src='"+rs.getString("datos")+"'>"; //sin embargo, al obtener la variable global, el valor de esta es 1, sin importar en cual de los input haya clickeado, aun cuando cada uno tiene un identificador(valor) diferente. 
  }
 }
}

/ Well that. Broadly speaking, I want to get the identifier generated from a while, assign it to a global variable, and have the value be passed to another function without being modified. Here I have two doubts; the first is how to make the value pass from one function to another without losing the value assigned in the first function. The second one, is like knowing if the problem comes in my WHILE, since as they are generated buttons, I do not know if when changing function, they are lost and assign a generic value, or the first value obtained from the while .... I hope to be understood. /

EDIT:

I have already been studying it, and the problem lies in my while. This is where your help would be solicitanto. As I said, my buttons are generated according to the data obtained from the sql. However, when passing the get ("xvalue"), and as it comes in an array, it only gets the one from the position 0 ([0] .trim ()). What I would like to know is how I can do so that when the user clicks on some button, he sends me the position of the button ([0 or 1 or 2 oetc] .trim ()). Again, I hope your help, Greetings!

    
asked by J.Carlos 18.04.2018 в 18:21
source

0 answers