my problem is that I have the following query:
SELECT respuestaId, respuesta, estado, r.preguntaId FROM respuestas r, preguntas p WHERE r.preguntaId = p.preguntaId AND r.preguntaId = 1
The result of the query is 4 responses, for example "resp1", "resp2", "resp3", "resp4". I keep it in the vector in the following way:
clsMtoRespuestas resp = new clsMtoRespuestas();
resp.setPreguntaId(pregunta);
if(resp.ConsultarRespuestasPorPregunta()) {
for(int i = 0; i <= 3;i++){
respuestas[i] = resp.getRespuesta();
}
}
My idea is that each answer is saved in a space of the vector but when it comes to showing these answers in a few jButtons it shows that only the first answer ("resp1") has been saved in the 4 spaces of the vector.
(I also put the code of how I show them so that there is no doubt whether I am the one that repeats the indices)
btnResp1.setText(respuestas[0]);
btnResp2.setText(respuestas[3]);
btnResp3.setText(respuestas[1]);
btnResp4.setText(respuestas[2]);
I want to know if there is an error in the way of saving the answers in the vector or if there is another way to do it. In advance, thank you.
I call the data from a class like this:
public boolean ConsultarRespuestasPorPregunta() {
boolean resp = false;
try{
String sql = "SELECT respuestaId, respuesta, estado, r.preguntaId FROM respuestas r, preguntas p WHERE r.preguntaId = p.preguntaId AND r.preguntaId = ?;";
PreparedStatement cmd = cn.prepareStatement(sql);
cmd.setInt(1, preguntaId);
ResultSet rs = cmd.executeQuery();
if(rs.next()){
resp = true;
respuestaId = rs.getInt(1);
respuesta = rs.getString(2);
estado = rs.getInt(3);
preguntaId = rs.getInt(4);
}
cmd.close();
//getCn().close();
}
catch (SQLException e){
System.out.println(e.toString());
}
return resp;
}