0

i'm just new to connecting MySql database to my java GUI and i have this problem. In a button I have two query, I want the second query not to run when the first query is not executable.

heres my button action:

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);
    }

and heres my theQuery to be able to execute the commands:

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());
      }
  }

please help me

1 Answers1

0

Since it appears you are accepting user input and storing it into a database, I highly recommend you use prepared statements in order to handle your queries. You can follow the answer given here and then look at the users code above in order to verify that the statement was indeed successful. Also, when using the prepared statements in your code, have the method return an Integer and then returning the value from executeUpdate() upon completion, then use the if statement to see if the value is greater than 0, and if it is, the query was successful (successful here meaning it affected 1 or more rows).

Community
  • 1
  • 1
Stephen
  • 376
  • 1
  • 3
  • 13