Javafx:身份验证错误

穆伊斯·乌韦斯

数据库类

package com.application.Database;

import com.mongodb.*;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;


import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

import static jdk.nashorn.internal.runtime.regexp.joni.Syntax.Java;

public class DBEmployees {
    public static boolean authenticate(String username, String password){
        boolean flag;
        BasicDBObject andQuery = new BasicDBObject();
        List<BasicDBObject> obj = new ArrayList<>();
        obj.add(new BasicDBObject("email", username));
        obj.add(new BasicDBObject("password", password));
        andQuery.put("$and", obj);
        DBCursor cursor = collection.find(andQuery);
        ArrayList<BasicDBObject> x = new ArrayList<>();
        while (cursor.hasNext()) {
            x.add((BasicDBObject) cursor.next());
            cursor.next();
        }
        if(x.size()==1){
            flag = true;
        }
        else {
            flag = false;
        }
        return flag;
    }


    final static Logger globalLogger = Logger.getLogger(java.util.logging.Logger.GLOBAL_LOGGER_NAME);


    private static MongoClient mongoClient = new MongoClient( "localhost" , 27017 );

    private static DB db = mongoClient.getDB("mydb");
    private static DBCollection collection = db.getCollection("Employees");

    public static void main(String[] args)  {

        globalLogger.setLevel(java.util.logging.Level.OFF);

        BasicDBObject employee1  =  new BasicDBObject("name","Damith Asanka")
                    .append("age","34")
                    .append("email","[email protected]")
                    .append("staff type","specific")
                    .append("password","123456789B")
                    .append("space type","office_1")
                    .append("building number","2");
            collection.insert(employee1);
            BasicDBObject employee2 = new BasicDBObject("name","rname yname")
                    .append("age","18")
                    .append("email","[email protected]")
                    .append("staff type","authentic")
                    .append("password","1239256789A")
                    .append("space type","studio_3")
                    .append("building number","1");
            collection.insert(employee2);
            BasicDBObject employee3 = new BasicDBObject("name","fname sname")
                    .append("age","25")
                    .append("email","[email protected]")
                    .append("staff type","specific")
                    .append("password","123456XyZa")
                    .append("space type","office_2")
                    .append("building number","2");
            collection.insert(employee3);
            BasicDBObject employee4 = new BasicDBObject("name","Thumindu Dodantenna")
                    .append("age","19")
                    .append("email","[email protected]")
                    .append("staff type","specific")
                    .append("password","456dm2934")
                    .append("space type","office_6")
                    .append("building number","1");
            collection.insert(employee4);
        BasicDBObject employee5 = new BasicDBObject("name","xname xmas")
                .append("age","19")
                .append("email","[email protected]")
                .append("staff type","specific")
                .append("password","456d11112934")
                .append("space type","office_4")
                .append("building number","1");
        collection.insert(employee5);
    }
}

控制器类

package com.applicationLoginInterface;


import javafx.fxml.FXML;

import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;

import com.application.Database.*;


import java.io.IOException;


public class ControllerLogin {


@FXML private PasswordField loginPassword;
@FXML private TextField loginID;


public static void main(String[] args){

}

@FXML private void loginBtnClicked() throws IOException {

    String username = loginID.getText();
    String password = loginPassword.getText();
    if(DBEmployees.authenticate(username,password)){
        System.out.println("Success");
    }
    else {
        System.out.println("Incorrect login credentials");
    }

  }
}

我收到的错误消息如下:

 Dec 29, 2017 9:20:26 PM com.mongodb.diagnostics.logging.JULLogger log INFO: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
 Dec 29, 2017 9:20:27 PM com.mongodb.diagnostics.logging.JULLogger log INFO: Cluster description not yet available. Waiting for 30000 ms before timing out
 Dec 29, 2017 9:20:27 PM com.mongodb.diagnostics.logging.JULLogger log INFO: Opened connection [connectionId{localValue:1, serverValue:51}] to localhost:27017
 Dec 29, 2017 9:20:27 PM com.mongodb.diagnostics.logging.JULLogger log INFO: Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 4, 7]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=null, roundTripTimeNanos=1507972}
 Dec 29, 2017 9:20:27 PM com.mongodb.diagnostics.logging.JULLogger log INFO: Opened connection [connectionId{localValue:2, serverValue:52}] to localhost:27017
 Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8413)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:748)
 Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
    ... 48 more
 Caused by: java.util.NoSuchElementException
    at com.mongodb.DBCursor.next(DBCursor.java:162)
    at com.application.Database.DBEmployees.authenticate(DBEmployees.java:23)
    at com.applicationLoginInterface.ControllerLogin.loginBtnClicked(ControllerLogin.java:30)
    ... 58 more

我在 Controller 类中的消息未显示

费边

stext属性TextField放入BasicDBObjects 而不是TextFields 本身:

...
obj.add(new BasicDBObject("email", username.getText()));
obj.add(new BasicDBObject("password", password.getText()));
...

编辑:

您的编辑引入了另一个错误:当可能只有一条剩余记录时,您调用了next两次而没有hasNext在将光标推进 2 次之间进行检查删除第二个调用next

while (cursor.hasNext()) {
    BasicDBObject dbObject = (BasicDBObject) cursor.next();
    x.add(dbObject);
    // use dbObject to access the object
    // since the cursor has already been placed at the next position
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章