0

我正在尝试与用户确认一些消息,如果他说他确定编写的代码应该执行,但是如果他拒绝 jconformation 框,即使我取消或按否,代码也不应该在我的情况下执行,代码仍然会被执行并且数据被提交。我能做些什么来阻止这个我的代码在下面给出......

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        if(evt.getSource()==jButton1){
                JOptionPane.showConfirmDialog(rootPane, "Are You Sure You Want to Submit This Data and Genertate a New Remittance Id!");

                DBUtil util = new DBUtil();
        try {

            Connection con = util.getConnection();
            PreparedStatement stmt = con.prepareStatement("INSERT INTO dbo.bk_det(rm_id,bk_name,bk_branch,bk_add,bk_ref,ref_dt) VALUES (?,?,?,?,?,?)");
            String rm = (tf_rm_id.getText().trim() == null || tf_rm_id.getText().equals("")) ? "0" : tf_rm_id.getText();
            Object bkn = (cb_bkname.getSelectedItem() == null || cb_bkname.getSelectedItem().equals("")) ? "NILL" : cb_bkname.getSelectedItem();
            Object bkbn = (cb_brname.getSelectedItem() == null || cb_brname.getSelectedItem().equals("")) ? "NILL" : cb_brname.getSelectedItem();
            Object bkpln = (cb_plname.getSelectedItem() == null || cb_plname.getSelectedItem().equals("")) ? "NILL" : cb_plname.getSelectedItem();
            String rf = (tb_bkref.getText().trim() == null || tb_bkref.getText().equals("")) ? "NILL" : tb_bkref.getText();
            String rfdt = (tf_refdt.getText().trim() == null || tf_refdt.getText().equals("")) ? "NILL" : tf_refdt.getText();
            stmt.setString(1, ""+(rm));
            stmt.setString(2, ""+(bkn));
            stmt.setString(3, ""+(bkbn));
            stmt.setString(4, ""+(bkpln));
            stmt.setString(5, ""+(rf));
            stmt.setString(6, ""+(rfdt));
            stmt.execute();

            PreparedStatement stmt2 = con.prepareStatement("INSERT INTO bk_rep(rm_id, br_name, br_desig, br_pf, dt_rep, mob) VALUES (?,?,?,?,?,?)");
            String rm1 = (tf_rm_id.getText().trim() == null || tf_rm_id.getText().equals("")) ? "0" : tf_rm_id.getText();
            String brn = (tfbrname.getText().trim() == null || tfbrname.getText().equals("")) ? "NILL" : tfbrname.getText();
            String brpf = (tf_brpf.getText().trim() == null || tf_brpf.getText().equals("")) ? "NILL" : tf_brpf.getText();
            String brdes = (tf_brdes.getText().trim() == null || tf_brdes.getText().equals("")) ? "NILL" : tf_brdes.getText();
            String brdtrep = (tf_rm_dt.getText().trim() == null || tf_rm_dt.getText().equals("")) ? "NILL" : tf_rm_dt.getText();
            String brmob = (tf_brmob.getText().trim() == null || tf_brmob.getText().equals("")) ? "NILL" : tf_brmob.getText();
            stmt2.setString(1, ""+(rm1));
            stmt2.setString(2, ""+(brn));
            stmt2.setString(3, ""+(brdes));
            stmt2.setString(4, ""+(brpf));
            stmt2.setString(5, ""+(brdtrep));
            stmt2.setString(6, ""+(brmob));

            stmt2.execute();

            PreparedStatement stmt3 = con.prepareStatement("INSERT INTO bk_sec([bs_name],[bs_desig],[rm_id],[dt_rep]) VALUES (?,?,?,?)");
            String rm2 = (tf_rm_id.getText().trim() == null || tf_rm_id.getText().equals("")) ? "0" : tf_rm_id.getText();
            String sn = (tfsname.getText().trim() == null || tfsname.getText().equals("")) ? "NILL" : tfsname.getText();
            String sdes = (tf_sdes.getText().trim() == null || tf_sdes.getText().equals("")) ? "NILL" : tf_sdes.getText();
            String bsdtrep = (tf_rm_dt.getText().trim() == null || tf_rm_dt.getText().equals("")) ? "NILL" : tf_rm_dt.getText();



            stmt3.setString(1, ""+(sn));
            stmt3.setString(2, ""+(sdes));
            stmt3.setString(3, ""+(rm2));
            stmt3.setString(4, ""+(bsdtrep));


            stmt3.execute();
            JOptionPane.showMessageDialog(null, "COMMITED SUCCESSFULLY!");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());

        }   
        CalendarUtil cal=new CalendarUtil();
        tf_rm_id.setText(cal.getRemId());
        String datePrefix = new SimpleDateFormat("MMMM dd, YYYY").format(new Date());
        tf_rm_dt.setText(datePrefix); 
        tb_bkref.setText("");
        tf_refdt.setText("");
        tf_brpf.setText("");
        tf_brdes.setText("");
        tf_brmob.setText("");
        tfsname.setText("");
        tf_sdes.setText("");
        tfbrname.setText("");
        tf_scity.setText("");

        }
    }      
4

1 回答 1

1

如果您仔细查看 showConfirmDialog,您会发现它正在返回一个整数。我将引用返回的 API。

Returns: an integer indicating the option selected by the user

返回应该是按钮的顺序,你进入了确认对话框,所以在你的情况下应该是 2。

你现在只需要一个 if 语句,它检查这个值

你可以这样做:

int buffer = JOptionPane.showConfirmDialog(rootPane, "Are You Sure You Want to Submit This Data and Genertate a New Remittance Id!")

// user pressed yes
if(buffer == 0) {
    try {
        // whatever
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage());
    }
}
else if(buffer == 1) {
     // user input = no
}

else if(buffer == 2) {
     // user input = cancel
}
于 2013-01-08T08:11:35.267 回答