2017-04-27 37 views
0

我只是新的連接MySql數據庫到我的Java GUI,我有這個問題。在一個按鈕中,我有兩個查詢,我希望第二個查詢在第一個查詢不可執行時不運行。如何停止第二個查詢運行時,第一個查詢是不可執行的java

我的繼承人按鈕動作:

private void addActionPerformed(java.awt.event.ActionEvent evt) {          
     theQuery("INSERT INTO `deliv`(`prod_code`, `client`, `address`,`quantity`,`delivery_date`,`deliv_code`) VALUES ('"+prodC.getText()+"','"+client.getText()+"','"+address.getText()+"','"+quantity.getText()+"','"+delivdate.getText()+"','"+delivCode.getText()+"')"); 
     theQuery("Update product_list set prod_stock=prod_stock-'"+quantity.getText()+"'where b.prod_code="+prodC.getText()); 
     this.setVisible(false); 
     new adminLogin().setVisible(true); 
    } 

和我的繼承人theQuery能夠執行的命令:

public void theQuery(String query){ 
     Connection con = null; 
     Statement st = null; 
     try{ 
      con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/dbsystem","root",""); 
      st = con.createStatement(); 
      st.executeUpdate(query); 
      JOptionPane.showMessageDialog(null,"Success!"); 
     }catch(Exception ex){ 
      JOptionPane.showMessageDialog(null,ex.getMessage()); 
     } 
    } 

請幫我

+0

請更改您的問題 - 這是不可理解的 –

+0

*,並且當該按鈕中的第一個查詢不可執行時,如果第二個查詢中的查詢可執行,則第二個查詢仍在執行時不會執行* - no想法這是什麼意思 –

+0

它現在編輯先生 – topher

回答

0

由於看來你是接受用戶輸入並將其存儲到數據庫中,我強烈建議您使用準備好的語句以便處理您的查詢秒。您可以按照給定的答案here,然後查看上面的用戶代碼,以驗證語句確實是成功的。另外,在代碼中使用準備好的語句時,請讓方法返回一個整數,然後在完成時返回值爲executeUpdate(),然後使用if語句查看該值是否大於0,如果是,查詢是成功的(這裏的成功意味着它影響了一行或多行)。

相關問題