简体   繁体   中英

(Java)(SQL) I'm trying to check a user input against my database to login but I'm getting and arguement type mismatch error?

In my program, the user inputs a username and password which is already in my SQL database and it checks the database to ensure that the username and password are correct. However, when I try to input data and press the login button I get an argument type mismatch error I've already gotten it to add data to the database with a similar layout. Here's my code:

Database Connection class:

public class DataBaseConnect {

    public static Connection getConnection() throws Exception{
        try{
            //get connection to database
            String url = "jdbc:mysql://localhost:3306/csgoopenerdatabase?autoReconnect=true&useSSL=false";
            String user = "root";
            String password = "hockey33";
            Connection con = DriverManager.getConnection(url,user,password);
            System.out.println("Connected to database!");   
            //execute sql query
            //process result set  
            return con;
        }catch(Exception e){
            System.out.println(e);
        }
        return null;
    } 
}

Controller class:

public class LoginController implements Initializable {

        @FXML
        protected static TextField UsernameFieldL;

        @FXML
        protected static PasswordField PasswordFieldL;

        @FXML
        private void handleLoginBack(MouseEvent event) throws IOException{
            Parent handleInventoryBackParent = FXMLLoader.load(getClass().getResource("LoginCreateBuffer.fxml"));
            Scene OPBackScene = new Scene(handleInventoryBackParent);
            Stage handleInventoryBackStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
            handleInventoryBackStage.setScene(OPBackScene);
            handleInventoryBackStage.show();
        }

        @FXML
        private void handleLoginContinue(MouseEvent event) throws IOException, Exception{
            CheckDB();
        }

        public static void CheckDB() throws Exception{               
            try{
                //get connection to database
                Connection conn = DataBaseConnect.getConnection();
                //create a statement
                PreparedStatement checkInfo = conn.prepareStatement("SELECT * FROM userinfo WHERE Username='"+UsernameFieldL.getText()+"' AND '"+PasswordFieldL.getText()+"'");
                //process result set
                checkInfo.executeQuery();
                System.out.println("complete");
            }
            catch(Exception e){
                e.printStackTrace();
            }            
        }
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}

ERROR:

java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:75)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:279)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1451)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
    at javafx.event.Event.fireEvent(Event.java:171)
    at javafx.scene.Node.fireEvent(Node.java:6866)
    at javafx.scene.control.Button.fire(Button.java:179)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:193)
    at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:336)
    at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:329)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
    at javafx.event.Event.fireEvent(Event.java:171)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3369)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3209)
    at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3164)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1582)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2267)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:530)
    at com.sun.glass.ui.View.notifyMouse(View.java:924)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
    at java.lang.Thread.run(Thread.java:745)
Exception in thread "JavaFX Application Thread"

您在末尾加了引号,并且还缺少Password=

PreparedStatement checkInfo = conn.prepareStatement("SELECT * FROM userinfo WHERE Username='"+UsernameFieldL.getText()+"' AND Password= + '"+PasswordFieldL.getText()+"'");

I am going out on a limb here and will delete my answer if I am wrong, but I am guessing that your events are button presses. So you should change MouseEvent to ActionEvent in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM