In my code, while(rs.next()) is returning only the last row data. I have also tried using if(rs.next()) but it will give me only the data of the first row.
Please help me resolve this.
ResultSet rs=stmt.executeQuery();
{ while(rs.next())
{ System.out.println(rs.getInt(1)+" "+rs.getInt(8));
l.setText(rs.getInt(2)+" "+rs.getString(3));
jb[0].setText(rs.getString(4));
jb[1].setText(rs.getString(5));
jb[2].setText(rs.getString(6));
jb[3].setText(rs.getString(7));
l.setVisible(true);
}
}
l.setBounds(30,40,450,20);
for(int i=0,j=0;i<=90;i+=30,j++)
jb[j].setBounds(50,80+i,200,20);
You keep overwriting the values in jb
with each iteration. The last element in the RS
is the last one written to jb
- by the time you are using jb
only the last element is left in it.
I am not sure what you need to use the results for but you can use a 2D array of Strings
to store all the values. Something along the lines of:
Updated based on comments, new understanding of question
ResultSet rs = stmt.executeQuery();
// stores the results into a collection
List<JRadioButton[]> radioButtonList = new ArrayList<>();
List<JLabel> labelList = new ArrayList<>();
// iterate thru result set
while(rs.next()){
// prints to console
System.out.println(rs.getInt(1) + " " + rs.getInt(8));
// init label
JLabel l = new JLabel();
l.setBounds(30,40,450,20);
// set text for label
l.setText(rs.getInt(2) + " " + rs.getString(3));
// at least one element, set to visible
l.setVisible(true);
// store values from rs into an array of radio buttons
JRadioButton[] jb = new JRadioButton[5];
jb[0].setText(rs.getString(4));
jb[1].setText(rs.getString(5));
jb[2].setText(rs.getString(6));
jb[3].setText(rs.getString(7));
// store object in list for future use
radioButtonList.add( jb );
//store label in list
labelList.add(l);
}
// now loop thru the lists
for(JRadioButton[] jbArray : radioButtonList){
// your code here - update to suit your needs
// jbArray[4].setBounds(50,80,200,20); // not sure what the values should be here
}
for(JLabel jLabel: labelList){
// your code here - update to suit your needs
// jLabel.setBounds(50,80,200,20); // not sure what the values should be here
}