在一种方法中执行多种方法

时间:2019-02-13 10:01:04

标签: java swing

你好,我需要一个按钮进行检查,以便使用步骤检查来调用另一个方法以及其中一个是否有错误来显示它。我做到了,但它向我返回了最后一个被调用方法的消息,如果ii有错误他返回最后一个方法的消息时,它不会返回错误

按钮的代码:

   private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    connect1();
    try {
        Check1();
        Check2();
    } catch (SQLException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}  

Chech1方法和Check2方法的代码:

    private Integer Check1() throws SQLException {
    Integer records = 0;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    connect1();
    String query = "select count(*) from MSISDNs_for_Import (nolock)\n"
            + " where len(msisdn) <> 12 or len(sim) <> 12  or msisdn not like '35988%'";
    pstmt = conn1.prepareStatement(query);
    rs = pstmt.executeQuery();
    if (rs.next()) {
        records = rs.getInt(1);
        System.out.println("COUNT= " + records);
        selectedFilesField.setText("Check 1 successfull");
        rs.close();
        if (records > 1) {
            selectedFilesField.setText("Error in check 1");
        }
    }

    return records;
}

private Integer Check2() throws SQLException {
    Integer records = 0;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    connect1();
    String query = "select count(*) from SIMs_for_Import  (nolock)\n"
            + " where substring(imsi, 8, 1) <> '6'";
    pstmt = conn1.prepareStatement(query);
    rs = pstmt.executeQuery();
    if (rs.next()) {
        records = rs.getInt(1);
        System.out.println("COUNT= " + records);
        selectedFilesField.setText("Check 2 successfull");
        rs.close();
        if (records > 1) {
            selectedFilesField.setText("Error in check 2");
        }
    }
    return records;
}

目前,如果每笔支票都有一个按钮,但我需要将它们组合到一个按钮中,则可以正常工作。

2 个答案:

答案 0 :(得分:0)

您可以使两个方法都返回整数,然后以这种方式重组代码。

    private Integer Check1() throws SQLException {
    Integer records = 0;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    connect1();
    String query = "select count(*) from MSISDNs_for_Import (nolock)\n"
            + " where len(msisdn) <> 12 or len(sim) <> 12  or msisdn not like '35988%'";
    pstmt = conn1.prepareStatement(query);
    rs = pstmt.executeQuery();
    if (rs.next()) {
        records = rs.getInt(1);
        System.out.println("COUNT= " + records);
        selectedFilesField.setText("Check 1 successfull");
        rs.close();
    }

    return records;
}

private Integer Check2() throws SQLException {
    Integer records = 0;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    connect1();
    String query = "select count(*) from SIMs_for_Import  (nolock)\n"
            + " where substring(imsi, 8, 1) <> '6'";
    pstmt = conn1.prepareStatement(query);
    rs = pstmt.executeQuery();
    if (rs.next()) {
        records = rs.getInt(1);
        System.out.println("COUNT= " + records);
        selectedFilesField.setText("Check 2 successfull");
        rs.close();
    }
    return records;
}

那么您的实际代码将是

private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    connect1();
    try {
        if( Check1() > 0 ) {
           if( Check2() > 0 ) {
              //Do Something
           } else {
              selectedFilesField.setText("Error in check 2");
           }
        } else {
           selectedFilesField.setText("Error in check 1");
        }
    } catch (SQLException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}  

答案 1 :(得分:0)

相反,您也可以这样写:

private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    connect1();
    try {
        int i = Check1();
if(i < 0){
selectedFilesField.setText("Error in check 1");
}else{

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

通过这种方式,您可以直接设置检查1的错误消息(如果检查1失败)和检查2(如果检查2失败并且检查1成功)。