如何检查JavaFx

时间:2019-06-24 10:31:15

标签: java

在图书馆管理系统中,我试图验证重复的主键条目。输入现有的会员ID时,应显示“会员ID已存在”。

MemberDbController.java

public static boolean checkMemberID(int memberId) throws SQLException, ClassNotFoundException {
        boolean memberIdExists = false;

        Connection conn=DBConnection.getDBConnection().getConnection();
        Statement stm = conn.createStatement();
        ResultSet rst = stm.executeQuery("SELECT * FROM members WHERE memberId='"+memberId+"'");

        String id;
        if (rst.next()){
            id = rst.getString("memberId");
            if(id.equals(memberId)){
                memberIdExists = true;
            }
        }
        return memberIdExists;
    }

MemberManagementController.java

@FXML
void addMember(ActionEvent event) {

        ValidationController valid = new ValidationController();

        if(valid.validateEmpty(memberIdField) && valid.validateEmpty(nameField) && valid.validateEmpty(doaField) &&
                valid.validateEmpty(emailField) && valid.validateEmpty(phoneField) && valid.validateDate(doaField))

        {
            int memberId = Integer.parseInt(memberIdField.getText());
            String name = nameField.getText();
            String doa = doaField.getText();
            RadioButton selectedRadioButton = (RadioButton) Gender.getSelectedToggle(); //Getting Selected Radio Button
            String gender = selectedRadioButton.getText();
            String email = emailField.getText();
            String phone = phoneField.getText();

            try {

                if(MemberDbController.checkMemberID(memberId)){

                    Member member = new Member(memberId,name,doa,gender,email,phone);
                    int i = MemberDbController.AddMember(member);

                    if (i > 0) {
                        Alert alert = new Alert(Alert.AlertType.INFORMATION);
                        alert.setTitle("Member Management");
                        alert.setHeaderText(null);
                        alert.setContentText("Member Added Successfully..!");
                        alert.showAndWait();

                        memberIdField.setText(null);
                        nameField.setText(null);
                        doaField.setText(null);
                        emailField.setText(null);
                        phoneField.setText(null);

                        //Table Refresh
                        data.clear();
                        loadTable();
                    }
                    else{
                        Alert alert = new Alert(Alert.AlertType.ERROR);
                        alert.setTitle("Member Management");
                        alert.setHeaderText(null);
                        alert.setContentText("There is an Error in Adding Member..!");
                        alert.showAndWait();
                    }

                }else{
                    Alert alert = new Alert(Alert.AlertType.ERROR);
                    alert.setTitle("Member Management");
                    alert.setHeaderText(null);
                    alert.setContentText("Member ID Already Exists..!");
                    alert.showAndWait();
                }

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

这没有验证,我得到的会员ID已经存在重复和新条目的警报。

1 个答案:

答案 0 :(得分:3)

您正在将StringInteger对象进行比较。这总是产生false

public static boolean checkMemberID(int memberId) ... {
    int memberId
    ...
    String id;
    ...
    if(id.equals(memberId)){

编译版本 id.equals(memberId)id.equals(Integer.valueOf(memberId))

无论如何都不需要检查列值,因为您的WHERE子句不会在该列中接受具有不同值的行。 (与字符串文字相比,这对我来说似乎很奇怪。您确定列的类型是文本类型吗?)

以下内容应足够:

public static boolean checkMemberID(int memberId) throws SQLException, ClassNotFoundException {
    Connection conn=DBConnection.getDBConnection().getConnection();
    Statement stm = conn.createStatement();
    ResultSet rst = stm.executeQuery("SELECT * FROM members WHERE memberId='"+memberId+"'");

    return rst.next();
}

此外,在addMember中,如果值不存在,而是当前值,则将结果视为返回true


请注意,假设这是表的唯一约束,尝试插入查询立即捕获SQLIntegrityConstraintViolationException实际上可能更好。否则,您可能需要检查有问题的错误,因为错误消息取决于所使用的数据库。