如何避免在javaFX中将相同的数据插入数据库

时间:2015-04-30 11:14:39

标签: jdbc javafx-2

我正在尝试使用JavaFX&amp ;;创建库存管理系统。 MySQL的。 在第一阶段,我在用户注册控制上堆叠。 我无法停止将相同的userName插入MySql数据库。

这是我的代码

    @FXML
    private Label AlartLable;
    @FXML
    private TextField UsrNameTF;
    @FXML
    private TextField FullNameTF;
    @FXML
    private TextField EmailAddressTF;
    @FXML
    private PasswordField PassWord;
    @FXML
    private PasswordField RepassWord;
    @FXML
    private Label passAlart;
    @FXML
    private Label passNotMatch;
    @FXML
    private Label usrLabel;
    @FXML
    private Label fullNameLabel;
    @FXML
    private Label emailAddresslbl;

    private PreparedStatement pst;
    private Connection con;
    private ResultSet rs;

    public void SignUpBtn(ActionEvent event) {

        if (isValidCondition()) {

            DBConnection toCon = new DBConnection();
            Connection connected = toCon.geConnection();

            try {
                pst = connected.prepareStatement("select * from User where UsrName = ?");
                pst.setString(1, UsrNameTF.getText());
                rs = pst.executeQuery();
                while(rs.next()){
                    if(rs.getString(1).matches(UsrNameTF.getText())){
                        AlartLable.setText("User Name already Exist");

                    }else{

                    }
                }
               AlartLable.setText("Regestration Sucess");
               reg();


            } catch (SQLException ex) {
                Logger.getLogger(RegestrationController.class.getName()).log(Level.SEVERE, null, ex);
            }

        } else {
            AlartLable.setText("");
        }
    }

    public void goToLoginPage(ActionEvent actionEvent) throws IOException {
        Parent logeIn = FXMLLoader.load(getClass().getResource("/FXMLGui/Login.fxml"));
        Scene logeInScene = new Scene(logeIn);
        Stage logInStage = (Stage) ((Node) actionEvent.getSource()).getScene().getWindow();
        logInStage.setScene(logeInScene);
        logInStage.setResizable(false);
        logInStage.show();
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }

    private boolean isValidCondition() {
        boolean Regestration = false;
        if (nullCheck(UsrNameTF, usrLabel)
                && nullCheck(FullNameTF, fullNameLabel)
                && nullCheck(EmailAddressTF, emailAddresslbl)
                && passCheck() && passMatch()) {

            Regestration = true;

        } else {
            Regestration = false;
        }
        return Regestration;
    }

    private boolean nullCheck(TextField values, Label label) {
        boolean nullcheck = false;
        if (values.getText().trim().length() == 0) {
            label.setText("Fill it");
        } else {
            label.setText("");
            nullcheck = true;
        }
        return nullcheck;
    }

    private boolean passCheck() {
        boolean password = false;
        int myPass = PassWord.getText().length();
        if (myPass >= 5) {
            passAlart.setText("");
            password = true;
        } else {
            passAlart.setText("Min 5");
        }
        return password;
    }

    private boolean passMatch() {
        boolean passMatch = false;
        String password = PassWord.getText();
        String rePass = RepassWord.getText();

        if (password.matches(rePass)) {
            passAlart.setText("");
            passNotMatch.setText("");
            passMatch = true;

        } else {
            passNotMatch.setText("Not Match");
            passMatch = false;
        }
        return passMatch;

    }

    private void reg() {
        DBConnection newCon = new DBConnection();

        con = newCon.geConnection();

        try {
            pst = con.prepareStatement("insert into User values(?,?,?,?)");
            pst.setString(1, UsrNameTF.getText());
            pst.setString(2, FullNameTF.getText());
            pst.setString(3, EmailAddressTF.getText());
            pst.setString(4, PassWord.getText());

            pst.executeUpdate();

        } catch (SQLException ex) {

            Logger.getLogger(RegestrationController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void clearAllTF() {
        UsrNameTF.clear();
        FullNameTF.clear();
        EmailAddressTF.clear();
        PassWord.clear();
        RepassWord.clear();
    }

}

当我尝试使用现有用户名注册时,我认为它显示了我的User Name Already Exist 但我将数据推送到数据库并由于usrName是唯一字段而给我一个sql异常。

错误

  

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:   重复进入' rifat'关键字' UsrName'

0 个答案:

没有答案