Search code examples
javamongodbuser-interfaceregistrationnetbeans-8

Why it is going in infinite loop


Here, what i wanted to do is that if given userid already exists then it should clear all the fields. and restart that page ....

private void uuserActionPerformed(java.awt.event.ActionEvent evt) {
    MongoClient mClient;
    DB mDB;
    DBCollection valCollection;
    Cursor cursor;

    try {
        mClient = new MongoClient(ip, port);

        System.out.println("Connected to mongodb instance at " + ip + ":" + port);
        mDB = mClient.getDB(dbName);
        valCollection = mDB.getCollection(collectionName);

        userid = uuser.getText();

        BasicDBObject findPara = new BasicDBObject();
        BasicDBObject findCon = new BasicDBObject();

        findPara.append("userid", userid);
        findCon.append("userid", true);
        findCon.append("_id", false);

        cursor = valCollection.find(findPara, findCon);

        flag = 1;
        while (cursor.hasNext()) {
            flag = 0;
            System.out.println("User ID is not correct!!!");
        }

        if (flag == 0) {
            System.out.println("User Id is not correct...");

            dispose();
            new Register().setVisible(true);
        }

        System.out.println("Move Forward Your Id is correct...");

    } catch (UnknownHostException e) {
        e.printStackTrace();
        System.exit(-1);
    }
}  

Solution

  • The code never advances to the next record.

    The loop should contain

    cursor.next()
    

    to consume the result.