I want to loop through documents in MongoDB. Basically here is the situation. I have some JTextfields which i want to populate from MongoDB. So each time the user click on the Next button, a new record must be fetched and display it in the JTextField. Here's my code:
public class nextstud implements ActionListener
{
public void actionPerformed(ActionEvent e) {
try {
Mongo s = new Mongo();
DB db = s.getDB( "omrs1" );
DBCollection coll = db.getCollection("Student") ;
DBCursor curs = coll.find();
if(curs.hasNext()) {
DBObject o = curs.next();
String fname = (String) o.get("Firstname") ;
String lname = (String) o.get("Lastname") ;
String sid = (String) o.get("StudentID") ;
String prg = (String) o.get("Programme") ;
String lvl = (String) o.get("Level") ;
txtfname.setText(fname) ;
}
btndelstud.setEnabled(true);
btnbkstud.setEnabled(true) ;
btnfwdstud.setEnabled(true);
} catch (UnknownHostException x) {
x.printStackTrace();
} catch (MongoException x) {
x.printStackTrace();
}
}
} // end class
However, it does not work. It only displays the first record each time i press the next button. If i change
if(curs.hasNext()) {
to
while(curs.hasNext()) {
It displays only the last record. Help please?