警告信息不显示

时间:2014-03-13 13:20:01

标签: mysql database if-statement javafx javafx-2

看看下面的代码。

void viewStudent() throws ClassNotFoundException, SQLException {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        connect2 = DriverManager
                .getConnection("jdbc:mysql://localhost:3306/project?"
                        + "user=root&password=virus");

        statement2 = connect2.createStatement();

        preparedStatement2 = connect2
                .prepareStatement("select regno from student where regno= "
                        + ""+Integer.parseInt(txt1.getText())+"");
        resultSet2 = preparedStatement2.executeQuery();
        checkRegno(resultSet2);

    } catch (ClassNotFoundException | SQLException e) {
        throw e;
    } finally {
        close1();
    }
}

void checkRegno(ResultSet resultSet2) throws SQLException {

    while (resultSet2.next()) {

        int trno = Integer.parseInt(resultSet2.getString("regno"));
        int regno = Integer.parseInt(txt1.getText());

        if (trno == regno) {
            resultSet2 = statement2
                    .executeQuery("select * from student where regno= " + regno + "");
            writeResultSet(resultSet2);
        } 
        else {
            final Text warning = new Text("Register Number Not Found...!");
            warning.setFill(Color.RED);
            warning.setStyle("-fx-font: 18 arial;");
            Stage dialogStage = new Stage();
            dialogStage.setTitle("Warning");
            dialogStage.initStyle(StageStyle.UTILITY);
            dialogStage.setScene(new Scene(VBoxBuilder.create().
                    children(warning).alignment(Pos.CENTER).
                    padding(new Insets(25, 25, 25, 25)).build()));
            dialogStage.show();
        }
    }
}

我在数据库中有一个名为'student'的表。当“if(trno == regno)”为真时,“writeResultSet(resultSet2);”工作正常。当它为假时,else部分不起作用。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

很可能你没有在FX线程上调用你的UI代码。尝试下一步:

Platform.runLater(new Runnable() {

    @Override
    public void run() {
        final Text warning = new Text("Register Number Not Found...!");
        warning.setFill(Color.RED);
        warning.setStyle("-fx-font: 18 arial;");
        Stage dialogStage = new Stage();
        dialogStage.setTitle("Warning");
        dialogStage.initStyle(StageStyle.UTILITY);
        dialogStage.setScene(new Scene(VBoxBuilder.create().
                children(warning).alignment(Pos.CENTER).
                padding(new Insets(25, 25, 25, 25)).build()));
        dialogStage.show();
    }
});