使用sqLite数据库在Javafx中进行登录表单验证

时间:2017-02-25 14:24:20

标签: java sqlite validation javafx

我在Javafx中编写了以下代码,以使用sqLite数据库验证登录表单。但问题是它总是抛出相同的错误"错误的用户名或密码"虽然我把有效的用户名和密码放在数据库中。

请记住,与数据库文件的连接是成功的,因为我在输入任何信息之前测试它,但不知道为什么它没有验证。 如果还有另一种验证形式的方法(比这更容易),请建议我。 谢谢!

这是 LoginModel.java

package sample;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class sqliteModel {
Connection connection;

public sqliteModel() throws SQLException, ClassNotFoundException {
    connection = sqliteConnection.Connector();
    if (connection == null) System.exit(1);
}

public boolean isDbConnected() throws SQLException {
    return !connection.isClosed();
}

public boolean loginValidateFunc(String userName, String password) throws SQLException {
    PreparedStatement preparedStatement;
    ResultSet resultSet;
    String query = "select * from UserDetails where Name = ? and Password = ?";

    try {
        preparedStatement = connection.prepareStatement(query);
        preparedStatement.setString(1,userName);
        preparedStatement.setString(2,password);
        resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {
            return true;
        } else {
            return false;
        }
    } catch (Exception e) {
        return false;
    }
}
}

这是 sqliteConnection

package sample;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class sqliteConnection {
    public static Connection Connector() throws SQLException, ClassNotFoundException {

            Class.forName("org.sqlite.JDBC");
            Connection conn = DriverManager.getConnection("jdbc:sqlite:UsersDatabase.sqlite");
            return conn;
    }

}

这是 Controller 类中的方法。

package sample;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.ToolBar;
import javafx.stage.Stage;

import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class Controller implements Initializable {
    public Controller() throws SQLException, ClassNotFoundException {
    }

    @FXML
    private ToolBar ttlbar;
    @FXML
    private TextField mailfield, passfield;
    @FXML
    private Label errorlabel, closebtn;

    public void login_func() throws Exception {

        String m = mailfield.getText();
        String p = passfield.getText();
        try {
            if (sqlModel.loginValidateFunc(mailfield.getText(),passfield.getText())) {
                errorlabel.setText("Login Success !");
            }
            else {
                errorlabel.setText("Wrong Email or Password !");
            }
        } catch (SQLException e) {
//            errorlabel.setText("Wrong Email or Password !");
            e.printStackTrace();

        }
    }


    public void closeFunc() {
        Stage s = (Stage) closebtn.getScene().getWindow();
        s.close();
    }

    public sqliteModel sqlModel = new sqliteModel();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        try {
            if (sqlModel.isDbConnected()) {
                errorlabel.setText("Connection Successful !");
            } else {
                errorlabel.setText("Connection Not Successful !");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}

0 个答案:

没有答案