Advertisement
Guest User

Untitled

a guest
Sep 17th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. public void deleteRec(JComboBox code)
  2. {
  3. boolean flag1 = false;
  4. boolean flag2 = false;
  5. try
  6. {
  7. Class.forName("com.mysql.jdbc.Driver");
  8. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/stock","root","");
  9. String query = "SELECT stockCode FROM stock_transaction WHERE stockCode = '" + code.getSelectedItem() + "'";
  10. Statement stmt = con.createStatement();
  11. ResultSet rs = stmt.executeQuery(query);
  12. while(rs.next())
  13. {
  14.  
  15. }
  16. con.close();
  17. }
  18. catch(Exception e)
  19. {
  20. flag1 = true;
  21. }
  22. try
  23. {
  24. Class.forName("com.mysql.jdbc.Driver");
  25. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/stock","root","");
  26. String query = "SELECT stockCode FROM stock_master WHERE stockCode = '" + code.getSelectedItem() + "'";
  27. Statement stmt = con.createStatement();
  28. ResultSet rs = stmt.executeQuery(query);
  29. while(rs.next())
  30. {
  31.  
  32. }
  33. con.close();
  34. }
  35. catch(Exception e)
  36. {
  37. flag2 = true;
  38. }
  39. if(flag1 == true && flag2 == true)
  40. {
  41. try
  42. {
  43. Class.forName("com.mysql.jdbc.Driver");
  44. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/stock","root","");
  45. PreparedStatement st = con.prepareStatement("DELETE FROM stock_master WHERE stockCode = ?");
  46. st.setString(1, (String)code.getSelectedItem());
  47. st.executeUpdate();
  48. con.close();
  49. }
  50. catch(Exception e)
  51. {
  52. JOptionPane.showMessageDialog(null, "This code is already in the transaction table and cannot be deleted ");
  53. }
  54. }
  55. }
  56.  
  57. buttonDelete.addActionListener(new ActionListener() {
  58. @Override
  59. public void actionPerformed(ActionEvent e)
  60. {
  61. deleteRec(comboBoxStockCode);
  62. }
  63. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement