2014-08-28 51 views
0

我有2個框架,我們將它們命名爲「請求」和「確認」。現在當點擊「請求」中的按鈕時,調用框架「確認」。當點擊「是」時,應該執行SQL語句。問題在於SQL語句在類「請求」中。類別「確認」也應該從其他框架調用,所以我不想在「確認」中的按鈕「是」上製作Action Listener。點擊「是」按鈕,我該如何獲得一個完美的結果。如果它真在「申請的聲明」應執行 我怎麼能做到這一點通過獲取布爾值Java執行SQL語句

ReqBnt.addMouseListener(new MouseAdapter() { 

     public void mouseClicked(MouseEvent arg0) { 
      if (arg0.getSource() == ReqBnt) { 

       String VendN = txtVendN.getText(); 
       String VendSN = txtVendSN.getText(); 
       String Ad = txtAd.getText(); 
       String Ads = txtAds.getText(); 
       String City = txtCity.getText(); 
       String Zip = txtZip.getText(); 
       String UID = txtUID.getText(); 
       String Reg = txtReg.getText(); 
       String Rep = txtRep.getText(); 
       String Tel = txtTel.getText(); 
       String Fax = txtFax.getText(); 
       String Mail = txtMail.getText(); 
       String Iban = txtIban.getText(); 
       String Bic = txtBic.getText(); 
       String BInst = txtBInst.getText(); 
       String VendInfo = txtVendInfo.getText(); 
       String Flag = "Y"; 
       String PT = comboPT.getSelectedItem().toString(); 
       String Ctry = comboCtry.getSelectedItem().toString(); 
      try{ 
       try { 
        String sqlx = "SELECT `Abbreviation 2`, `Country English` From `database`.`country_master` " + 
          "WHERE `Country English` = '" + Ctry + "'"; 
        PreparedStatement pstx = conn.prepareStatement(sqlx); 
        ResultSet rs = pstx.executeQuery(); 
        while (rs.next()){ 
         partCode = rs.getString("Abbreviation 2"); 
         }}catch (Exception ex) { 
          System.out.println("Abbreviation Not Found"); 
         } 

       String sqlFind = "Select max(substring(`Vendor Code`, 3)) From `database`.`vendor_master`"; 
       try { 
        PreparedStatement pstFind = conn.prepareStatement(sqlFind); 
        ResultSet rs = pstFind.executeQuery(); 
        while (rs.next()){ 
        partNo = rs.getInt("Vendor Code")+1; 
         }}catch (Exception ex) { 
          partNo = 10001; 
        } 


       String VendC = partCode+partNo; 

       try { 
        String sqlx = "SELECT `Abbreviation Use`, `Country English` From `database`.`country_master` " + 
          "WHERE `Country English` = '" + Ctry + "'"; 
        PreparedStatement pstx = conn.prepareStatement(sqlx); 
        ResultSet rs = pstx.executeQuery(); 
        while (rs.next()){ 
         CtryC = rs.getString("Abbreviation Use"); 
         }}catch (Exception ex) { 
          System.out.println("Abbreviation Not Found"); 
         } 


        Calendar currentDate = Calendar.getInstance(); //Get the current date 
        SimpleDateFormat formatter= new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); 
        String dateNow = formatter.format(currentDate.getTime()); 
      //if 
      // 1.step: check if there are null values 
      // 2.step: if there are no null values -> call frame "Confirm" 
      // 3.step: if Confirm = true -> execute statement 
      //   if Confitm = fals -> do nothing 
       try { 
       String sql = "INSERT INTO `database`.`vendor_master`(`Vendor Code`,`Vendor Full Name`,`Vendor Short Name`,"+ 
           "`Address`,`Address Suffix`,`ZIP Code`,`City`,`Country`,`Country Code`,`UID No`,`Registration No`,"+ 
           "`Payment Term`,`Creation Date`,`IBAN`,`BIC`,`Banking Institution`,`Representive`,`Email Address`,"+ 
           "`Telefon No`,`Fax`,`Vendor Information`,`Active Flag`) "+ 
           "Values('"+VendC+"','"+VendN+"','"+VendSN+"','"+Ad+"','"+Ads+"','"+Zip+"','"+City+"','"+Ctry+"','"+CtryC+"','"+ 
           UID+"','"+Reg+"','"+PT+"','"+dateNow+"','"+Iban+"','"+Bic+"','"+BInst+"','"+Rep+"','"+Mail+"','"+Tel+"','"+ 
           Fax+"','"+VendInfo+"','"+Flag+"')"; 


        PreparedStatement pstExecute = conn.prepareStatement(sql); 
        pstExecute.execute(); 

        System.out.println("Vendor Registered"); 

        }catch (Exception ex) { 
         System.out.println("Please insert required Fields!"); 
        } 

       ///end if 
       }catch (Exception ex) { 
       System.out.println("Error: "+ex); 
      } 
     } 
     } 
    }); 
+0

顯示代碼,請! – Jens 2014-08-28 18:57:46

+0

我做了一個合作我想放置此動作的部分 – coo12 2014-08-28 19:06:09

回答

0

嘗試使用一個JDialog代替:

int dialogResult = JOptionPane.showConfirmDialog (
        null, 
        "Are you sure you want to do that?", 
        "Warning", 
        JOptionPane.YES_NO_OPTION); 
if(dialogResult == JOptionPane.YES_OPTION){ 
    //Do a thing here 
} 

你可以看看這裏的對話: http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

你甚至可以擴展JDialog的,而不是如果你想要更多的功能。

+0

謝謝,這是我在找的! – coo12 2014-08-29 06:59:10