尽管使用了正确的SQL语法,SQL查询仍未返回有效答案

时间:2018-10-18 04:13:48

标签: java mysql javafx xampp

我已经创建了一个Java项目,试图在其中创建一个Login页面。我的项目中包含以下程序 * LoginController.c * RegisterController.c * login.fxml * register.fxml

要使用户登录,他必须首先注册为新用户: 这是通过以下程序“ RegisterController”完成的。运行良好。用户名和密码已添加到表users中的MySql数据库IWS中。

编辑1:在此功能中,我被告知要删除寄存器控制器中用户名单词周围的单引号:

 boolean doesUserExist(String username)
        {
            boolean exist=false;
            String query="SELECT * FROM `users` WHERE username='%s'";

                try
                {
                    ResultSet set=
                            DBConnect.getStatement().executeQuery(String.format(query,username));
                    exist=set.next();
                    set.close();
                }catch(SQLException e)
                {
                    e.printStackTrace();
                }return exist;

        }

我将之前具有“用户名”的字符串查询更改为仅用户名。仍然无法登录

package controllers;

import IWS.DBConnect;
import IWS.Main;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

import java.awt.event.ActionEvent;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

public class RegisterController {

    @FXML
    private GridPane root;

    @FXML
    private PasswordField regpassword;

    @FXML
    private TextField regusername;

    @FXML
    private PasswordField regconpassword;

    @FXML
    private Button toregister;

    @FXML
    private Button backToLogin;

    @FXML
    private Label regstatus;

    @FXML
    void newRegister(javafx.event.ActionEvent event) {
        if(regusername.getText().isEmpty()||regpassword.getText().isEmpty()||
        regconpassword.getText().isEmpty())
        {
            regstatus.setText("please fill all details");
        }
        else if(!regpassword.getText().equals(regconpassword.getText()))
        {regstatus.setText("password do not match");

        }

        else if(doesUserExist(regusername.getText()))
        {
            regstatus.setText("username already exists");
        }

        else
        {
            //register user

                String query = "INSERT INTO `users`( `username`, `password`) VALUES ('%s','%s')";
                query = String.format(query, regusername.getText(), regpassword.getText());

            try
            {
                DBConnect.getStatement().executeUpdate(query);
                regstatus.setText("user registerd");
            }

            catch(SQLException e)
            {
                e.printStackTrace();
            }

        }

    }

    boolean doesUserExist(String username)
    {
        boolean exist=false;
        String query="SELECT * FROM `users` WHERE username='%s'";

            try
            {
                ResultSet set=
                        DBConnect.getStatement().executeQuery(String.format(query,username));
                exist=set.next();
                set.close();
            }catch(SQLException e)
            {
                e.printStackTrace();
            }return exist;

    }

    @FXML
    void openLogin(javafx.event.ActionEvent event)throws IOException {
        Stage loginStage= Main.stage;
        loginStage.setTitle("Login");
        root= FXMLLoader.load(getClass().getResource("/fxml/login.fxml"));
        loginStage.setScene(new Scene(root));
        loginStage.show();

    }

}

但是,在LoginController中,当我尝试检查用户输入的用户名和密码是否有效时,总是显示用户名和密码无效,尽管它们存在于MySQL数据库IWS和表用户中因为他们被加入了登记册。为什么会这样?

package controllers;

import IWS.DBConnect;
import IWS.Main;
import com.sun.org.apache.xerces.internal.util.Status;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;


import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import static javafx.fxml.FXMLLoader.load;

public class LoginController {


    @FXML
    private GridPane root;

    @FXML
    private TextField username;

    @FXML
    private PasswordField password;

    @FXML
    private Button login;

    @FXML
    private Button register;

    @FXML
    private Label status;

    @FXML
    void onLogin(javafx.event.ActionEvent event) throws IOException {
        String query="SELECT * FROM `users` WHERE username= 'abc' AND password = 'abc'";
        query=String.format(query,username.getText(),password.getText());

        if(username.getText().isEmpty()||password.getText().isEmpty())
        {
            status.setText("username and password cant be empty");
        }
        else
        {
            try
            {
                ResultSet set= DBConnect.getStatement().executeQuery(query);
                if(set.next())
                {
                    status.setText("Logged in successfully");
                    Stage mainscreenStage= Main.stage;
                    mainscreenStage.setTitle("Register");
                    root=FXMLLoader.load(getClass().getResource("/fxml/mainscreen.fxml"));
                    mainscreenStage.setScene(new Scene(root));
                    mainscreenStage.show();

                }
                else
                {
                    status.setText("invalid username or password");
                }
                set.close();
            }catch (SQLException e)
            {
                e.printStackTrace();
            }
        }



    }

    @FXML
    void onRegister(javafx.event.ActionEvent event)throws IOException {
        Stage registerStage= Main.stage;
        registerStage.setTitle("Register");
        root=FXMLLoader.load(getClass().getResource("/fxml/register.fxml"));
        registerStage.setScene(new Scene(root));
       registerStage.show();



    }

}

0 个答案:

没有答案