2017-12-29 24 views
0

Databaseクラスの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); 
    } 
} 

Contorllerクラス

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 

私のメッセージは」doesnの表示するには

答えて

0

BasicDBObjectTextField秒のtextプロパティの値を入れてよ代わりのTextFieldそのものだ:

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

EDIT:

編集内容は、別のエラーを導入しました:あなたはチェックせずに二回nextを呼び出しますhasNextの間に残りのレコードが1つしかない場合はカーソルを2回進めます。 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 
} 
+0

違いはありません。 –

+0

@MuizUvais違いがあります。エラーメッセージが変更されました。別のエントリが存在する場合、ckeckingなしでループごとに 'next'を2回呼び出さないでください... – fabian

+0

ああ、それは今動作します。どうもありがとうございました –