如何停止第二个查询运行时,第一个查询是不可执行的java

问题描述:

我只是新的连接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

由于看来你是接受用户输入并将其存储到数据库中,我强烈建议您使用准备好的语句以便处理您的查询秒。您可以按照给定的答案here,然后查看上面的用户代码,以验证语句确实是成功的。另外,在代码中使用准备好的语句时,请让方法返回一个整数,然后在完成时返回值为executeUpdate(),然后使用if语句查看该值是否大于0,如果是,查询是成功的(这里的成功意味着它影响了一行或多行)。